问题描述
我使用Microsoft Visual Studio 2005专业版在Microsoft Visual C#2005中使用了以下代码。
I have used the following code in Microsoft Visual C# 2005 using Microsoft Visual Studio 2005 Professional Edition.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
_selectedVolt = comboBox1.SelectedIndex;
}
private void comboBox1_TextChanged(object sender, EventArgs e)
{
volt = Convert.ToDouble(comboBox1.SelectedText);
}
我已经在设计时将属性调色板的项目字段中的comboBox值(0.7,0.8,0.9)。
现在请告诉我将comboBox中所选文本的值存储在变量中的过程。当我使用'volt'值进行算术计算时,它没有任何值。
I have put the values (0.7, 0.8, 0.9) for comboBox in Items field of Property pallette in design-time.
Now please let me know the process of storing the value of the selected text from comboBox in a variable. When I'm doing arithmetic calculation with the value of 'volt' it takes no value in it.
推荐答案
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// The index of the selected Item
var index = comboBox1.SelectedIndex;
// The Text in a writable combobox. Is is used for text suggestion etc
var text = comboBox1.SelectedText;
// The text or string value as it appears in the selected item
var value = comboBox1.SelectedItem.Text
// comboBox1.SelectedItem.ToString() calls comboBox1.SelectedItem.Text
// This means that you can use the object comboBox1.SelectedItem for string
// fields such as:
var dValue = Convert.ToDouble(comboBox1.SelectedItem);
}
volt = Convert.ToDouble(comboBox1.SelectedText);
并将鼠标悬停在 comboBox1.SelectedText
上,您会发现它是空白的。而是使用
and hover your mouse over comboBox1.SelectedText
you will discover that it is blank. Instead use
volt = Convert.ToDouble(comboBox1.SelectedItem);
这篇关于如何从comboBox中保存变量中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!