Getting status of dynamic checkbox (WIndows Mobile SDK)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bjwest
    New Member
    • Dec 2008
    • 12

    Getting status of dynamic checkbox (WIndows Mobile SDK)

    I'm writing a windows mobile app that uses dynamiclly created checkboxes. I'm having trouble getting the checked status.

    Code:
            public void Add_CheckBox(String CBText)
            {
                CheckBox MyCheckbox = new CheckBox();
                MyCheckbox.Location = new System.Drawing.Point(4, CB_Row * 20);
                MyCheckbox.Name = "MycheckBox" + CB_Row;
                MyCheckbox.Text = CBText;
                MyCheckbox.Font = NormalFont;
                MyCheckbox.Size = new System.Drawing.Size(CBText.Length * 12, 20);
                MyCheckbox.TabIndex = CB_Row;
                this.Controls.Add(MyCheckbox);
                MyCheckbox.Click += new EventHandler(MyCheckbox_Click);
                CB_Row = CB_Row + 1;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Add_CheckBox("TextBox " + CB_Row);
            }
    
            private void MyCheckbox_Click(object sender, EventArgs e)
            {
                ((System.Windows.Forms.Control)(sender)).Font = StrikeoutFont;
            }
    When I press the button, the checkboxes are created correctly, and when I check them the font changes to strikeout as it should. However, I need to be able to determine when the checkbox is checked and unchecked.
  • nukefusion
    Recognized Expert New Member
    • Mar 2008
    • 221

    #2
    If you want to be informed when the check state changes you could just hook into the CheckedChanged event.

    If you want to be able to query it at some other time, without changing the way your code is set up at the moment, you'd probably have to iterate the Control collection of the form and look for the checkbox by name (presumably you will know the row the checkbox you want to check is on, so will be able to reconstruct the name MyCheckBox + row).
    You could then cast it to CheckBox and get the value of the Checked property.

    Comment

    • bjwest
      New Member
      • Dec 2008
      • 12

      #3
      I'd like it to act he same as a non dynamic checkbox would:

      Code:
              private void checkBox1_CheckStateChanged(object sender, EventArgs e)
              {
                  if (checkBox1.Checked)
                      checkBox1.Font = StrikeoutFont;
                  else
                      checkBox1.Font = NormalFont;
      The thing is, I can't figure out how to replace the 'checkBox1' with the dynamicly produced checkbox object.

      Sorry, but I may not be stating myself well. I'm this is my first attempt at C#,and I'm still rather new to OO programing in general.

      Comment

      • nukefusion
        Recognized Expert New Member
        • Mar 2008
        • 221

        #4
        Ah, ok. In your AddCheckbox method add an additional line to wire-up the event-handler. When the event-handler is called the sender parameter will be the object that raised the event, which in this case will be the checkbox you are after. Simply cast the object to a checkbox and access the values. Like this:

        Code:
        public void Add_CheckBox(String CBText) 
        { 
            ...
            MyCheckbox.CheckedChanged += new EventHandler(MyCheckbox_CheckedChanged); 
            ... 
        } 
        
        private void checkBox1_CheckedChanged(object sender, EventArgs e) 
        { 
            CheckBox checkBox1 = (CheckBox)sender;
            if (checkBox1.Checked) 
                checkBox1.Font = StrikeoutFont; 
            else 
                checkBox1.Font = NormalFont;
        }

        Comment

        • bjwest
          New Member
          • Dec 2008
          • 12

          #5
          Thank You!

          Thank you nukefusion. The Compact Framework (I'm developing for Windows Mobile) doesn't have the CheckedChanged event. It's called CheckStateChang ed. Why they change the names of things, I don't know. It looks like it does nothing other than making porting over difficult.

          This is how the code looks now, and it works as it should:

          Code:
                  public void Add_CheckBox(String CBText)
                  {
                      CheckBox MyCheckbox = new CheckBox();
                      MyCheckbox.Location = new System.Drawing.Point(4, CB_Row * 20);
                      MyCheckbox.Name = "MycheckBox" + CB_Row;
                      MyCheckbox.Text = CBText;
                      MyCheckbox.Font = NormalFont;
                      MyCheckbox.Size = new System.Drawing.Size(CBText.Length * 12, 20);
                      MyCheckbox.TabIndex = CB_Row;
                      MyCheckbox.CheckStateChanged += new EventHandler(MyCheckbox_CheckStateChanged);
                      this.Controls.Add(MyCheckbox);
                      CB_Row = CB_Row + 1;
                  }
          
                  private void MyCheckbox_CheckStateChanged(object sender, EventArgs e)
                  {
                      CheckBox MyCheckbox = (CheckBox)sender;
                      if (MyCheckbox.Checked)
                      {
                          MyCheckbox.ForeColor = Color.Gray;
                          MyCheckbox.Font = StrikeoutFont;
                      }
                      else
                      {
                          MyCheckbox.ForeColor = Color.Black;
                          MyCheckbox.Font = NormalFont;
                      }
                  }
          I think my main problem these past couple of days trying to get this to work is I wasn't issuing 'CheckBox MyCheckbox = (CheckBox)sende r;' to get the CheckBox object I needed. Like I said before, I'm still rather new to OO programing. All this overloading and inheritance stuff is a bit confusing to an old school C programmer at first.

          I really appreciate your help and the existence of bytes. You'll most likely see me here again until I get this OO stuff down.

          Comment

          • nukefusion
            Recognized Expert New Member
            • Mar 2008
            • 221

            #6
            No problem, glad you got it sorted and it looks like I learnt something too about Compact framework development. ;)

            Comment

            Working...