By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,336 Members | 2,307 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,336 IT Pros & Developers. It's quick & easy.

Checkbox EventHandler

P: 11
Hi,

I am creating dynamic checkboxes which should have an eventhandler attached to the submitButton. What I mean is while I’m changing state (check / uncheck) nothing should be happening but when I press the submitButton it should read all the checkboxes state and text.

Thx.



Expand|Select|Wrap|Line Numbers
  1. SimpleButton submitButton = new SimpleButton();
  2.                     submitButton.Location = new System.Drawing.Point(292, (29 + countMachines * 21) + 181);
  3.                     submitButton.Name = "submitButton";
  4.                     submitButton.Size = new System.Drawing.Size(78, 23);
  5.                     submitButton.TabIndex = 0;
  6.                     submitButton.Text = "Submit";
  7.                     machineGroupControl.Controls.Add(submitButton);
  8.                     submitButton.Click += new System.EventHandler(submitButton_click);
  9.  
  10.  
  11.  
  12. CheckBox[] dynamicMachineCheckbox = new CheckBox[countMachines];
  13.  
  14.                     for (int x = 0; x < countMachines; x++)
  15.                     {
  16.                         for (countLines = 0; countLines < this.maintenanceDataSet.Equipment.Count; countLines++)
  17.                         {
  18.                             foreach (DataRow machine in adapter.GetMachinesBy(lineArray[i].ToString()))
  19.                             {
  20.                                 string[] machineManufacturerArray = new string[countMachines];
  21.                                 string[] machineTypeArray = new string[countMachines];
  22.                                 string[] equipmentNumberArray = new string[countMachines];
  23.  
  24.                                 machineManufacturerArray[x] = Convert.ToString(machine.Table.Rows[x][0]);
  25.                                 machineTypeArray[x] = Convert.ToString(machine.Table.Rows[x][1]);
  26.                                 equipmentNumberArray[x] = Convert.ToString(machine.Table.Rows[x][2]);
  27.  
  28.  
  29.  
  30.  
  31.                                 dynamicMachineCheckbox[x] = new CheckBox();
  32.                                 dynamicMachineCheckbox[x].Location = new System.Drawing.Point((20), (29 + x * 21));
  33.                                 dynamicMachineCheckbox[x].Name = "checkbox" + x;
  34.                                 dynamicMachineCheckbox[x].Width = 250;
  35.                                 dynamicMachineCheckbox[x].Text = machineManufacturerArray[x] + " " + machineTypeArray[x] + " " + "(" + equipmentNumberArray[x] + ")";
  36.                                 this.xtraTabControl2.TabPages[countLines + 1].Controls.Add(dynamicMachineCheckbox[x]);
  37.                                 machineGroupControl.Controls.Add dynamicMachineCheckbox[x]);
  38.  
  39.                                 //Event sending checkbox data to submitButton_click()
  40.  
  41.  
  42.                             }
  43.                         }
  44.                     }
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.         private void submitButton_click(object sender, EventArgs e)
  53.         {
  54.             //In here i need every checkboxes checkstate and text (i know the code below isn't right)
  55.  
  56.             //if (dynamicMachineCheckbox[x].Checked)
  57.             //{ machineCheckboxArray[x] = dynamicMachineCheckbox[x].Text; }
  58.             //else { machineCheckboxArray[x] = ""; }
  59.         }
Mar 8 '09 #1
Share this Question
Share on Google+
4 Replies


vekipeki
Expert 100+
P: 229
dynamic checkboxes which should have an eventhandler attached to the submitButton
No, checkboxes will not have an event handler, only the button click event will be handled. Until the button is pressed, you don't need to care about what is going on with the checkboxes.

Just read them all in submitButton_click handler, the same way you created them, using a for loop.
Mar 9 '09 #2

P: 11
The problem is that those controls are created on button click from a different control. see line 47-51.

