Reputation: 83
I have read through a bunch of topic on this subject. I've tried a lot of variations, but for some reason, I can't get this to work. Here's the problem: I have a combobox that is populated with the sheet names from an Excel workbook. (I'll include that code, in case the problem is there) When I try to get the selected text value, I get nothing. What am I doing wrong?
/********************************************************************************
* The following code takes in an excel filename and populates a combobox based
* on the excel tab names.
********************************************************************************/
private void GetExcelSheetNames(string excelFile)
{
_Application xlApp;
Workbook xlTemplateWB;
xlApp = new ApplicationClass();
xlTemplateWB = xlApp.Workbooks.Open(Elmnt.getDBPath() + TEMPLATENAME, 0, true,
5, "", "", false, XlPlatform.xlWindows, "", true, false, 0, true, false, false);
foreach (Worksheet temp in xlTemplateWB.Worksheets)
{
cboBenchSheets.Items.Add(temp.Name);
}
xlTemplateWB.Close(true, Missing.Value, Missing.Value);
xlApp.Quit();
}
/********************************************************************************
* This returns the selected item in the cboBenchSheets combo box
********************************************************************************/
public String getSelection()
{
String selected;
selected = cboBenchSheets.Text; //Returns nothing
selected = cboBenchSheets.SelectedText; //Returns nothing
selected = cboBenchSheets.SelectedValue.ToString(); //Returns nothing
selected = cboBenchSheets.GetItemText(cboBenchSheets.SelectedIndex); //Returns -1
return selected;
}
Upvotes: 0
Views: 2518
Reputation: 4218
If you populate a combo box with items and don't do anything else with it, then it has no selected item until a selected item is explicitly set either by the user or in code. I know this seems odd as the combo box appears to select the first item by default, but that's just the way it is.
Try setting the selected item to the first item manually after your for loop that populates the combo box's items collection.
foreach (Worksheet temp in xlTemplateWB.Worksheets)
{
cboBenchSheets.Items.Add(temp.Name);
}
cboBenchSheets.SelectedIndex = 0;
Upvotes: 1
Reputation: 1445
I think this section of the documentation for ComboBox may help:
You can use the SelectedText property to retrieve or change the currently selected text in a ComboBox control. However, you should be aware that the selection can change automatically because of user interaction. For example, if you retrieve the SelectedText value in a button Click event handler, the value will be an empty string. This is because the selection is automatically cleared when the input focus moves from the combo box to the button.
When the combo box loses focus, the selection point moves to the beginning of the text and any selected text becomes unselected. In this case, getting the SelectedText property retrieves an empty string, and setting the SelectedText property adds the specified value to the beginning of the text.
When the combo box gains focus, the full text in the control is automatically selected. If you call the control's Focus method to set the input focus, the full text is selected regardless of whether the control already had focus. When the user selects an item from the drop-down list or by using the UP ARROW and DOWN ARROW keys, the text for the new item is automatically selected. However, if you try to get the SelectedText value in a SelectedIndexChanged or SelectedValueChanged event handler, the property returns an empty string. This is because, at the time of these events, the previous SelectedText value has been cleared and the new value has not yet been set. To retrieve the current value in a SelectedIndexChanged or SelectedValueChanged event handler, use the SelectedItem property instead.
Upvotes: 2