Checkbox EventHandler

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • darkminos
    New Member
    • Mar 2009
    • 11

    Checkbox EventHandler

    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.



    Code:
    SimpleButton submitButton = new SimpleButton();
                        submitButton.Location = new System.Drawing.Point(292, (29 + countMachines * 21) + 181);
                        submitButton.Name = "submitButton";
                        submitButton.Size = new System.Drawing.Size(78, 23);
                        submitButton.TabIndex = 0;
                        submitButton.Text = "Submit";
                        machineGroupControl.Controls.Add(submitButton);
                        submitButton.Click += new System.EventHandler(submitButton_click);
    
    
    
    CheckBox[] dynamicMachineCheckbox = new CheckBox[countMachines];
    
                        for (int x = 0; x < countMachines; x++)
                        {
                            for (countLines = 0; countLines < this.maintenanceDataSet.Equipment.Count; countLines++)
                            {
                                foreach (DataRow machine in adapter.GetMachinesBy(lineArray[i].ToString()))
                                {
                                    string[] machineManufacturerArray = new string[countMachines];
                                    string[] machineTypeArray = new string[countMachines];
                                    string[] equipmentNumberArray = new string[countMachines];
    
                                    machineManufacturerArray[x] = Convert.ToString(machine.Table.Rows[x][0]);
                                    machineTypeArray[x] = Convert.ToString(machine.Table.Rows[x][1]);
                                    equipmentNumberArray[x] = Convert.ToString(machine.Table.Rows[x][2]);
    
    
    
    
                                    dynamicMachineCheckbox[x] = new CheckBox();
                                    dynamicMachineCheckbox[x].Location = new System.Drawing.Point((20), (29 + x * 21));
                                    dynamicMachineCheckbox[x].Name = "checkbox" + x;
                                    dynamicMachineCheckbox[x].Width = 250;
                                    dynamicMachineCheckbox[x].Text = machineManufacturerArray[x] + " " + machineTypeArray[x] + " " + "(" + equipmentNumberArray[x] + ")";
                                    this.xtraTabControl2.TabPages[countLines + 1].Controls.Add(dynamicMachineCheckbox[x]);
                                    machineGroupControl.Controls.Add dynamicMachineCheckbox[x]);
    
                                    //Event sending checkbox data to submitButton_click()
    
    
                                }
                            }
                        }
                    
    
    
    
    
    
    
            private void submitButton_click(object sender, EventArgs e)
            {
                //In here i need every checkboxes checkstate and text (i know the code below isn't right)
    
                //if (dynamicMachineCheckbox[x].Checked)
                //{ machineCheckboxArray[x] = dynamicMachineCheckbox[x].Text; }
                //else { machineCheckboxArray[x] = ""; }
            }
    Last edited by pbmods; Mar 8 '09, 05:52 PM. Reason: Added CODE tags.
  • vekipeki
    Recognized Expert New Member
    • Nov 2007
    • 229

    #2
    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_cl ick handler, the same way you created them, using a for loop.

    Comment

    • darkminos
      New Member
      • Mar 2009
      • 11

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

      Code:
              private void barbuttonSMT_ItemClick(object sender, ItemClickEventArgs e)
              {
                  createControls(sender, e);
              }
      
      
      public void createControls(object sender, EventArgs e)
              {
      
                       .../
      
                          SimpleButton submitButton = new SimpleButton();
                          submitButton.Location = new System.Drawing.Point(292, (29 + countMachines * 21) + 181);
                          submitButton.Name = "submitButton";
                          submitButton.Size = new System.Drawing.Size(78, 23);
                          submitButton.TabIndex = 0;
                          submitButton.Text = "Submit";
                          machineGroupControl.Controls.Add(submitButton);
                          submitButton.Click += new System.EventHandler(submitButton_click);
      
      
                          for (int x = 0; x < countMachines; x++)
                          {
                              for (countLines = 0; countLines < adapter.GetDystinctLinesBy(bt.PressedLink.Item.Name).Count; countLines++)
                              {
                                  foreach (DataRow machine in adapter.GetMachinesBy(lineArray[i].ToString()))
                                  {
                                      string[] machineManufacturerArray = new string[countMachines];
                                      string[] machineTypeArray = new string[countMachines];
                                      string[] equipmentNumberArray = new string[countMachines];
      
                                      machineTypeArray[x] = Convert.ToString(machine.Table.Rows[x][1]);
                                      equipmentNumberArray[x] = Convert.ToString(machine.Table.Rows[x][2]);
      
      
      
                                      CheckBox dynamicMachineCheckbox = new CheckBox();
                                      dynamicMachineCheckbox = new CheckBox();
                                      dynamicMachineCheckbox.Location = new System.Drawing.Point((20), (29 + x * 21));
                                      dynamicMachineCheckbox.Name = "checkbox" + x;
                                      dynamicMachineCheckbox.Width = 250;
                                      dynamicMachineCheckbox.Text = machineTypeArray[x] + " " + "(" + equipmentNumberArray[x] + ")";
                                      this.xtraTabControl2.TabPages[countLines].Controls.Add(dynamicMachineCheckbox);
                                      machineGroupControl.Controls.Add(dynamicMachineCheckbox);
      
      
                                      submitButton_click(dynamicMachineCheckbox, e);  
      //THIS SHOULD BE SOME SORT OF EVENTHANDLER
       AS IN THIS FORM IT IS BEING READ AFTER
       barbuttonSMT IS CLICKED AND I ONLY WANT
       THIS TO HAPPEN AFTER submitButton IS
       CLICKED
                                      
      
      
                                  }
                              }
                          }
                      }
      
      
      
      
              private void submitButton_click(object sender, EventArgs e)
              {
                  CheckBox button = (CheckBox)sender;
                  System.Windows.Forms.MessageBox.Show(button.Text.ToString());
      
              }

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        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.
        Code:
        List<Checkbox> MyCheckboxList = new List<Checkbox>();
        then right after you make the checkbox
        Code:
        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.

        Code:
        foreach (Control c in MyForm.Controls)
        {
           // do your thing here if it is a checkbox
        }
        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.

        Comment

        • darkminos
          New Member
          • Mar 2009
          • 11

          #5
          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)

          Comment

          Working...