473,379 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1.  //Clear out the inputs
  2.             ComboBox cmbo = default(ComboBox);
  3.  
  4.             foreach (Control ctrl in this.grpSoldTo.Controls) 
  5.             {
  6.                 if (ctrl.Name.StartsWith("txt")) 
  7.                 {
  8.                     ctrl.Text = null;
  9.                 }
  10.                 else if (ctrl.Name.StartsWith("cmbo"))
  11.                 {
  12.                     cmbo = ctrl;
  13.                     cmbo.SelectedIndex = 0;
  14.                 }
  15.             }
The offending line is cmbo = ctrl;

Can anyone help?
Thanks,
Bill
May 18 '10 #1

✓ answered by tlhintoq

Expand|Select|Wrap|Line Numbers
  1. cmbo = ctrl;
  2.                     cmbo.SelectedIndex = 0;
You have to cast the 'control' into a ComboBox

Expand|Select|Wrap|Line Numbers
  1. ((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.

4 25328
tlhintoq
3,525 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. cmbo = ctrl;
  2.                     cmbo.SelectedIndex = 0;
You have to cast the 'control' into a ComboBox

Expand|Select|Wrap|Line Numbers
  1. ((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.
May 18 '10 #2
Thank you thats what I needed.
I also discovered that I could do this:
cmbo = (ComboBox)ctrl;
cmbo.SelectedIndex=-1

Thanks for the quick response.
May 18 '10 #3
tlhintoq
3,525 Expert 2GB
You can. But why create the extra cmbo object if you don't need to?
Glad I could help
May 18 '10 #4
GaryTexmo
1,501 Expert 1GB
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.

Expand|Select|Wrap|Line Numbers
  1. ComboBox theComboBox = theControl as ComboBox;
  2. if (theComboBox != null)
  3. {
  4.   // whatever you would do if the control is a combobox, using theComboBox
  5. }
You can also actually use the is operator to test...
Expand|Select|Wrap|Line Numbers
  1. if (theControl is ComboBox)
  2. {
  3.   // whatever you would do if the control is a combobox, using (ComboBox)theControl
  4. }
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.
May 18 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Anita C | last post by:
I have the foll. code to update the value of an attribute: xmlDocument.Load("abc.xml"); XmlAttribute xmlAttrib = xmlDocument.SelectSingleNode(root/web/theme/@desc); xmlAttrib.Value =...
22
by: Christoph Boget | last post by:
I am getting an error (a few among many) for the following lines of code: retval.BrokerName = (( curRow == System.DBNull.Value ) ? SqlString.Null : (string)curRow ); retval.BrokerGroupId = ((...
6
by: juli | last post by:
I declared: public delegate void PaintEventHandler(object objSender,PaintEventArgs pea); and this.Paint+=new PaintEventHandler(MyPaintHandler); and the: static void MyPaintHandler(object...
7
by: Assimalyst | last post by:
Hi, I am trying to write some server side validator code in C#. I want to have a piece of code that can be used by all DropDownLists on a webform, giving a false value if the SelectedIndex is 0....
9
by: Andy Sutorius | last post by:
Hi, I am receiving the error when compiling the project, "cannot implicitly convert type object to string". The error points to this line of code and underlines the dtrRecipient:...
2
by: Paul Hemans | last post by:
I am very new at .Net. I have a small project where I need to manipulate the contents of a web page. I have a form with a web browser control (webBrowser1) on it. Within the...
3
by: pankajprakash | last post by:
hi all i m creating a window application and i used a listview control in a window's form. But when i try to build that application then the following error prompts : Cannot implicitly convert...
1
by: alex21 | last post by:
Ok i am trying to use a Linq query to access a dictionary. public static Dictionary<string, Client> Clients = new Dictionary<string, Client>();Using this Linq query: IEnumerable<Staff> loginquery...
2
by: ransaj | last post by:
objCompanyRequirement.Salary = Convert.ToInt32 (txtCRF_Salary.Text); cannot implicitly convert type 'int' to 'string' objCompanyRequirement.InterviewDate = ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.