473,748 Members | 2,461 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

5 New Member
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
4 25382
tlhintoq
3,525 Recognized Expert Specialist
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
clflyer
5 New Member
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.
May 18 '10 #3
tlhintoq
3,525 Recognized Expert Specialist
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 Recognized Expert Top Contributor
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
14971
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 = ddDes.SelectedItem.ToString(); xmlDocument.Save("abc.xml"); However, I get the foll. error: Cannot implicitly convert type 'System.Xml.XmlNode' to 'System.Xml.XmlAttribute'
22
27892
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 = (( curRow == System.DBNull.Value ) ? SqlInt32.Null : (int)curRow ); The errors are (in turn):
6
14378
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 objSender,PaintEventArgs pea) { }
7
13976
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. Here's the code: protected void serverValidateCboBx(object source, ServerValidateEventArgs args)
9
10529
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: objMailMessage.To = dtrRecipient; I'm not sure why I am getting this error. Below is the complete code I am working with.
2
14807
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 webBrowser1_DocumentCompleted method I have the following code. mshtml.HTMLDocument oDoc = new HTMLDocumentClass(); oDoc = (mshtml.HTMLDocument)webBrowser1.Document;
3
18565
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 type 'string' to 'System.Windows.Forms.ColumnHeader' i m not able the understand what is the problem.
1
8681
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 = from staff in Database.Staff where staff.Value.Passcode == txt_password.Text select staff;but i get the error Error 2 Cannot implicitly convert type...
2
4496
by: ransaj | last post by:
objCompanyRequirement.Salary = Convert.ToInt32 (txtCRF_Salary.Text); cannot implicitly convert type 'int' to 'string' objCompanyRequirement.InterviewDate = Convert.ToDateTime(txtCRF_InterviewDate.Text); cannot implicitly convert type 'System.DateTime' to 'string'
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9558
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9378
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9331
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8250
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6798
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2216
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.