loop through comboboxes
-
Hi, Ik have a form containing a number of comboboxes, called cmb1, cmb2, etc. I want to loop through these boxes to set their itemlist. I have tried the following code:
for each (Control^ box in this->Controls){
if (box->GetType() == ComboBox::typeid){
box->Items->Clear();
}
}This works fine, except that I cannot access the combobox's
Items
property. I can for instance access theText
property, so I think it's strange that theItems
property is missing. Does anyone have an idea how to solve this? Thanks! -
Hi, Ik have a form containing a number of comboboxes, called cmb1, cmb2, etc. I want to loop through these boxes to set their itemlist. I have tried the following code:
for each (Control^ box in this->Controls){
if (box->GetType() == ComboBox::typeid){
box->Items->Clear();
}
}This works fine, except that I cannot access the combobox's
Items
property. I can for instance access theText
property, so I think it's strange that theItems
property is missing. Does anyone have an idea how to solve this? Thanks!The reason you can't access the Items property is because you have declared
box
as aControl
andControl
doesn't have anItems
collection. You would have to declare a new variable asComboBox
and castbox
into it:for each (Control^ box in this->Controls){
if (box->GetType() == ComboBox::typeid){
ComboBox^ combo = (ComboBox^)box;
combo->Items->Clear();
}
}modified on Friday, June 4, 2010 11:46 AM
-
The reason you can't access the Items property is because you have declared
box
as aControl
andControl
doesn't have anItems
collection. You would have to declare a new variable asComboBox
and castbox
into it:for each (Control^ box in this->Controls){
if (box->GetType() == ComboBox::typeid){
ComboBox^ combo = (ComboBox^)box;
combo->Items->Clear();
}
}modified on Friday, June 4, 2010 11:46 AM
-
Have you tried this code
for each(ComboBox^ box in this->Controls)
{
if(box->GetType() == ComboBox::typeid)
{
box->Items->Clear();
}
}voo doo12, I've tried this, but when I run the program I get an error that says: "Unable to cast object of type 'System.Windows.Forms.Button' to type 'System.Windows.forms.ComboBox'. So it seems that if the program encounters a control which is not a ComboBox it cannot deal with it in this way.
-
The reason you can't access the Items property is because you have declared
box
as aControl
andControl
doesn't have anItems
collection. You would have to declare a new variable asComboBox
and castbox
into it:for each (Control^ box in this->Controls){
if (box->GetType() == ComboBox::typeid){
ComboBox^ combo = (ComboBox^)box;
combo->Items->Clear();
}
}modified on Friday, June 4, 2010 11:46 AM
Richard, this makes sense. However, when I compile this code I get: error C2440: 'type cast' : cannot convert from 'System::Windows::Forms::Control ^' to 'System::Windows::Forms::ComboBox' Any idea how to solve this?
-
The reason you can't access the Items property is because you have declared
box
as aControl
andControl
doesn't have anItems
collection. You would have to declare a new variable asComboBox
and castbox
into it:for each (Control^ box in this->Controls){
if (box->GetType() == ComboBox::typeid){
ComboBox^ combo = (ComboBox^)box;
combo->Items->Clear();
}
}modified on Friday, June 4, 2010 11:46 AM
Solved! I made a typo in Richard's code (forgot the ^ symbol in the cast part). Now it works, thanks a lot for your help!
-
Solved! I made a typo in Richard's code (forgot the ^ symbol in the cast part). Now it works, thanks a lot for your help!
Glad to be helpful. :)