Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted
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.

Being smarter than you look is always better than looking smarter than you are.
Posted

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

Posted
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

Being smarter than you look is always better than looking smarter than you are.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...