EFileTahi-A Posted December 4, 2004 Posted December 4, 2004 Am looping all objects inside a panel which I want to get the combobox type ones to perform some special tasks. The problem is, when I get a combobox (identified through its name (starts With "cbx_") I can't access its comboBox special features like ".items.add" :(. So? I will I walk around this one? foreach (System.Windows.Forms.Control cCtrl in cTargetPanel.Controls) { if (cCtrl.StartsWith("cbx_") == true) { cCtrl.Items.Add("bla bla"); } } Although this pice of code does not works, it gives you an clear ideia of what I need to do... tks for the attention Quote
Mothra Posted December 4, 2004 Posted December 4, 2004 Am looping all objects inside a panel which I want to get the combobox type ones to perform some special tasks. The problem is, when I get a combobox (identified through its name (starts With "cbx_") I can't access its comboBox special features like ".items.add" :(. So? I will I walk around this one? foreach (System.Windows.Forms.Control cCtrl in cTargetPanel.Controls) { if (cCtrl.StartsWith("cbx_") == true) { cCtrl.Items.Add("bla bla"); } } Although this pice of code does not works, it gives you an clear ideia of what I need to do... tks for the attention I'm not EXACTLY sure of the C# syntax but, you have to cast the item to a combobox type before you can access combobox members and methods. In VB it's like this... For Each cCtrl As Control In cTargetPanel.Controls If cCtrl = theControlYouAreLookingFor Then Dim myComboBox As ComboBox = DirectCast(cCtrl, ComboBox) 'Now you can use the combobox as you normally would myComboBox.Items.Add("bla bla") End If Next I believe the C# syntax for casting an object to a different type is: (targetType) object In your case something like this: foreach (System.Windows.Forms.Control cCtrl in cTargetPanel.Controls) { if (cCtrl.StartsWith("cbx_") == true) { ComboBox myComboBox = (ComboBox)cCtrl; myComboBox.Items.Add("bla bla"); } } Again, I'm not exactly sure about that syntax but, you defiantly have to cast the cCtrl to a ComboBox type. Quote Being smarter than you look is always better than looking smarter than you are.
EFileTahi-A Posted December 4, 2004 Author Posted December 4, 2004 hi, tks for the reply. I already tryed that before I post here, it does work indeed, I can add items but it seems the will be add to the new created combobox, not the target combobox itself locked by the foreach... :( { foreach (System.Windows.Forms.Control cCtrl in cTargetPanel.Controls) { if (cCtrl.Tag != null) { if (cCtrl.Tag.ToString() == "DIN" && cCtrl.Name.StartsWith("cbx_") == true) { if (cCtrl.Name.ToString().ToUpper().StartsWith("CBX_TELINFO") == true | cCtrl.Name.ToString().ToUpper().StartsWith("CBX_EMP_TELINFO") == true) { sSQL = "SELECT * FROM dinamica WHERE telinfo = true"; } else { sSQL = "SELECT * FROM dinamica WHERE " + cCtrl.Name.ToString().Remove(0,4) + " = true"; } System.Windows.Forms.ComboBox cCtrlCbx = (System.Windows.Forms.ComboBox)cCtrl; OleDbCommand cmd2 = new OleDbCommand(sSQL, conAccess); OleDbDataReader myReader; myReader = cmd2.ExecuteReader(); try { this.cbx_emp_nome.Items.Clear(); while(myReader.Read()) { cCtrlCbx.Items.Add(myReader.GetValue(1).ToString()); } myReader.Close(); } catch(Exception g) { MessageBox.Show(g.Message); myReader.Close(); } } } } well here is my code anyway Quote
Mothra Posted December 5, 2004 Posted December 5, 2004 hi, tks for the reply. I already tryed that before I post here, it does work indeed, I can add items but it seems the will be add to the new created combobox, not the target combobox itself locked by the foreach... :( That is what the DirectCast statement handles in VB. I'm not sure if there is a C# equivilent or not. You might try putting the name of the combobox you're trying to update in as the "target type"... ... (cboYourComboBox)cCtrl; I don't know if it will work or not but, it may be worth a try Quote Being smarter than you look is always better than looking smarter than you are.
EFileTahi-A Posted December 5, 2004 Author Posted December 5, 2004 Ahhhhh! It does works! My mistake, the comboBox.drawMode was set to OwnerDrawFixed which was making the combobox items invisible!!! - THREAD RESOLVED - 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.