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