Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Break code execution from base class in inherited form

Break code execution from base class in inherited form

Scheduled Pinned Locked Moved C#
questioncsharpwinforms
5 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RobScripta
    wrote on last edited by
    #1

    I fear this question has been asked a thousand times, but the keywords I can think of are too common and give millions of answers and I can't find the correct one. In a Windows forms application (C# 2008) I have a base form with a datagridview on it, and I want to catch the cell click events on the descendants. If the contents of the datagridview is clicked I have to create a custom event, but if columnhead is clicked I allways want the same event. I hoped I could achieve this by defining the common event in the base class, followed by the custom event in the descendant. I defined a break in the code of the base class when column head is clicked, the break is correctly handled, but the code resumes in the descendant's form class. How can I stop the code execution in the descendant form if I have a break in the base class ? Thanks Rob

    H 1 Reply Last reply
    0
    • R RobScripta

      I fear this question has been asked a thousand times, but the keywords I can think of are too common and give millions of answers and I can't find the correct one. In a Windows forms application (C# 2008) I have a base form with a datagridview on it, and I want to catch the cell click events on the descendants. If the contents of the datagridview is clicked I have to create a custom event, but if columnhead is clicked I allways want the same event. I hoped I could achieve this by defining the common event in the base class, followed by the custom event in the descendant. I defined a break in the code of the base class when column head is clicked, the break is correctly handled, but the code resumes in the descendant's form class. How can I stop the code execution in the descendant form if I have a break in the base class ? Thanks Rob

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      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.

      R D 2 Replies Last reply
      0
      • H Heath Stewart

        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.

        R Offline
        R Offline
        RobScripta
        wrote on last edited by
        #3

        Thanks for your answer, although it is not what I wanted to hear.... Perhaps I was unclear, I'm creating an application with over a dozen forms with a datagridview on it. The datagridview contains buttons, and their action is dependent on the child form. In the cellclickevent I get the rownumber, and when it's -1 the columnheader is clicked. I want all child forms to ignore the click of the columnheader and I wanted to achieve it like: child form: protected override void dgvSelect_CellClick(object sender, DataGridViewCellEventArgs e) { base.dgvSelect(sender, e); custom cell click events; } I will solve it like this: protected override void dgvSelect_CellClick(object sender, DataGridViewCellEventArgs e) { if (base.dgvSelectHeaderClicked(sender, e) == true) { return; } custom cell click events; } Thanks for your time. Rob

        1 Reply Last reply
        0
        • H Heath Stewart

          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.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Holy shit! A rare sighting of the elusive Heath Stewert!! I am honored to be in the presence of the greatness of one of CP's legends. ;)

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          H 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Holy shit! A rare sighting of the elusive Heath Stewert!! I am honored to be in the presence of the greatness of one of CP's legends. ;)

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            Thanks! :-D I might have to make that first one part of my sig.

            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]

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups