Why is my control added three times?
-
Hi all, In my windows form I have many group-boxes with text-boxes. I am changing the back color of the text-boxes whenever a text-box has or looses focus. This works OK. I also want to add a help button in the group-box when a text-box in this group-box has focus, but my help button is added three times? This is the code I use:
private void control_Settings()
{
Button buttonHelp = new Button();
buttonHelp.BackColor = System.Drawing.Color.Transparent;
buttonHelp.Image = Properties.Resources.HelpButtonImage;
buttonHelp.Dock = DockStyle.Right; // only for testing.
buttonHelp.Name = "buttonHelp";
buttonHelp.Size = new System.Drawing.Size(25, 25);
buttonHelp.UseVisualStyleBackColor = false;
buttonHelp.Click += new System.EventHandler(this.buttonHelp_Click);foreach (Control gb in Controls) { if (gb is GroupBox) { GroupBox grpbx = (GroupBox)gb; foreach (Control t in grpbx.Controls) { TextBox textBox = t as TextBox; if (textBox != null) { textBox.Enter += delegate(object sender, EventArgs eventArgs) { ((TextBox)sender).BackColor = Color.Gold; Control pToAd = ((TextBox)sender).Parent; if(!pToAd.Controls.Contains(buttonHelp))pToAd.Controls.Add(buttonHelp); }; textBox.Leave += delegate(object sender, EventArgs eventArgs) { Control pToRemove = ((TextBox)sender).Parent; ((TextBox)sender).BackColor = Color.White; pToRemove.Controls.Remove(buttonHelp); }; } } } } }
Thanks in advance, Groover
0200 A9 23 0202 8D 01 80 0205 00
-
Hi all, In my windows form I have many group-boxes with text-boxes. I am changing the back color of the text-boxes whenever a text-box has or looses focus. This works OK. I also want to add a help button in the group-box when a text-box in this group-box has focus, but my help button is added three times? This is the code I use:
private void control_Settings()
{
Button buttonHelp = new Button();
buttonHelp.BackColor = System.Drawing.Color.Transparent;
buttonHelp.Image = Properties.Resources.HelpButtonImage;
buttonHelp.Dock = DockStyle.Right; // only for testing.
buttonHelp.Name = "buttonHelp";
buttonHelp.Size = new System.Drawing.Size(25, 25);
buttonHelp.UseVisualStyleBackColor = false;
buttonHelp.Click += new System.EventHandler(this.buttonHelp_Click);foreach (Control gb in Controls) { if (gb is GroupBox) { GroupBox grpbx = (GroupBox)gb; foreach (Control t in grpbx.Controls) { TextBox textBox = t as TextBox; if (textBox != null) { textBox.Enter += delegate(object sender, EventArgs eventArgs) { ((TextBox)sender).BackColor = Color.Gold; Control pToAd = ((TextBox)sender).Parent; if(!pToAd.Controls.Contains(buttonHelp))pToAd.Controls.Add(buttonHelp); }; textBox.Leave += delegate(object sender, EventArgs eventArgs) { Control pToRemove = ((TextBox)sender).Parent; ((TextBox)sender).BackColor = Color.White; pToRemove.Controls.Remove(buttonHelp); }; } } } } }
Thanks in advance, Groover
0200 A9 23 0202 8D 01 80 0205 00
You must have something strange going on, I just copied your code into a blank winforms project and it worked perfectly. (I changed some of the naming so I could understand the flow better)
using System;
using System.Drawing;
using System.Windows.Forms;public partial class FormMain : Form
{
public Form1()
{
// Added two GroupBoxes both containing two Textboxes in designer
InitializeComponent();
ControlSettings();
}
private void ControlSettings()
{
Button buttonHelp = new Button();
buttonHelp.BackColor = System.Drawing.Color.Transparent;
buttonHelp.Dock = DockStyle.Right; // only for testing.
buttonHelp.Name = "buttonHelp";
buttonHelp.Size = new System.Drawing.Size(25, 25);
buttonHelp.UseVisualStyleBackColor = false;foreach (Control control in Controls) { if (control is GroupBox) { GroupBox groupBox = (GroupBox)control; foreach (Control nestedControl in groupBox.Controls) { TextBox textBox = nestedControl as TextBox; if (textBox != null) { textBox.Enter += delegate(object sender, EventArgs eventArgs) { ((TextBox)sender).BackColor = Color.Gold; Control parent = ((TextBox)sender).Parent; parent.Controls.Add(buttonHelp); }; textBox.Leave += delegate(object sender, EventArgs eventArgs) { Control parent = ((TextBox)sender).Parent; ((TextBox)sender).BackColor = Color.White; parent.Controls.Remove(buttonHelp); }; } } } } }
}
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Hi all, In my windows form I have many group-boxes with text-boxes. I am changing the back color of the text-boxes whenever a text-box has or looses focus. This works OK. I also want to add a help button in the group-box when a text-box in this group-box has focus, but my help button is added three times? This is the code I use:
private void control_Settings()
{
Button buttonHelp = new Button();
buttonHelp.BackColor = System.Drawing.Color.Transparent;
buttonHelp.Image = Properties.Resources.HelpButtonImage;
buttonHelp.Dock = DockStyle.Right; // only for testing.
buttonHelp.Name = "buttonHelp";
buttonHelp.Size = new System.Drawing.Size(25, 25);
buttonHelp.UseVisualStyleBackColor = false;
buttonHelp.Click += new System.EventHandler(this.buttonHelp_Click);foreach (Control gb in Controls) { if (gb is GroupBox) { GroupBox grpbx = (GroupBox)gb; foreach (Control t in grpbx.Controls) { TextBox textBox = t as TextBox; if (textBox != null) { textBox.Enter += delegate(object sender, EventArgs eventArgs) { ((TextBox)sender).BackColor = Color.Gold; Control pToAd = ((TextBox)sender).Parent; if(!pToAd.Controls.Contains(buttonHelp))pToAd.Controls.Add(buttonHelp); }; textBox.Leave += delegate(object sender, EventArgs eventArgs) { Control pToRemove = ((TextBox)sender).Parent; ((TextBox)sender).BackColor = Color.White; pToRemove.Controls.Remove(buttonHelp); }; } } } } }
Thanks in advance, Groover
0200 A9 23 0202 8D 01 80 0205 00
-
You must have something strange going on, I just copied your code into a blank winforms project and it worked perfectly. (I changed some of the naming so I could understand the flow better)
using System;
using System.Drawing;
using System.Windows.Forms;public partial class FormMain : Form
{
public Form1()
{
// Added two GroupBoxes both containing two Textboxes in designer
InitializeComponent();
ControlSettings();
}
private void ControlSettings()
{
Button buttonHelp = new Button();
buttonHelp.BackColor = System.Drawing.Color.Transparent;
buttonHelp.Dock = DockStyle.Right; // only for testing.
buttonHelp.Name = "buttonHelp";
buttonHelp.Size = new System.Drawing.Size(25, 25);
buttonHelp.UseVisualStyleBackColor = false;foreach (Control control in Controls) { if (control is GroupBox) { GroupBox groupBox = (GroupBox)control; foreach (Control nestedControl in groupBox.Controls) { TextBox textBox = nestedControl as TextBox; if (textBox != null) { textBox.Enter += delegate(object sender, EventArgs eventArgs) { ((TextBox)sender).BackColor = Color.Gold; Control parent = ((TextBox)sender).Parent; parent.Controls.Add(buttonHelp); }; textBox.Leave += delegate(object sender, EventArgs eventArgs) { Control parent = ((TextBox)sender).Parent; ((TextBox)sender).BackColor = Color.White; parent.Controls.Remove(buttonHelp); }; } } } } }
}
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Thank You Dave for Your effort, Knowing there is nothing wrong with my code, I found there is something strange.
ControlSettings()
was also called in:
private void Form1_Resize(object sender, System.EventArgs e)
{
// some code to handle resizing
control_Settings();
this.Invalidate();
}to do some other settings. If I remove these other settings it is still not working, If I remove the form resizing it is working OK?? I do not do any resizing so why is the form resize event breaking my control_Settings??
0200 A9 23 0202 8D 01 80 0205 00
-
Thank You Dave for Your effort, Knowing there is nothing wrong with my code, I found there is something strange.
ControlSettings()
was also called in:
private void Form1_Resize(object sender, System.EventArgs e)
{
// some code to handle resizing
control_Settings();
this.Invalidate();
}to do some other settings. If I remove these other settings it is still not working, If I remove the form resizing it is working OK?? I do not do any resizing so why is the form resize event breaking my control_Settings??
0200 A9 23 0202 8D 01 80 0205 00
Then remove either
control_Settings();
from the resize handler, or stop handling the event if you don't need it. I added the call only to the constructor as this is the type of code that would normally only be run once.Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
I agree with Dave that the code presented works for placing and removing a button. Of course as soon as you try to click on it, the textbox loses focus and the button disappears! Alan.
:-D Yeah, I noticed that too - but if that's what the OP wants...
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
I agree with Dave that the code presented works for placing and removing a button. Of course as soon as you try to click on it, the textbox loses focus and the button disappears! Alan.
Hi Alan, You are absolutely right, but I had to get it working first. I moved the
buttonHelp.Click += new System.EventHandler(buttonHelp_Click);
to the textBox.enter delegate and modified the textBox.leave to:
textBox.Leave += delegate(object sender, EventArgs eventArgs)
{Control pToRemove = ((TextBox)sender).Parent; ((TextBox)sender).BackColor = Color.White; if (!pToRemove.ContainsFocus) { pToRemove.Controls.Remove(buttonHelp); buttonHelp.Click -= new System.EventHandler(this.buttonHelp\_Click); } };
Return focus to the control is handled in the buttonHelp.Click event handler. But there is still the question why placing a call to Control_Settings in the Form.resize event, that is never fired, makes the helpbutton control appear three times??
0200 A9 23 0202 8D 01 80 0205 00
-
Hi all, In my windows form I have many group-boxes with text-boxes. I am changing the back color of the text-boxes whenever a text-box has or looses focus. This works OK. I also want to add a help button in the group-box when a text-box in this group-box has focus, but my help button is added three times? This is the code I use:
private void control_Settings()
{
Button buttonHelp = new Button();
buttonHelp.BackColor = System.Drawing.Color.Transparent;
buttonHelp.Image = Properties.Resources.HelpButtonImage;
buttonHelp.Dock = DockStyle.Right; // only for testing.
buttonHelp.Name = "buttonHelp";
buttonHelp.Size = new System.Drawing.Size(25, 25);
buttonHelp.UseVisualStyleBackColor = false;
buttonHelp.Click += new System.EventHandler(this.buttonHelp_Click);foreach (Control gb in Controls) { if (gb is GroupBox) { GroupBox grpbx = (GroupBox)gb; foreach (Control t in grpbx.Controls) { TextBox textBox = t as TextBox; if (textBox != null) { textBox.Enter += delegate(object sender, EventArgs eventArgs) { ((TextBox)sender).BackColor = Color.Gold; Control pToAd = ((TextBox)sender).Parent; if(!pToAd.Controls.Contains(buttonHelp))pToAd.Controls.Add(buttonHelp); }; textBox.Leave += delegate(object sender, EventArgs eventArgs) { Control pToRemove = ((TextBox)sender).Parent; ((TextBox)sender).BackColor = Color.White; pToRemove.Controls.Remove(buttonHelp); }; } } } } }
Thanks in advance, Groover
0200 A9 23 0202 8D 01 80 0205 00
Just an alternative way of thought. I would have added the help buttons and made them invisible. When the textbox receives focus I would make them visible, etc... I think it would be easier to develop, easier to maintain and faster at runtime. hope this helps.
V.
(MQOTD Rules )