Click here to Skip to main content
15,888,610 members
Home / Discussions / C#
   

C#

 
AnswerRe: unblock some item in form Pin
Dr.Walt Fair, PE9-Jul-11 10:38
professionalDr.Walt Fair, PE9-Jul-11 10:38 
GeneralRe: unblock some item in form Pin
BobJanova10-Jul-11 22:19
BobJanova10-Jul-11 22:19 
GeneralRe: unblock some item in form Pin
DaveAuld11-Jul-11 2:47
professionalDaveAuld11-Jul-11 2:47 
GeneralRe: unblock some item in form Pin
BobJanova11-Jul-11 3:37
BobJanova11-Jul-11 3:37 
QuestionCreate Dial Up Connection Pin
jojoba20119-Jul-11 6:06
jojoba20119-Jul-11 6:06 
AnswerRe: Create Dial Up Connection Pin
Heath Stewart10-Jul-11 16:08
protectorHeath Stewart10-Jul-11 16:08 
QuestionBreak code execution from base class in inherited form Pin
RobScripta9-Jul-11 4:30
professionalRobScripta9-Jul-11 4:30 
AnswerRe: Break code execution from base class in inherited form Pin
Heath Stewart9-Jul-11 9:16
protectorHeath Stewart9-Jul-11 9:16 
That's how subtyping works. When you crete an instance of the child class, all code is executing against that instance. If the base class defines handlers for events like in your case, it still executes against that instance of the child class you created.

But what is it that's not working? Both events can be defined on the base class, like so:

internal abstract class MyFormBase : Form
{
  DataGridView dataGridView1;

  protected MyFormBase()
  {
    dataGridView1 = new DataGridView();
    dataGridView1.CellClick += OnDataGridViewCellClick;
    // Other initialization
  }

  protected virtual string Message
  {
    get { return "Message from the base class."; }
  }

  protected virtual void OnDataGridViewCellClick(object sender, DataGridViewCellEventArgs e)
  {
    // Override this in your child class if you wish.
  }

  protected virtual void OnDataGridViewColumnHeaderMouseClick(object sender, DataGridViewBindingCompleteEventArgs e)
  {
    MessageBox.Show(Message, "Test");
  }
}

internal class MyChildForm : MyFormBase
{
  protected override string Message
  {
    get { return "Message from the child class."; }
  }

  protected override void OnDataGridViewCellClick(object sender, DataGridViewCellEventArgs e)
  {
    MessageBox.Show("You clicked a cell.", "Test");

    // Example of how to call a base method. Not always required, and in this case doesn't do anyway.
    // However, for some virtual methods it's very important to call the base so read documentation.
    base.OnDataGridViewCellClick(sender, e);
  }
}


Now when you create an instance of MyChildForm and click a column header, you'll see "Message from the child class" even though the event handler is defined in the base class. It calls the virtual (overridable) property which IS the property on the MyChildForm instance. If you want to call the base class's method, use base.

You also overrided the CellClick event handler which will show "You clicked a cell." I also show an example of calling the base class's method though in this case it doesn't do anyway. If it would never do anything, define it as abstract instead of virtual and remove the body like so:

private abstract void OnDataGridViewCellClick(object sender, DataGridViewCellEventArgs e);


Hopefully this shows you an example of how polymorphism is working such that you can define your handlers in your base class but have it access the data - expectedly - in the child class.
This posting is provided "AS IS" with no warranties, and confers no rights.

Program Manager II
Visual Studio Professional Deployment Experience
Microsoft

[My Articles] [My Blog]

GeneralRe: Break code execution from base class in inherited form Pin
RobScripta9-Jul-11 9:29
professionalRobScripta9-Jul-11 9:29 
GeneralRe: Break code execution from base class in inherited form Pin
Dave Kreskowiak9-Jul-11 10:12
mveDave Kreskowiak9-Jul-11 10:12 
GeneralRe: Break code execution from base class in inherited form Pin
Heath Stewart10-Jul-11 16:16
protectorHeath Stewart10-Jul-11 16:16 
Questiondouble buffering Pin
shvanisingh9-Jul-11 3:54
shvanisingh9-Jul-11 3:54 
AnswerRe: double buffering Pin
fjdiewornncalwe9-Jul-11 4:57
professionalfjdiewornncalwe9-Jul-11 4:57 
AnswerRe: double buffering Pin
OriginalGriff9-Jul-11 5:24
mveOriginalGriff9-Jul-11 5:24 
AnswerRe: double buffering Pin
Pete O'Hanlon9-Jul-11 6:31
mvePete O'Hanlon9-Jul-11 6:31 
Questionclass with dynamic property Pin
mehrdadc488-Jul-11 21:14
mehrdadc488-Jul-11 21:14 
AnswerRe: class with dynamic property Pin
Abhinav S8-Jul-11 21:31
Abhinav S8-Jul-11 21:31 
AnswerRe: class with dynamic property Pin
Pete O'Hanlon8-Jul-11 22:18
mvePete O'Hanlon8-Jul-11 22:18 
QuestionCheckBox ListView Multiple Selection Problem c# [modified] Pin
varunpandeyengg8-Jul-11 4:52
varunpandeyengg8-Jul-11 4:52 
AnswerRe: CheckBox ListView Multiple Selection Problem c# Pin
Shahriar Iqbal Chowdhury/Galib8-Jul-11 11:07
professionalShahriar Iqbal Chowdhury/Galib8-Jul-11 11:07 
GeneralRe: CheckBox ListView Multiple Selection Problem c# Pin
varunpandeyengg8-Jul-11 20:58
varunpandeyengg8-Jul-11 20:58 
AnswerRe: CheckBox ListView Multiple Selection Problem c# Pin
DaveyM699-Jul-11 0:53
professionalDaveyM699-Jul-11 0:53 
GeneralRe: CheckBox ListView Multiple Selection Problem c# Pin
varunpandeyengg10-Jul-11 21:18
varunpandeyengg10-Jul-11 21:18 
AnswerRe: CheckBox ListView Multiple Selection Problem c# Pin
varunpandeyengg11-Jul-11 1:00
varunpandeyengg11-Jul-11 1:00 
QuestionData in the gridview cell should get select Pin
NarVish8-Jul-11 0:54
NarVish8-Jul-11 0:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.