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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Cancel Click In Inherited User Control

Cancel Click In Inherited User Control

Scheduled Pinned Locked Moved C#
oophelp
10 Posts 3 Posters 0 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
    rich_wenger
    wrote on last edited by
    #1

    Hi, I've got a custom user control using inheritance that I'm working on that I would like to cancel the 'Click' event. Any help would be greatly appreciated.

    C 1 Reply Last reply
    0
    • R rich_wenger

      Hi, I've got a custom user control using inheritance that I'm working on that I would like to cancel the 'Click' event. Any help would be greatly appreciated.

      C Offline
      C Offline
      Curtis Schlak
      wrote on last edited by
      #2

      From the MSDN documentation on the Control.OnClick method:

      The OnClick method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class. Notes to Inheritors: When overriding OnClick in a derived class, be sure to call the base class's OnClick method so that registered delegates receive the event.

      From reading this, I infer that if you override your inherited control's OnClick method and do not call base.OnClick, then any registerd event handlers will not receive the event notification. The code below shows this:

      using System;
      using System.Windows.Forms;

      namespace OnClickOverride
      {
      public class MainForm : Form
      {
      public MainForm()
      {
      Height = 100;
      Width = 200;
      Button b = new Button();
      b.Text = "Normal";
      b.Top = 10;
      b.Left = 10;
      b.Click += new EventHandler( normal_Click );
      Controls.Add( b );
      b = new OverriddenButton();
      b.Text = "Abnormal";
      b.Top = 10;
      b.Left = 100;
      b.Click += new EventHandler( abnormal_Click );
      Controls.Add( b );
      }

      	private void normal\_Click( object sender, EventArgs e )
      	{
      		MessageBox.Show( "Clicked the normal button." );
      	}
      
      	private void abnormal\_Click(object sender, EventArgs e)
      	{
      		MessageBox.Show( "Clicked the abnormal button." );
      	}
      
      	\[STAThread\]
      	public static void Main()
      	{
      		Application.Run( new MainForm() );
      	}
      }
      
      public class OverriddenButton : Button
      {
      	protected override void OnClick(EventArgs e)
      	{
      		// Nothing happens.
      	}
      }
      

      }

      "we must lose precision to make significant
      statements about complex systems."
      -deKorvin on uncertainty

      R 2 Replies Last reply
      0
      • C Curtis Schlak

        From the MSDN documentation on the Control.OnClick method:

        The OnClick method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class. Notes to Inheritors: When overriding OnClick in a derived class, be sure to call the base class's OnClick method so that registered delegates receive the event.

        From reading this, I infer that if you override your inherited control's OnClick method and do not call base.OnClick, then any registerd event handlers will not receive the event notification. The code below shows this:

        using System;
        using System.Windows.Forms;

        namespace OnClickOverride
        {
        public class MainForm : Form
        {
        public MainForm()
        {
        Height = 100;
        Width = 200;
        Button b = new Button();
        b.Text = "Normal";
        b.Top = 10;
        b.Left = 10;
        b.Click += new EventHandler( normal_Click );
        Controls.Add( b );
        b = new OverriddenButton();
        b.Text = "Abnormal";
        b.Top = 10;
        b.Left = 100;
        b.Click += new EventHandler( abnormal_Click );
        Controls.Add( b );
        }

        	private void normal\_Click( object sender, EventArgs e )
        	{
        		MessageBox.Show( "Clicked the normal button." );
        	}
        
        	private void abnormal\_Click(object sender, EventArgs e)
        	{
        		MessageBox.Show( "Clicked the abnormal button." );
        	}
        
        	\[STAThread\]
        	public static void Main()
        	{
        		Application.Run( new MainForm() );
        	}
        }
        
        public class OverriddenButton : Button
        {
        	protected override void OnClick(EventArgs e)
        	{
        		// Nothing happens.
        	}
        }
        

        }

        "we must lose precision to make significant
        statements about complex systems."
        -deKorvin on uncertainty

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

        Thanks for your reply; I'm trying to work it out in context.

        1 Reply Last reply
        0
        • C Curtis Schlak

          From the MSDN documentation on the Control.OnClick method:

          The OnClick method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class. Notes to Inheritors: When overriding OnClick in a derived class, be sure to call the base class's OnClick method so that registered delegates receive the event.

          From reading this, I infer that if you override your inherited control's OnClick method and do not call base.OnClick, then any registerd event handlers will not receive the event notification. The code below shows this:

          using System;
          using System.Windows.Forms;

          namespace OnClickOverride
          {
          public class MainForm : Form
          {
          public MainForm()
          {
          Height = 100;
          Width = 200;
          Button b = new Button();
          b.Text = "Normal";
          b.Top = 10;
          b.Left = 10;
          b.Click += new EventHandler( normal_Click );
          Controls.Add( b );
          b = new OverriddenButton();
          b.Text = "Abnormal";
          b.Top = 10;
          b.Left = 100;
          b.Click += new EventHandler( abnormal_Click );
          Controls.Add( b );
          }

          	private void normal\_Click( object sender, EventArgs e )
          	{
          		MessageBox.Show( "Clicked the normal button." );
          	}
          
          	private void abnormal\_Click(object sender, EventArgs e)
          	{
          		MessageBox.Show( "Clicked the abnormal button." );
          	}
          
          	\[STAThread\]
          	public static void Main()
          	{
          		Application.Run( new MainForm() );
          	}
          }
          
          public class OverriddenButton : Button
          {
          	protected override void OnClick(EventArgs e)
          	{
          		// Nothing happens.
          	}
          }
          

          }

          "we must lose precision to make significant
          statements about complex systems."
          -deKorvin on uncertainty

          R Offline
          R Offline
          rich_wenger
          wrote on last edited by
          #4

          Hi, I'm not certain how to use this in a user control? Someone suggested I use this: protected override void onclick(System.EventArgs e) { //base.onclick (e); } But it returned an error saying "no suitable method found to override". I'm trying to interrupt the event in the user control and not in the parent form.

          C 1 Reply Last reply
          0
          • R rich_wenger

            Hi, I'm not certain how to use this in a user control? Someone suggested I use this: protected override void onclick(System.EventArgs e) { //base.onclick (e); } But it returned an error saying "no suitable method found to override". I'm trying to interrupt the event in the user control and not in the parent form.

            C Offline
            C Offline
            Curtis Schlak
            wrote on last edited by
            #5

            Two things, really. 1) Does the user control inherit from the System.Windows.Forms.Control class? 2) I'm sorry, but the formatter messed up my code. The method has a capital O and capital E so that it reads OnClick.

            "we must lose precision to make significant
            statements about complex systems."
            -deKorvin on uncertainty

            R 2 Replies Last reply
            0
            • C Curtis Schlak

              Two things, really. 1) Does the user control inherit from the System.Windows.Forms.Control class? 2) I'm sorry, but the formatter messed up my code. The method has a capital O and capital E so that it reads OnClick.

              "we must lose precision to make significant
              statements about complex systems."
              -deKorvin on uncertainty

              R Offline
              R Offline
              rich_wenger
              wrote on last edited by
              #6

              using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Windows.Forms; namespace RadioButtonRocLib { public class RadioButtonRoc : System.Windows.Forms.RadioButton { private System.ComponentModel.Container components = null; public RadioButtonRoc() { Code continues.........

              1 Reply Last reply
              0
              • C Curtis Schlak

                Two things, really. 1) Does the user control inherit from the System.Windows.Forms.Control class? 2) I'm sorry, but the formatter messed up my code. The method has a capital O and capital E so that it reads OnClick.

                "we must lose precision to make significant
                statements about complex systems."
                -deKorvin on uncertainty

                R Offline
                R Offline
                rich_wenger
                wrote on last edited by
                #7

                This following builds but I see no effect in my test form: protected override void OnClick(System.EventArgs e) { base.OnClick(e); } -- modified at 16:01 Monday 14th November, 2005

                C L 2 Replies Last reply
                0
                • R rich_wenger

                  This following builds but I see no effect in my test form: protected override void OnClick(System.EventArgs e) { base.OnClick(e); } -- modified at 16:01 Monday 14th November, 2005

                  C Offline
                  C Offline
                  Curtis Schlak
                  wrote on last edited by
                  #8

                  If you don't want the radio button to process the OnClic****k event, remove the base.OnClic****k(e) call from your code.

                  "we must lose precision to make significant
                  statements about complex systems."
                  -deKorvin on uncertainty

                  1 Reply Last reply
                  0
                  • R rich_wenger

                    This following builds but I see no effect in my test form: protected override void OnClick(System.EventArgs e) { base.OnClick(e); } -- modified at 16:01 Monday 14th November, 2005

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    make it like this protected override void OnClick(System.EventArgs e) { //base.OnClick(e); }

                    R 1 Reply Last reply
                    0
                    • L Lost User

                      make it like this protected override void OnClick(System.EventArgs e) { //base.OnClick(e); }

                      R Offline
                      R Offline
                      rich_wenger
                      wrote on last edited by
                      #10

                      Hi Josh, I think the Code Project site has reformatted your code or else it's identical previous suggestions.

                      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