shankar_it

shankar_it

Freshman
Joined
Jul 6, 2005
Messages
46
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?
 
Last edited:
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?
 
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.


Cags said:
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?
 
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
C#:
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.
 
marble_eater said:
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.
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.
 
Back
Top