shankar_it Posted September 14, 2006 Posted September 14, 2006 (edited) controls I have a for loop which looks into all the controls and finds out if any one is a combo box.here is the code foreach (Control ctrl in ctrls.Controls) { if(ctrl is ComboBox) { } } inside the if loop i wanted to select the first vaule in the collections in all the combo box.i am not sure of how to do it here inside the if loop.Can any one help me in this? Edited September 14, 2006 by shankar_it Quote
Cags Posted September 14, 2006 Posted September 14, 2006 Before I offer any suggestions can I just clarify what you are attempting to do. It sounds like you have several checkboxes inside a container object (such as a form or a panel) and that you wish to set the 'Checked' property of the first one to true. If this is the case then one observation I will make is that the concept of the 'first' item is a difficult one. How you would define the first item would have some kind of impact on how you achieve the end result. For example is the first item the one with the lowest TabIndex, the item furthest to the left of the screen or perhaps the item closest to the top. Alternatively have I completely got the wrong end of the stick? Quote Anybody looking for a graduate programmer (Midlands, England)?
shankar_it Posted September 14, 2006 Author Posted September 14, 2006 i was just trying to select the first item in the Dropdown box.I solved it foreach (Control ctrl in ctrls.Controls) { if(ctrl is ComboBox) { ComboBox theComboBox = (ComboBox)ctrl; if(theComboBox.Items.Count == 1) { theComboBox.SelectedIndex = 0; } } } this code worked for me. Before I offer any suggestions can I just clarify what you are attempting to do. It sounds like you have several checkboxes inside a container object (such as a form or a panel) and that you wish to set the 'Checked' property of the first one to true. If this is the case then one observation I will make is that the concept of the 'first' item is a difficult one. How you would define the first item would have some kind of impact on how you achieve the end result. For example is the first item the one with the lowest TabIndex, the item furthest to the left of the screen or perhaps the item closest to the top. Alternatively have I completely got the wrong end of the stick? Quote
Leaders snarfblam Posted September 14, 2006 Leaders Posted September 14, 2006 Cags, we are looking at combo boxes, not check boxes, and we want to set them all to their first value, not the first one to a specific value. shankar, I don't think that you want to test whether the item count is one, since that won't work on any combos that have more than one item. Maybe something along the lines of foreach (Control ctrl in ctrls.Controls) { ComboBox combo = ctrl as ComboBox; // Save type checking and code by using 'as' operator if(combo != null && cb.Items.Count > 0) { // Count should be > 0 combo.SelectedIndex = 0; } } By the way, the [CS] tags are very handy. Quote [sIGPIC]e[/sIGPIC]
Cags Posted September 15, 2006 Posted September 15, 2006 Cags' date=' we are looking at combo boxes, not check boxes, and we want to set them all to their first value, not the first one to a specific value.[/quote']I realised that after shankar_it's second post. Talk about being way off base. I started a new job a few days ago, which is exhausting my mind. I think I need to get used to that before trying to make many 'helpfull' suggestions. Quote Anybody looking for a graduate programmer (Midlands, England)?
mskeel Posted September 15, 2006 Posted September 15, 2006 I started a new job a few days ago...I realize this is off topic, but...Congratulations on the new job! :D Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.