EventHandling for multiple controls that are loaded using a button_click event

75 views Asked by At

I have a button in the form. A groupbox is loaded each time the button is clicked (you can refer to my previous question). there are few textboxes and labels in each of the groupbox. I have raised keydown events for the textboxes in the groupbox. The keyDown event does a few mathematical calculations.

Link to my previous question: Loading multiple Groupboxes in the form using the button_click event

It works fine when only one groupbox is loaded. But when the second groupbox is loaded, the event raised doesnot work for the textboxes in the previously loaded groupboxes. It only works for the new groupbox. For example, If the button is clicked once groupbox1 is loaded, if it is clicked the second time groupbox2 is loaded,and so on... The problem is that the keydown event only works for the newly loaded groupbox. (i.e. if the button is clicked the third time then the keydown event works only for groupbox3. For groupbox1 and groupbox2, it does not work) What should be done to retain the events for the controls in the previous groupbox when the new group box is loaded.

This is what I tried: But the problem persists.

int count;
private GroupBox GetGroupBox(int a)
        {
            groupBox = new GroupBox() { Text = "S" + (a.ToString()), Name = "S" + (a.ToString()), Width = 150, Height = 150, Location = new Point(15 + (count - 1) * (150 + 5), 270) };
                        
            for (int i=0; i<5; i++)
            {
                switch (i)
                {
                    case 1: // Add a TextBox and A Label
                        Tb1 = new TextBox() { Width = 40, Height = 10, Text = "Something", Location = new Point(75, 30) };
                        groupBox.Controls.Add(Tb1);
                        Tb1.KeyDown += Tb1_KeyDown;
                        lbl1 = new Label() { Name = "lbl1", Text = "r1.X", Width = 40, Height = 15, Location = new Point(10, 30) };
                        groupBox.Controls.Add(lbl1);
                        break;
                    case 2: // Add a TextBox and A Label
                        Tb2 = new TextBox() { Width = 40, Height = 10, Text = "Something", Location = new Point(75, 50) };
                        groupBox.Controls.Add(Tb2);
                        Tb2.KeyDown += Tb2_KeyDown;
                        lbl2 = new Label() { Name = "lbl2", Text = "r1.Y", Width = 40, Height = 15, Location = new Point(10, 50) };
                        groupBox.Controls.Add(lbl2);
                        break;
                    case 3: // Add a TextBox and A Label
                        Tb3 = new TextBox() { Width = 40, Height = 10, Text = "Something", Location = new Point(75, 70) };
                        groupBox.Controls.Add(Tb3);
                        Tb3.KeyDown += Tb3_KeyDown;
                        lbl3 = new Label() { Name = "lbl3", Text = "r1.Width", Width = 50, Height = 15, Location = new Point(10, 70) };
                        groupBox.Controls.Add(lbl3);
                        break;
                    case 4: // Add a TextBox and A Label
                        Tb4 = new TextBox() { Width = 40, Height = 10, Text = "Something", Location = new Point(75, 90) };
                        groupBox.Controls.Add(Tb4);
                        Tb4.KeyDown += Tb4_KeyDown;
                        lbl4 = new Label() { Name = "lbl4", Text = "r1.Height", Width = 50, Height = 15, Location = new Point(10, 90) };
                        groupBox.Controls.Add(lbl4);
                        break;
                }
            }            
            return groupBox;
        }
private void Tb4_KeyDown(object sender, KeyEventArgs e)
        {
            // / call a method to do something using the Tb.text as argument
        }
private void Tb3_KeyDown(object sender, KeyEventArgs e)
        {
            // / call a method to do something using the Tb.text as argument
        }
private void Tb2_KeyDown(object sender, KeyEventArgs e)
        {
            // / call a method to do something using the Tb.text as argument
        }
private void Tb1_KeyDown(object sender, KeyEventArgs e)
        {
            // call a method to do something using the Tb.text as argument
        }
private void button1_Click(object sender, EventArgs e)
        {
            this.Controls.Add(GetGroupBox(++count));            
        }

Thank you in advance.

1

There are 1 answers

5
Jazz. On

From your description and the code, there are many things missing:

  • Do you have a single form? Why are you creating a new Form1() each time?
  • How do you create those arrays with TextBoxes? Can you add that code? Are you taking the groupbox Childs? My intuition says no.
  • Where do you assign the += in your code? From the behaviour that you mention, I think that you are somehow only able to assign the += to the last available groupbox instance that you keep at hand.

It seems to be that your problem is that you are creating a new group and probably later on assing it to the form. If this is the case, you could call the += inside the GetGroupbox method. An alternative would be to add all the TextBox instances to a list during the GetGroupBox and later on do the +=.

Now, your foreach is terrible. You are iterating on tbx1, what is tbx2 in that context?

I would recommend you to learn to debug (via breakpoints) and evaluate your code behavior. This will step up your coding.