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. Extending a .Net Control. Implement new functionality in event

Extending a .Net Control. Implement new functionality in event

Scheduled Pinned Locked Moved C#
helpcsharp
12 Posts 4 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.
  • S Offline
    S Offline
    sodevrom
    wrote on last edited by
    #1

    Hello, I want to create some custom groupboxes. What I did is created a new class called MyGroupBox that is derived from System.Windows.Forms.GroupBox . Everything is going great but I have a problem. I want to overwrite the default OnEnabledChanged event and add a new feature. What I did is this:

    protected override void OnEnabledChanged(EventArgs e)
    {
    if (this.Enabled)
    {
    this.ColorScheme = EnmColorScheme.Purple;
    }
    else
    {
    this.ColorScheme = EnmColorScheme.Green;
    }
    }

    Now what this will do is change the color of a disaled groupbox. The problem is that if I do this, the default functionality dissapers. When disabling a groupbox, all controls from the groupbox should be disabled. If I override the event, than his does not happen. What can I do to make the event behave like in the past, but also hadd my functionality (to add colors). I don't wan to implement this in every object that I created, that's why I am doing it in the main class "MyGroupBox". Any help is greatly appreciated. Thanks! Vlad

    L D 3 Replies Last reply
    0
    • S sodevrom

      Hello, I want to create some custom groupboxes. What I did is created a new class called MyGroupBox that is derived from System.Windows.Forms.GroupBox . Everything is going great but I have a problem. I want to overwrite the default OnEnabledChanged event and add a new feature. What I did is this:

      protected override void OnEnabledChanged(EventArgs e)
      {
      if (this.Enabled)
      {
      this.ColorScheme = EnmColorScheme.Purple;
      }
      else
      {
      this.ColorScheme = EnmColorScheme.Green;
      }
      }

      Now what this will do is change the color of a disaled groupbox. The problem is that if I do this, the default functionality dissapers. When disabling a groupbox, all controls from the groupbox should be disabled. If I override the event, than his does not happen. What can I do to make the event behave like in the past, but also hadd my functionality (to add colors). I don't wan to implement this in every object that I created, that's why I am doing it in the main class "MyGroupBox". Any help is greatly appreciated. Thanks! Vlad

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

      I haven't looked but if the GroupBox default OnEnabledChanged is to loop through each child control and disable or enable them then that is your problem. Since you are overriding the default method you are not putting that code in there. The code above is just changing the color. You need to add the code to disable/enable all the controls.

      S 1 Reply Last reply
      0
      • L Lost User

        I haven't looked but if the GroupBox default OnEnabledChanged is to loop through each child control and disable or enable them then that is your problem. Since you are overriding the default method you are not putting that code in there. The code above is just changing the color. You need to add the code to disable/enable all the controls.

        S Offline
        S Offline
        sodevrom
        wrote on last edited by
        #3

        Hello, Thanks for the suggestion but I already tried it in the past but it did not work. I used this:

        for (int i = 0; i < this.Controls.Count; i++)
        {
        this.Controls[i].Enabled = this.Enabled;
        }

        The controls are getting activated, but they are not clickable. Can you give it a shot? Vlad

        L 2 Replies Last reply
        0
        • S sodevrom

          Hello, Thanks for the suggestion but I already tried it in the past but it did not work. I used this:

          for (int i = 0; i < this.Controls.Count; i++)
          {
          this.Controls[i].Enabled = this.Enabled;
          }

          The controls are getting activated, but they are not clickable. Can you give it a shot? Vlad

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

          I think you might have to force a refresh after changing it try something like this:

              protected override void OnEnabledChanged(EventArgs e)
              {
                  if (this.Enabled)
                  {
                      // Do whatever here
                      EnableChildren();
                      base.Refresh();
                  }
                  else
                  {
                      // Do whatever here
                      DisableChildren();
                      base.Refresh();
                  }
              }
          
              private void DisableChildren()
              {
                  foreach (Control c in this.Controls)
                  {
                      c.Enabled = false;
                  }
              }
          
              private void EnableChildren()
              {
                  foreach (Control c in this.Controls)
                  {
                      c.Enabled = true;
                  }
              }
          
          1 Reply Last reply
          0
          • S sodevrom

            Hello, Thanks for the suggestion but I already tried it in the past but it did not work. I used this:

            for (int i = 0; i < this.Controls.Count; i++)
            {
            this.Controls[i].Enabled = this.Enabled;
            }

            The controls are getting activated, but they are not clickable. Can you give it a shot? Vlad

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

            I did try your example.. It does do what you want but it doesn't show up because it hasn't repainted the controls. You will notice that once that runs and it disables all child controls that they appear to still be enabled (even though they are not). If you run your mouse over them they will redraw and will now show to be disabled. After your for statement run a this.Refresh(); So when the groupbox gets called to disable/enable each time it will loop through each control and eitehr disable or enable them.

            S 1 Reply Last reply
            0
            • L Lost User

              I did try your example.. It does do what you want but it doesn't show up because it hasn't repainted the controls. You will notice that once that runs and it disables all child controls that they appear to still be enabled (even though they are not). If you run your mouse over them they will redraw and will now show to be disabled. After your for statement run a this.Refresh(); So when the groupbox gets called to disable/enable each time it will loop through each control and eitehr disable or enable them.

              S Offline
              S Offline
              sodevrom
              wrote on last edited by
              #6

              Hello, First, your code could easily be written as:

              foreach(Control c in this.Controls)
              {
              c.Enabled = this.Enabled;
              }
              base.Refresh();

              *this is just a note :) Now the problem is that it still does not work :( I tried it with this.Refresh() also but still no luck...

              L 1 Reply Last reply
              0
              • S sodevrom

                Hello, First, your code could easily be written as:

                foreach(Control c in this.Controls)
                {
                c.Enabled = this.Enabled;
                }
                base.Refresh();

                *this is just a note :) Now the problem is that it still does not work :( I tried it with this.Refresh() also but still no luck...

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

                I know that. Here is what I created to mimic your problem:

                public partial class CustomControl1 : System.Windows.Forms.GroupBox
                {
                public CustomControl1()
                {
                InitializeComponent();
                }

                    protected override void OnPaint(PaintEventArgs pe)
                    {
                        base.OnPaint(pe);
                    }
                
                    protected override void OnEnabledChanged(EventArgs e)
                    {
                        if (this.Enabled)
                        {
                            // Do whatever here
                        }
                        else
                        {
                            // Do whatever here
                        }
                
                        foreach (Control c in this.Controls)
                            c.Enabled = this.Enabled;
                
                        base.Refresh();
                    }
                }
                

                After I created that I created a Windows Form with two group box. One groupbox was my custom one, and the other was an normal group box. Each groupbox had two regular checkboxes in them. I also placed two buttons out to the right (one for Enable and Disable)

                private void button1_Click(object sender, EventArgs e)
                {
                customControl11.Enabled = true;
                groupBox1.Enabled = true;
                }

                    private void button2\_Click(object sender, EventArgs e)
                    {
                        customControl11.Enabled = false;
                        groupBox1.Enabled = false;
                    }
                

                Now on my form I click the Disable button and it disables both group boxes and all FOUR checkboxes. I click Enable and it enables them. This is what your problem was correct?

                S 1 Reply Last reply
                0
                • L Lost User

                  I know that. Here is what I created to mimic your problem:

                  public partial class CustomControl1 : System.Windows.Forms.GroupBox
                  {
                  public CustomControl1()
                  {
                  InitializeComponent();
                  }

                      protected override void OnPaint(PaintEventArgs pe)
                      {
                          base.OnPaint(pe);
                      }
                  
                      protected override void OnEnabledChanged(EventArgs e)
                      {
                          if (this.Enabled)
                          {
                              // Do whatever here
                          }
                          else
                          {
                              // Do whatever here
                          }
                  
                          foreach (Control c in this.Controls)
                              c.Enabled = this.Enabled;
                  
                          base.Refresh();
                      }
                  }
                  

                  After I created that I created a Windows Form with two group box. One groupbox was my custom one, and the other was an normal group box. Each groupbox had two regular checkboxes in them. I also placed two buttons out to the right (one for Enable and Disable)

                  private void button1_Click(object sender, EventArgs e)
                  {
                  customControl11.Enabled = true;
                  groupBox1.Enabled = true;
                  }

                      private void button2\_Click(object sender, EventArgs e)
                      {
                          customControl11.Enabled = false;
                          groupBox1.Enabled = false;
                      }
                  

                  Now on my form I click the Disable button and it disables both group boxes and all FOUR checkboxes. I click Enable and it enables them. This is what your problem was correct?

                  S Offline
                  S Offline
                  sodevrom
                  wrote on last edited by
                  #8

                  Hello, I just wanted to add the reply and I see your message. Anyways, I think I found the best way to do this. What I did is included this after my code (to change the color): base.OnEnabledChanged(e); So I call the "default" method from the main class. The idea came to me when you wrote base ... I don't use the base keyword to much. What do you think? It works 100%, and it's the "fastest" way to implement it. Do you think it's "safe" ? Thanks for the help! Vlad

                  L J 2 Replies Last reply
                  0
                  • S sodevrom

                    Hello, I just wanted to add the reply and I see your message. Anyways, I think I found the best way to do this. What I did is included this after my code (to change the color): base.OnEnabledChanged(e); So I call the "default" method from the main class. The idea came to me when you wrote base ... I don't use the base keyword to much. What do you think? It works 100%, and it's the "fastest" way to implement it. Do you think it's "safe" ? Thanks for the help! Vlad

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

                    Would it be possible for you to post all the code so I can take a look at what you are doing? The ColorScheme is not part of the GroupBox so I figured that is something you created. Calling base.OnEnabledChanged(e) will run the code in the base class which will disable / enable the child controls. That way you can get rid of your foreach statement in your method. I'm still interested in the rest of the code as to why it wasn't working with a Refresh or Invalidate. Doing it that way should be fine and safe. Most likely is the fastest way also I would think

                    1 Reply Last reply
                    0
                    • S sodevrom

                      Hello, I want to create some custom groupboxes. What I did is created a new class called MyGroupBox that is derived from System.Windows.Forms.GroupBox . Everything is going great but I have a problem. I want to overwrite the default OnEnabledChanged event and add a new feature. What I did is this:

                      protected override void OnEnabledChanged(EventArgs e)
                      {
                      if (this.Enabled)
                      {
                      this.ColorScheme = EnmColorScheme.Purple;
                      }
                      else
                      {
                      this.ColorScheme = EnmColorScheme.Green;
                      }
                      }

                      Now what this will do is change the color of a disaled groupbox. The problem is that if I do this, the default functionality dissapers. When disabling a groupbox, all controls from the groupbox should be disabled. If I override the event, than his does not happen. What can I do to make the event behave like in the past, but also hadd my functionality (to add colors). I don't wan to implement this in every object that I created, that's why I am doing it in the main class "MyGroupBox". Any help is greatly appreciated. Thanks! Vlad

                      D Offline
                      D Offline
                      DaveyM69
                      wrote on last edited by
                      #10

                      The solution you reached in the end, calling the base's method is the correct way.

                      protected override void OnEnabledChanged(EventArgs e)
                      {
                      if (this.Enabled)
                      this.ColorScheme = EnmColorScheme.Purple;
                      else
                      this.ColorScheme = EnmColorScheme.Green;
                      base.OnEnabledChanged(e);
                      }

                      Dave

                      If this helped, please vote & accept answer!

                      Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
                      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                      1 Reply Last reply
                      0
                      • S sodevrom

                        Hello, I just wanted to add the reply and I see your message. Anyways, I think I found the best way to do this. What I did is included this after my code (to change the color): base.OnEnabledChanged(e); So I call the "default" method from the main class. The idea came to me when you wrote base ... I don't use the base keyword to much. What do you think? It works 100%, and it's the "fastest" way to implement it. Do you think it's "safe" ? Thanks for the help! Vlad

                        J Offline
                        J Offline
                        Johnny J
                        wrote on last edited by
                        #11

                        You've found the correct way of doing it! Nothing to worry about there...

                        1 Reply Last reply
                        0
                        • S sodevrom

                          Hello, I want to create some custom groupboxes. What I did is created a new class called MyGroupBox that is derived from System.Windows.Forms.GroupBox . Everything is going great but I have a problem. I want to overwrite the default OnEnabledChanged event and add a new feature. What I did is this:

                          protected override void OnEnabledChanged(EventArgs e)
                          {
                          if (this.Enabled)
                          {
                          this.ColorScheme = EnmColorScheme.Purple;
                          }
                          else
                          {
                          this.ColorScheme = EnmColorScheme.Green;
                          }
                          }

                          Now what this will do is change the color of a disaled groupbox. The problem is that if I do this, the default functionality dissapers. When disabling a groupbox, all controls from the groupbox should be disabled. If I override the event, than his does not happen. What can I do to make the event behave like in the past, but also hadd my functionality (to add colors). I don't wan to implement this in every object that I created, that's why I am doing it in the main class "MyGroupBox". Any help is greatly appreciated. Thanks! Vlad

                          D Offline
                          D Offline
                          DaveyM69
                          wrote on last edited by
                          #12

                          Just a quick addition... If your code is really that simple, you can drop the if/else and do a one liner:

                          ColorScheme = Enabled ? EnmColorScheme.Purple : EnmColorScheme.Green;
                          base.OnEnabledChanged(e);

                          Dave

                          If this helped, please vote & accept answer!

                          Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
                          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                          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