Cannot implicitly convert type 'System.Windows.Forms.Control' to Combobox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • clflyer
    New Member
    • May 2010
    • 5

    Cannot implicitly convert type 'System.Windows.Forms.Control' to Combobox

    I have a small problem. I'm pretty much a newbie to C# coming out of VB.Net.
    I have a windows form that has a GroupBox named grpSoldTo.
    I want to be able to clear all the textboxes and comboxes that are in the GroupBox.
    I know how to do it in VB.Net but when I try something similar in C# I get the following error:

    Cannot implicitly convert type 'System.Windows .Forms.Control' to 'System.Windows .Forms.ComboBox '. An explicit conversion exists (are you missing a cast?)

    Here is a snippet of my code:
    Code:
     //Clear out the inputs
                ComboBox cmbo = default(ComboBox);
    
                foreach (Control ctrl in this.grpSoldTo.Controls) 
                {
    	            if (ctrl.Name.StartsWith("txt")) 
                    {
    		            ctrl.Text = null;
    	            }
                    else if (ctrl.Name.StartsWith("cmbo"))
                    {
                        cmbo = ctrl;
                        cmbo.SelectedIndex = 0;
                    }
                }
    The offending line is cmbo = ctrl;

    Can anyone help?
    Thanks,
    Bill
    Last edited by tlhintoq; May 18 '10, 06:02 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Code:
    cmbo = ctrl;
                        cmbo.SelectedIndex = 0;
    You have to cast the 'control' into a ComboBox

    Code:
    ((ComboBox)ctrl).SelectedIndex = -1;
    Also, to have nothing selected you want to set the selected index to -1, not zero. Remember that all lists are zero-indexed, meaning the first item is index 0, second item is index 1 and so on. -1 is the value of a combobox when nothing is selected.

    Comment

    • clflyer
      New Member
      • May 2010
      • 5

      #3
      Thank you thats what I needed.
      I also discovered that I could do this:
      cmbo = (ComboBox)ctrl;
      cmbo.SelectedIn dex=-1

      Thanks for the quick response.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        You can. But why create the extra cmbo object if you don't need to?
        Glad I could help

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          Another option you can use is the "as" operator... I like to use it when casting like this since you don't know if an object can be cast or not.

          Code:
          ComboBox theComboBox = theControl as ComboBox;
          if (theComboBox != null)
          {
            // whatever you would do if the control is a combobox, using theComboBox
          }
          You can also actually use the is operator to test...
          Code:
          if (theControl is ComboBox)
          {
            // whatever you would do if the control is a combobox, using (ComboBox)theControl
          }
          Just helps make things a little safer. For example, if your controls contain a button with a name that starts with "cmbo", I believe your code will throw an exception.

          Comment

          Working...