Combobox.Selectedtext Doesn't Give Me the Selectedtext

ComboBox.SelectedText doesn't give me the SelectedText

I think you want to use

String status = "The status of my combobox is " + comboBoxTest.Text

SelectedText property from MSDN

Gets or sets the text that is selected in the editable portion of a
ComboBox.

while Text property from MSDN

Gets or sets the text associated with this control.

ComboBox SelectedText, why is it not switching to the SelectedText item?

I think you're misunderstanding what the SelectedText property is, refer to the MSDN documentation.

The SelectedText property is not the item from the list of items, it's the portion of an editable combobox that is selected, as if you were doing a copy/paste type of selection.

Your SetComboBoxToTextIndex method is the proper way to find and select an item in the list. Aternatively, if your ComboBoxItem properly implements Equals, you can find the appropriate instance and set the SelectedItem property.

Right way to set selected text of combobox in winforms?

In the constructor, you need to set the selected item, for example:

foreach ( var item in combobox.Items )
if ( (string)item == obj.StringProperty )
combobox.SelectedItem = item;

Or:

foreach ( var item in combobox.Items )
if ( (int)item == Convert.ToInt32(obj.StringProperty) )
combobox.SelectedItem = item;

It's confusing but despite its name, the property SelectedText is not really the selected item... because combo box items are objects and not strings: texts shown are a representation of the item objects using ToString().

Therefore setting the selected text will not guarantee to select an item and we can prefer setting the SelectedItem.

In addition to these considerations, you set the selected text property in the constructor after populating the combo box and that can cause problems because it is before the form and the control are drawn or something like that... that is to say perhaps before the ToString() methods are called on items to prepare the visual cache, so setting the selected text can't get a match with the list.

Setting the selected text selects an existing item if done in the form load or shown events.

private void Form_Load(object sender, EventArgs e)
{
combobox.SelectedText = obj.StringProperty;
}

ComboBox.SelectedText doesn't give me the SelectedText

ComboBox.SelectedText Property

label.Text = ComboBox.SelectedText(); always null?

I think the problem is in use of SelectedText. The SelectedTextproperty "Gets or sets the text that is selected in the editable portion of a System.Windows.Forms.ComboBox".

Instead try to use the SelectedItem property.

label1.Text = comboBox1.SelectedItem.ToString();

Can't save selected Combobox Item because SelectedText is empty String. Why?

OK, Thank you at all, I solved the problem. All your Code is correct and there are many way's, like comboBox.Text, comboBox.Item to save it.... My problem was that, I saved it twice, once after connecting to the scale and once when the Form is closing... And when Form was closing it wrote the selected index into the file so that is why it couldn't work. I didn't noticed that and was wondering why its not working. Sorry, and thank you!

C# show MessageBox based on Combobox SelectedText

Try this.

if (comboSelectServer.Text == "SERV1")
{
MessageBox.Show("SERV1");
}
else if (comboSelectServer.Text == "SERV2")
{
MessageBox.Show("SERV2");
}
else if (comboSelectServer.Text == "SERV3")
{
MessageBox.Show("SERV3");
}

However, this is easier...

if (comboSelectServer.SelectedIndex == 0) //SERV1
{
MessageBox.Show("SERV1");
}
else if (comboSelectServer.SelectedIndex == 1) //SERV2
{
MessageBox.Show("SERV2");
}
else if (comboSelectServer.SelectedIndex == 2) //SERV3
{
MessageBox.Show("SERV3");
}

How to get ComboBox.SelectedText in WPF

You can get access to the ComboBox's TextBox by using:

var edit = (TextBox)myCombo.Template.FindName("PART_EditableTextBox", myCombo);

Then you can access the SelectedText property of that TextBox:

var selectedText = edit.SelectedText;

Validating if a combobox it's SelectedText property is empty always fails

Generally a few observations/suggestions.

First you're using string values and are basing logic on these values, you might want to look into using an Enum and binding all it's values to the combo box. Then use the SelectedItem property and compare it to the Enum.

When nothing is selected the SelectedItem will return NULL, another option is using SelectedIndex which will return -1 when no item has been selected.

So with the SelectedIndex it would become something like;

if (comboBox1.SelectedIndex == -1)//Nothing selected
{
MessageBox.Show("You must select a conversion type", "Error");
}
else
{
//Do Magic
}

Generally using string comparisons should only be done when anything "strong" like an int comparison or even better an enum comparison is not possible. (Maybe it's just me though but strings change to often and are just scary for this sort of stuff.)

For the enum suggestion possibly look at one of these links;

Binding an enum to a WinForms combo box, and then setting it

Load values of enum type into a combobox

Is it possible to load items from an Enum to a ComboBox in .NET 3.5?

Binding a ComboBox to an Enumeration

I'm not sure which .NET version and things you're using as binding is alot easier in WPF then in the old windows form (in my opinion).



Related Topics



Leave a reply



Submit