469,265 Members | 1,991 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,265 developers. It's quick & easy.

Getting status of dynamic checkbox (WIndows Mobile SDK)

12
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.
Dec 12 '08 #1
5 3413
nukefusion
221 Expert 100+
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.
Dec 12 '08 #2
bjwest
12
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.
Dec 12 '08 #3
nukefusion
221 Expert 100+
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.  
Dec 12 '08 #4
bjwest
12
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.
Dec 12 '08 #5
nukefusion
221 Expert 100+
No problem, glad you got it sorted and it looks like I learnt something too about Compact framework development. ;)
Dec 12 '08 #6

Post your reply

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

Similar topics

1 post views Thread by Sharon_a | last post: by
2 posts views Thread by =?Utf-8?B?TWFyYw==?= | last post: by
3 posts views Thread by =?Utf-8?B?S2ltYXJrTmF0?= | last post: by
4 posts views Thread by =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post: by
1 post views Thread by =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.