简体   繁体   English

将组合框设置值作为字符串

[英]Get combobox set value as string

Found several ways to do so, but none of them works for me.. I have a combobox with variable number of options (taken from dynamic xml file). 找到了几种方法,但它们都不适用于我..我有一个可变数量选项的组合框(取自动态xml文件)。 For next, I need to know what the user choosed, and I cant find out how to do it. 接下来,我需要知道用户选择了什么,我无法知道如何做到这一点。 This is one of ways I found and tried: 这是我发现和尝试的方法之一:

string myString = myCombobox.SelectedValue.ToString();

At least I dont get an error, but when I try to show that string, it does nothing. 至少我没有收到错误,但是当我尝试显示该字符串时,它什么也没做。

Had a similar problem, try this: 有类似的问题,试试这个:

string myString = ((ComboBoxItem)myCombobox.SelectedItem).Content.ToString();

It works for me! 这个对我有用!

Just try to understand seeing myCombobox design. 试着去了解myCombobox设计。

myCombobox.SelectedItem.ToString(); 

应该为你做的伎俩。

string myString = myCombobox.Text;

Use mycombobox.SelectedItem.ToString(); 使用mycombobox.SelectedItem.ToString(); Instead of the SelectedValue 而不是SelectedValue

Try with myCombobox.SelectedValue.Value.ToString() or myCombobox.SelectedValue.Text.ToString() . 尝试使用myCombobox.SelectedValue.Value.ToString()myCombobox.SelectedValue.Text.ToString() Anyway, this question is too low quality for this forum. 无论如何,这个问题对于这个论坛来说质量太低了。 You need to do some research before asking. 在询问之前你需要做一些研究。 That's why I'm voting down you. 这就是我投票给你的原因。

try myCombobox.SelectedItem.ToString(); 试试myCombobox.SelectedItem.ToString();

it will work if you are adding items to the combobox using this way: 如果您使用以下方式将项目添加到组合框,它将起作用:

  comboBox1.Items.Add("Item");

eg 例如

        myComboBox.Items.Add("Item1");
        myComboBox.Items.Add("Item2");
        myComboBox.Items.Add("Item3");

        myComboBox.SelectedIndex = 1; //force change selection
        Console.WriteLine(myComboBox.SelectedItem.ToString()); //will output Item2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM