如何从comboBox中保存变量中的值 [英] How do I save value in a variable from a comboBox

查看:164
本文介绍了如何从comboBox中保存变量中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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.

推荐答案

Winforms:



Winforms:

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);
} 


如果在行上放置一个断点
If you put a breakpoint on the line
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中保存变量中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