473,372 Members | 811 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,372 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 3665
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
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.