C# ComboBox.SelectedText 不给我 SelectedText

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10194171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:50:24  来源:igfitidea点击:

ComboBox.SelectedText doesn't give me the SelectedText

c#winformsstringcombobox

提问by Cocoa Dev

I am building a String and the code looks like

我正在构建一个字符串,代码看起来像

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

I am using WinForm in VS2010

我在 VS2010 中使用 WinForm

The result looks like

结果看起来像

"The status of my combobox is "

“我的组合框的状态是”

采纳答案by Marco

I think you want to use

我想你想用

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

SelectedText property from MSDN

MSDN 中的SelectedText 属性

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

获取或设置在 ComboBox 的可编辑部分中选择的文本。

while Text property from MSDN

而来自MSDN 的Text 属性

Gets or sets the text associated with this control.

获取或设置与此控件关联的文本。

回答by Marshal

I think you dont need SelectedTextbut you may need

我认为你不需要,SelectedText但你可能需要

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

回答by Shree

From the documentation:

文档

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.

您可以使用 SelectedText 属性来检索或更改 ComboBox 控件中当前选定的文本。但是,您应该知道,由于用户交互,选择可能会自动更改。例如,如果您在按钮 Click 事件处理程序中检索 SelectedText 值,则该值将为空字符串。这是因为当输入焦点从组合框移动到按钮时,选择会自动清除。

当组合框失去焦点时,选择点将移动到文本的开头,并且任何选定的文本都将变为未选中状态。在这种情况下,获取 SelectedText 属性将检索一个空字符串,并设置 SelectedText 属性将指定的值添加到文本的开头。

回答by dansasu11

or try this code

或者试试这个代码

 String status = "The status of my combobox is " + comboBoxTest.SelectedItem.ToString();

回答by Allison Steranko

Here's how I would approach the problem, assuming you want to change the text of say, a label

下面是我将如何处理这个问题,假设你想改变 say 的文本,一个标签

    private void comboBoxtest_SelectedIndexChanged(object sender, EventArgs e)
    {
        var combotext = comboBoxtest.Text;
        var status = "The status of my combo box is" + combotext;
        label1.Text = status;
    }

回答by Mitja Bonca

To get selected item, you have to use SELECTEDITEM property of comboBox. And since this is an Object, if you wanna assign it to a string, you have to convert it to string, by using ToString() method:

要获取所选项目,您必须使用组合框的 SELECTEDITEM 属性。因为这是一个对象,如果你想把它分配给一个字符串,你必须使用 ToString() 方法将它转换为字符串:

string myItem = comboBox1.SelectedItem.ToString(); //this does the trick

回答by infredha

Try this:

尝试这个:

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

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

回答by José Antonio López Cano

I face this problem 5 minutes before.

我在 5 分钟前遇到了这个问题。

I think that a solution (with visual studio 2005) is:

我认为解决方案(使用 Visual Studio 2005)是:

myString = comboBoxTest.GetItemText(comboBoxTest.SelectedItem);

Forgive me if I am wrong.

如果我错了,请原谅我。

回答by JGFMK

If you bind your Combobox to something like KeyValuePair, with properties in the constructor like so...:

如果您将 Combobox 绑定到 KeyValuePair 之类的东西,在构造函数中具有这样的属性...:

 DataSource = dataSource,
 DisplayMember = "Value",
 ValueMember = "Key"

so dataSourceis of type KeyValuePair...

所以dataSource是 KeyValuePair 类型...

You end up with having to do this...

你最终不得不这样做......

  string v = ((KeyValuePair)((ComboBox)c).SelectedItem).Value;

(I had a Dynamic form - where cwas of type Control- so had to cast it to ComboBox)

(我有一个动态表单 -c类型在哪里Control- 所以必须将它转换为 ComboBox)

回答by Nathan

All of the previous answers explain what the OP 'should' do. I am explaining what the .SelectedTextproperty is.

之前的所有答案都解释了 OP“应该”做什么。我在解释什么是.SelectedText财产。

The .SelectedTextproperty is not the text in the combobox. It is the text that is highlighted. It is the same as .SelectedTextproperty for a textbox.

.SelectedText属性不是combobox. 这是突出显示的文本。它与.SelectedTexta 的属性相同textbox

The following picture shows that the .SelectedTextproperty would be equal to "ort".

下图显示该.SelectedText属性将等于“ort”。

enter image description here

在此处输入图片说明