C# foreach{} question

EFileTahi-A

Contributor
Joined
Aug 8, 2004
Messages
623
Location
Portugal / Barreiro
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?

Code:
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
 
EFileTahi-A said:
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?

Code:
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...

Visual Basic:
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:
Code:
(targetType) object
In your case something like this:
Code:
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.
 
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... :(
Code:
{
				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
 
EFileTahi-A said:
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"...

Code:
     ... (cboYourComboBox)cCtrl;
I don't know if it will work or not but, it may be worth a try
 
Back
Top