Solved

How do you set default selected in a combo box in c#.net

Posted on 2011-02-17
11
531 Views
Last Modified: 2012-05-11
How do you set default selected in a combo box in c#.net
0
Comment
Question by:rockhammer99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
11 Comments
 
LVL 15

Accepted Solution

by:
Russell_Venable earned 250 total points
ID: 34921428
cmbBox.SelectedIndex = 0; //<-- Set the number of the index location of the option you want as your default here

Open in new window

0
 
LVL 16

Assisted Solution

by:BurnieP
BurnieP earned 250 total points
ID: 34921763
If you don't know the index you can set it by value also.

cb.SelectedValue = "MyValue"l
0
 
LVL 15

Expert Comment

by:Russell_Venable
ID: 34921920
Also take note you can do that this way as well.
cmbBox.SelectedIndex = cmbBox.FindString(@"YourValueHere");

Open in new window


This will give you the index of the Value you want. You can test it by setting a seperate integer and calling it like this.
int index = cmbBox.FindString(@"YourValueHere");
System.Windows.Forms.MessageBox.Show(index);

Open in new window

0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 11

Expert Comment

by:Sudhakar Pulivarthi
ID: 34922275
Hi,

This will show the possible ways additionally.
http://www.dotnetspider.com/forum/161988-Combo-box-default-Selection-C.aspx
0
 
LVL 12

Expert Comment

by:starlite551
ID: 34928088
Try this method :
public void SearchComboBox(ComboBox cbTest,string searchValue,string searchFlag)
{
       switch(searchFlag)
       {
             case "I" :
                cbTest.SelectedIndex = Int32.Parse(searchValue);
                break;
             case "V" :
                cbTest.SelectedValue = searchValue;
                break;
             case "T" :
                cbTest.SelectedItem.ToString() = searchValue;
                break;
             default :
                cbTest.SetSelected(0,true);//Select First Index..     
                break;                  
       }
}

Open in new window

0
 
LVL 12

Expert Comment

by:starlite551
ID: 34928130
How to call above method ?? Answer is this way :
SearchComboBox(comboBox1,"2","I"); //TO SELECT 2nd Index Position.
SearchComboBox(comboBox1,"myValue","V"); //TO SELECT Value "myValue" from list..
SearchComboBox(comboBox1,"xyz","lkj"); //TO SELECT 0 index by default..

Open in new window

0
 
LVL 5

Expert Comment

by:Tom Knowlton
ID: 36122545
I've requested that this question be deleted for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
 
LVL 11

Expert Comment

by:Sudhakar Pulivarthi
ID: 36122546
Hi,

I have provided the possible solution in comment http:#34922275 Pls review the answers for the question asked before taking decision. And accordingly close the issues.
0
 
LVL 15

Expert Comment

by:Russell_Venable
ID: 36122980
I get these all the time even though I give them many different solutions. Seems to be more often too. Ignoring your own type of deal here. It's what we have come to know it as a "Fire & Forget" question.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question