473,320 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 3662
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

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

Similar topics

1
by: Sharon_a | last post by:
Hello, I downloaded the Windows Mobile 5.0 Pocket PC SDK. When I try to install it I get an error : "The cabinet file 'sdkfiles.cab' required for this installation is corrupt and cannot be used....
3
by: renil | last post by:
I have a repeater control that displays info. from a datatable. Each row in the repeater has a checkbox. Also, I have a delete linkbutton outside the repeater control. What I'm trying to do when...
2
by: =?Utf-8?B?TWFyYw==?= | last post by:
In Visual Studio 2005, I am developing a Windows Mobile application, using Mobile SQL 2005. I need Data from a Database to be shown in a DataGrid, this works. But now I want to be able to get the...
3
by: =?Utf-8?B?S2ltYXJrTmF0?= | last post by:
What do i need to download to add windows ce 6.0 templates in visual studio 2005? I have been spending the last couple of days trying to figure out how to add the capability to create an...
2
by: tomtomtom | last post by:
Hello together. Thanks for giving me the opportunity to talk with software development experts. I'd like to develop an application to run on a PDA involving GPS. In this context I've got a...
4
by: ink | last post by:
Hi All, I am trying to build an image capture user control using DirectShow to be used in an existing C# application. It needs to have a view finder/ preview window and be able to take...
7
Niheel
by: Niheel | last post by:
Apple just threw a massive blow to the world of Windows Mobile or any Mobile Hardware/OS company with the launch of iPhone2.0 and the developer SDK. Apple and Jobs have done what Gates did to the...
4
by: =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post by:
I am creating an application for windows mobile using Visual C# windows. I am using visual studio 2008 and I have windows Microsoft Vista on my computer. I also downloaded the professional and...
1
by: =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post by:
Could someone please let me know the C# codes to check the battery status of Windows mobile 5.0. The application should check the PC device battery level. I can use BatteryStatus however it runs...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.