Expand|Select|Wrap|Line Numbers
  1.         private void barbuttonSMT_ItemClick(object sender, ItemClickEventArgs e)
  2.         {
  3.             createControls(sender, e);
  4.         }
  5.  
  6.  
  7. public void createControls(object sender, EventArgs e)
  8.         {
  9.  
  10.                  .../
  11.  
  12.                     SimpleButton submitButton = new SimpleButton();
  13.                     submitButton.Location = new System.Drawing.Point(292, (29 + countMachines * 21) + 181);
  14.                     submitButton.Name = "submitButton";
  15.                     submitButton.Size = new System.Drawing.Size(78, 23);
  16.                     submitButton.TabIndex = 0;
  17.                     submitButton.Text = "Submit";
  18.                     machineGroupControl.Controls.Add(submitButton);
  19.                     submitButton.Click += new System.EventHandler(submitButton_click);
  20.  
  21.  
  22.                     for (int x = 0; x < countMachines; x++)
  23.                     {
  24.                         for (countLines = 0; countLines < adapter.GetDystinctLinesBy(bt.PressedLink.Item.Name).Count; countLines++)
  25.                         {
  26.                             foreach (DataRow machine in adapter.GetMachinesBy(lineArray[i].ToString()))
  27.                             {
  28.                                 string[] machineManufacturerArray = new string[countMachines];
  29.                                 string[] machineTypeArray = new string[countMachines];
  30.                                 string[] equipmentNumberArray = new string[countMachines];
  31.  
  32.                                 machineTypeArray[x] = Convert.ToString(machine.Table.Rows[x][1]);
  33.                                 equipmentNumberArray[x] = Convert.ToString(machine.Table.Rows[x][2]);
  34.  
  35.  
  36.  
  37.                                 CheckBox dynamicMachineCheckbox = new CheckBox();
  38.                                 dynamicMachineCheckbox = new CheckBox();
  39.                                 dynamicMachineCheckbox.Location = new System.Drawing.Point((20), (29 + x * 21));
  40.                                 dynamicMachineCheckbox.Name = "checkbox" + x;
  41.                                 dynamicMachineCheckbox.Width = 250;
  42.                                 dynamicMachineCheckbox.Text = machineTypeArray[x] + " " + "(" + equipmentNumberArray[x] + ")";
  43.                                 this.xtraTabControl2.TabPages[countLines].Controls.Add(dynamicMachineCheckbox);
  44.                                 machineGroupControl.Controls.Add(dynamicMachineCheckbox);
  45.  
  46.  
  47.                                 submitButton_click(dynamicMachineCheckbox, e);  
  48. //THIS SHOULD BE SOME SORT OF EVENTHANDLER
  49.  AS IN THIS FORM IT IS BEING READ AFTER
  50.  barbuttonSMT IS CLICKED AND I ONLY WANT
  51.  THIS TO HAPPEN AFTER submitButton IS
  52.  CLICKED
  53.  
  54.  
  55.  
  56.                             }
  57.                         }
  58.                     }
  59.                 }
  60.  
  61.  
  62.  
  63.  
  64.         private void submitButton_click(object sender, EventArgs e)
  65.         {
  66.             CheckBox button = (CheckBox)sender;
  67.             System.Windows.Forms.MessageBox.Show(button.Text.ToString());
  68.  
  69.         }
  70.  
  71.  
Mar 12 '09 #3

tlhintoq
Expert 2.5K+
P: 3,525
I think I see your confusion. Line 37 where you make a new checkbox, you then don't know how to refer to that check box later in other methods. Right? In addition to making it, you could add it to a list of checkboxes that you later loop through.
Expand|Select|Wrap|Line Numbers
  1. List<Checkbox> MyCheckboxList = new List<Checkbox>();
then right after you make the checkbox
Expand|Select|Wrap|Line Numbers
  1. MyCheckboxList.Add(dynamicMachineCheckbox);
Then in your button_submit method loop through all the elements of the list checking their status.

- Or plan 'B' -
You could also not worry about keeping a list and just loop through all the controls on your form looking for check boxes.

Expand|Select|Wrap|Line Numbers
  1. foreach (Control c in MyForm.Controls)
  2. {
  3.    // do your thing here if it is a checkbox
  4. }
But I like the List<> better because you can keep different lists for different areas on the form as you make them. That way you can group the checkboxes if you have need etc.
Mar 12 '09 #4

P: 11
Many Thanks!
That's exactly what I was searching for! And it helped me to find an error as well, which was causing the software to run slow. (i'm using the List function)
Mar 12 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.