Connecting Tech Pros Worldwide Help | Site Map

Getting status of dynamic checkbox (WIndows Mobile SDK)

Newbie
 
Join Date: Dec 2008
Posts: 12
#1: Dec 12 '08
I'm writing a windows mobile app that uses dynamiclly created checkboxes. I'm having trouble getting the checked status.

Expand|Select|Wrap|Line Numbers
  1.         public void Add_CheckBox(String CBText)
  2.         {
  3.             CheckBox MyCheckbox = new CheckBox();
  4.             MyCheckbox.Location = new System.Drawing.Point(4, CB_Row * 20);
  5.             MyCheckbox.Name = "MycheckBox" + CB_Row;
  6.             MyCheckbox.Text = CBText;
  7.             MyCheckbox.Font = NormalFont;
  8.             MyCheckbox.Size = new System.Drawing.Size(CBText.Length * 12, 20);
  9.             MyCheckbox.TabIndex = CB_Row;
  10.             this.Controls.Add(MyCheckbox);
  11.             MyCheckbox.Click += new EventHandler(MyCheckbox_Click);
  12.             CB_Row = CB_Row + 1;
  13.         }
  14.  
  15.         private void button1_Click(object sender, EventArgs e)
  16.         {
  17.             Add_CheckBox("TextBox " + CB_Row);
  18.         }
  19.  
  20.         private void MyCheckbox_Click(object sender, EventArgs e)
  21.         {
  22.             ((System.Windows.Forms.Control)(sender)).Font = StrikeoutFont;
  23.         }
  24.  
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's Avatar
Expert
 
Join Date: Mar 2008
Location: Essex, UK
Posts: 197
#2: Dec 12 '08

re: Getting status of dynamic checkbox (WIndows Mobile SDK)


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.
Newbie
 
Join Date: Dec 2008
Posts: 12
#3: Dec 12 '08

re: Getting status of dynamic checkbox (WIndows Mobile SDK)


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

Expand|Select|Wrap|Line Numbers
  1.         private void checkBox1_CheckStateChanged(object sender, EventArgs e)
  2.         {
  3.             if (checkBox1.Checked)
  4.                 checkBox1.Font = StrikeoutFont;
  5.             else
  6.                 checkBox1.Font = NormalFont;
  7.  
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.
nukefusion's Avatar
Expert
 
Join Date: Mar 2008
Location: Essex, UK
Posts: 197
#4: Dec 12 '08

re: Getting status of dynamic checkbox (WIndows Mobile SDK)


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:

Expand|Select|Wrap|Line Numbers
  1. public void Add_CheckBox(String CBText) 
  2.     ...
  3.     MyCheckbox.CheckedChanged += new EventHandler(MyCheckbox_CheckedChanged); 
  4.     ... 
  5.  
  6. private void checkBox1_CheckedChanged(object sender, EventArgs e) 
  7.     CheckBox checkBox1 = (CheckBox)sender;
  8.     if (checkBox1.Checked) 
  9.         checkBox1.Font = StrikeoutFont; 
  10.     else 
  11.         checkBox1.Font = NormalFont;
  12. }
  13.  
Newbie
 
Join Date: Dec 2008
Posts: 12
#5: Dec 12 '08

re: Getting status of dynamic checkbox (WIndows Mobile SDK)


Thank you nukefusion. The Compact Framework (I'm developing for Windows Mobile) doesn't have the CheckedChanged event. It's called CheckStateChanged. 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:

Expand|Select|Wrap|Line Numbers
  1.         public void Add_CheckBox(String CBText)
  2.         {
  3.             CheckBox MyCheckbox = new CheckBox();
  4.             MyCheckbox.Location = new System.Drawing.Point(4, CB_Row * 20);
  5.             MyCheckbox.Name = "MycheckBox" + CB_Row;
  6.             MyCheckbox.Text = CBText;
  7.             MyCheckbox.Font = NormalFont;
  8.             MyCheckbox.Size = new System.Drawing.Size(CBText.Length * 12, 20);
  9.             MyCheckbox.TabIndex = CB_Row;
  10.             MyCheckbox.CheckStateChanged += new EventHandler(MyCheckbox_CheckStateChanged);
  11.             this.Controls.Add(MyCheckbox);
  12.             CB_Row = CB_Row + 1;
  13.         }
  14.  
  15.         private void MyCheckbox_CheckStateChanged(object sender, EventArgs e)
  16.         {
  17.             CheckBox MyCheckbox = (CheckBox)sender;
  18.             if (MyCheckbox.Checked)
  19.             {
  20.                 MyCheckbox.ForeColor = Color.Gray;
  21.                 MyCheckbox.Font = StrikeoutFont;
  22.             }
  23.             else
  24.             {
  25.                 MyCheckbox.ForeColor = Color.Black;
  26.                 MyCheckbox.Font = NormalFont;
  27.             }
  28.         }
  29.  
I think my main problem these past couple of days trying to get this to work is I wasn't issuing 'CheckBox MyCheckbox = (CheckBox)sender;' 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.
nukefusion's Avatar
Expert
 
Join Date: Mar 2008
Location: Essex, UK
Posts: 197
#6: Dec 12 '08

re: Getting status of dynamic checkbox (WIndows Mobile SDK)


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