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. Why is my control added three times?

Why is my control added three times?

Scheduled Pinned Locked Moved C#
graphicstestingbeta-testinghelpquestion
8 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.
  • G Offline
    G Offline
    GrooverFromHolland
    wrote on last edited by
    #1

    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

    D A V 3 Replies Last reply
    0
    • G GrooverFromHolland

      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

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

      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)

      G 1 Reply Last reply
      0
      • G GrooverFromHolland

        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

        A Offline
        A Offline
        Alan N
        wrote on last edited by
        #3

        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 G 2 Replies Last reply
        0
        • D DaveyM69

          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)

          G Offline
          G Offline
          GrooverFromHolland
          wrote on last edited by
          #4

          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

          D 1 Reply Last reply
          0
          • G GrooverFromHolland

            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

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

            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)

            1 Reply Last reply
            0
            • A Alan N

              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 Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              :-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)

              1 Reply Last reply
              0
              • A Alan N

                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.

                G Offline
                G Offline
                GrooverFromHolland
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • G GrooverFromHolland

                  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

                  V Offline
                  V Offline
                  V 0
                  wrote on last edited by
                  #8

                  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 )

                  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