473,324 Members | 2,002 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,324 software developers and data experts.

checkbox array, access through index

28
Hello,
I have a no. of checkboxes on a c# form application. these are labelled as 1, 2, 3 and so on... I want to programmatically make them checked or unchecked according to their label, e-g;

Expand|Select|Wrap|Line Numbers
  1. if (i==2)
  2. //mark checkbox labelled '2' as checked
I can have a list of checkboxes by filtering myForm.controls[] but don't know how to index them?
Jun 1 '09 #1
21 6316
balame2004
142 100+
try this:

Expand|Select|Wrap|Line Numbers
  1. foreach(Control cnt in this.Controls)
  2. {
  3.  
  4.   if(typeof(cnt).ToString().ToLower().Contails("checkbox) )
  5.  {
  6. if(((CheckBox)cnt).Text.ToString().Equals("2"))
  7. {
  8.     ((CheckBox)cnt).Checked=true;
  9.     break;
  10. }
  11. }
  12. }
Jun 1 '09 #2
tlhintoq
3,525 Expert 2GB
For those that don't know how to put in [code] tags
TIP: When you are writing your question (or reply), there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Jun 1 '09 #3
tlhintoq
3,525 Expert 2GB
May I make a suggestion? As long as you have to make the checkboxes anyway, why not add them to a List<checkbox> while you're making them?
Jun 1 '09 #4
aatif
28
@tlhintoq
well, then how can I assign their position on the form? some algo to programmatically assign them x.y coords according to form size .. :)
Jun 2 '09 #5
aatif
28
@balame2004
I m having error on the following line;

Expand|Select|Wrap|Line Numbers
  1. typeof(cnt).ToString().....
I tried this;

Expand|Select|Wrap|Line Numbers
  1. if (cnt.GetType() == typeof(System.Windows.Forms.CheckBox))
but still no success, it doesn't find any checkbox...
Jun 2 '09 #6
tlhintoq
3,525 Expert 2GB
@aatif
You already placed them on the form. Just add them to a List<>

Expand|Select|Wrap|Line Numbers
  1. List<checkbox> myChkBx = new List<checkbox>;
  2. // then down in the form_load event
  3. myChkBx.Add(checkbox1);
  4. myChkBx.Add(checkbox2);
  5.  
Now you loop through the list.
Jun 2 '09 #7
r035198x
13,262 8TB
When do you want to set the status? When adding them to the form or after some event? If you want to set them while adding the controls then it should be easy to just play around in the loop that creates them (You are creating them in a loop right?).
If this should happen later in response to some action then write a method that takes the Panel that directly contains these checkboxes and use the later approach that you tried above. It should work then.
Jun 2 '09 #8
aatif
28
well, after initializing the checkboxes, I want to add them in the list according to their label, e-g the one labeled "1" should be at first index. To make the label-wise marking efficient ...
There are 128 or more checkboxes on the form.
Jun 2 '09 #9
tlhintoq
3,525 Expert 2GB
just to clarify, are you making the 128 boxes prgrammatically or through designer? Either way: make one, position it, add it to the list for later reference - THEN worry about initialization

If you feel you can't for some reason you might want to share some pertinent code so we can better help
Jun 2 '09 #10
aatif
28
ok, I did it by using nested loops;

Expand|Select|Wrap|Line Numbers
  1. for (int i = 1; i <= 128; i++)
  2. {
  3.     foreach (Control found in this.groupBox1.Controls)
  4.     {
  5.         if (found is CheckBox)
  6.         {
  7.             if (((CheckBox)found).Text.ToString().Equals(i.ToString()))
  8.             {
  9.                 m_CheckBoxList.Add((CheckBox)found);
  10.                 break;
  11.             }
  12.         }
  13.     }
  14. }
then set the conditional status as;

Expand|Select|Wrap|Line Numbers
  1. m_CheckBoxList[x].Checked = true;
Jun 2 '09 #11
r035198x
13,262 8TB
Couldn't you just do away with one of the loops if you stick to the indexed loop and not use the foreach loop? You could then access each control using the array syntax
Expand|Select|Wrap|Line Numbers
  1. this.groupBox1.Controls[i]
This is one of those situations where the foreach is not appropriate.
Jun 2 '09 #12
aatif
28
@r035198x
Sorry, I couldn't get you?
How would I get them indexed according to the label text?
Jun 2 '09 #13
r035198x
13,262 8TB
Now I don't get you. I thought you said you wanted to check them according to label text rather than index them according to label text.

Expand|Select|Wrap|Line Numbers
  1. for (int 0 = 1; i < this.groupBox1.Controls.Length; i++) {
  2.     Control found  = groupBox1.Controls[i];
  3.     if (found is CheckBox)  {
  4.      //get the text label on the checkbox and check it depending on whether
  5.      //the label represents an even number or not.     
  6.     }
  7. }
  8.  
Jun 2 '09 #14
aatif
28
@r035198x
I want to index them according to label text, to later check them according to label (index). I needed indexing because they were not initialized sequentially.
Jun 2 '09 #15
r035198x
13,262 8TB
That's not what you said in your first post. I still don't see what the indexing is for. You should still use the one loop approach. In your nested loops you are looping the controls 128 times. You only need to loop through them once.
Jun 2 '09 #16
aatif
28
I am looping 128 times because I have to check 128 labels. I don't think that one iterator will accurately match both the labels (1 to 128) and the controls.
Jun 2 '09 #17
r035198x
13,262 8TB
You have 128 checkboxes labeled 1 .. 128 (in any order). You can loop through them all using one loop. Your foreach loop is redundant.
Jun 2 '09 #18
aatif
28
ok, could you plz help me with the code? there is an array of 128 boolean values and I want to 'check' the checkboxes having label equal to the 'true' value's index.
Jun 2 '09 #19
r035198x
13,262 8TB
@aatif
You keep changing your requirements. You never mentioned this array of 128 booleans before.
Jun 2 '09 #20
aatif
28
:-| sorry if I forgot to mention exactly, thats why I needed to enlist them for easy acces for marking ...
Jun 2 '09 #21
r035198x
13,262 8TB
Better use a Map<int,boolean> (or do you .Netters call it a Dictionary<int, boolean>).
Jun 2 '09 #22

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

Similar topics

2
by: Steve | last post by:
I'm working on a page for an e-commerce site that has a form than lists items in an order placed by a customer. The form serves two purposes: to edit and save the details of the items (i.e. number...
24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
6
by: Denis C | last post by:
Hi I have a byte array of length 2404. I want to access subsets of the array and pass the subset to the constructor of a class. i.e. Dim bData(2403) As Byte Dim aThing As SomeClass = new...
4
by: Paul Morrison | last post by:
Hi, I have a checkbox array containing the id of a record in a MySQL database. The checkboxes are created dynamically depending on what is stored in the database. I want to send the checkbox...
3
by: JackM | last post by:
Okay, I'm starting to get a little ticked off because I've worked for hours on this and I can't seem to find the cause. I'm using PHP 5.1.6. I'm trying to get the values of some form checkboxes...
0
by: Nolanclark | last post by:
Hi there. I've read a previous thread regarding the Old VB 6 checkbox array and how it's not really needed any more. That's fine, but I'm not really sure how to implement the checkbox control array...
3
realin
by: realin | last post by:
Hiya all, i am in deep trouble, i thought it was an easy task, but now its getting on my nerves. Well i have a div on a form, which contains a number of checkboxes, as <div...
4
by: Joni Seth | last post by:
I have a database of required skills for employees. There is a many to many relationship between the users table and the skills table, called user_skills. It contains the following fields: auto...
2
JnrJnr
by: JnrJnr | last post by:
I'm going to leave out most details so say for instance i have a combobox and when I select a car from the combobox I have a variable called Index that grows with each selection of the combobox. that...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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)...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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.