Combo Box default value.

J

John Denver

Hello

On a windows form I've added a combo box, in which I display a list of all
removable drives and CDRom's that are attached to my machine. So to get a
list of drives I'm using the GetDrives method, and as I loop through that
collections I look for the correct drive type. If the type is removable I
then make up a string string devicestr = Name + VolumeLabel

Once I make up the string I add it to the comboBox
comboBox1.Items.Add(devicestr);

There may be a more intuitive way of doing that, but my question is this. I
always want to default to my CDRom and have that displayed. How do I make
my combobox display a certain value ?
 
J

joecool1969

Hello

On a windows form I've added a combo box, in which I display a list of all
removable drives and CDRom's  that are attached to my machine.  So toget a
list of drives I'm using the GetDrives method, and as I loop through that
collections I look for the correct drive type.  If the type is removable  I
then make up a string  string devicestr =  Name + VolumeLabel

Once I make up the string I add it to the comboBox
comboBox1.Items.Add(devicestr);

There may be a more intuitive way of doing that, but my question is this. I
always want to default to my CDRom and have that displayed.  How do I make
my combobox display a certain value ?

You description doesn't indicate it, but I assume when you are looping
through the drive list, you are looking for both Removable and CDROM.
And when you find the CDROM and add it to the ComboBox's Items
collection, save the current index in an int variable. It would be:

this.comboBox1.Items.Count - 1

And after the loading loop set the combobox's SelectedIndex to the int
variable you saved.
 
I

Ignacio Machin ( .NET/ C# MVP )

Hello

On a windows form I've added a combo box, in which I display a list of all
removable drives and CDRom's  that are attached to my machine.  So toget a
list of drives I'm using the GetDrives method, and as I loop through that
collections I look for the correct drive type.  If the type is removable  I
then make up a string  string devicestr =  Name + VolumeLabel

Once I make up the string I add it to the comboBox
comboBox1.Items.Add(devicestr);

There may be a more intuitive way of doing that, but my question is this. I
always want to default to my CDRom and have that displayed.  How do I make
my combobox display a certain value ?

somethin like:
string defaultDrive = string.empty;
foreach( ..... in ..GetDriver())
{
string devicestr = ....;
if ( ... is default drive .... )
defaultDrive =devicestr ;
}
if (defaultDrive!=string.empty)
combo.SelectedItem = defaultDrive;

you could do a similar thing with an index:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top