知道什么选项,在下拉框在C#中被选中? [英] Know what option in combobox is selected in C#?

查看:536
本文介绍了知道什么选项,在下拉框在C#中被选中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组合框并在其事情的清单....事情列表中的金额未设置。它是从一个文件夹收集数据,你可以在组合框中项的无限(有点夸张)金额...我怎么知道哪个选项用户选择?



我试过下面的代码,但它不工作。我是全新的,以C#和不知道我做错了。

  comboBox1.SelectedIndex = 0; 
comboBox1.Refresh();

如果(comboBox1.SelectedIndex = 0)
{
//设置路径的代码放在这里
}


解决方案

编辑:显然,我要去的快速回答,而不是良好的信息,我增加更多的信息,以使这个更容易阅读



有是用于触发每次选择更改组合框的事件。在设计师选择组合框,然后将事件选项卡,然后双击的SelectionChanged。



如果你只需要来访问可以说点击一个按钮,你可以使用哪些已选定作为拉胡尔说。

 的button1_Click(...)
{
MessageBox.Show(comboBox1.SelectedItem。的ToString());
}



或者如果你只是想访问当前显示在组合框中的文本

 的button1_Click(...)
{
MessageBox.Show(comboBox1.SelectedText);
}


I have a combobox and have a list of things in it....the amount of things in the list is not set. It is gathering data from a folder and you can have an infinite (a little exaggeration) amount of items in the combobox...how do I know which option a user selects?

I tried the code below but it doesn't work. I'm brand new to C# and don't know what I'm doing wrong.

        comboBox1.SelectedIndex = 0;
        comboBox1.Refresh();

        if(comboBox1.SelectedIndex = 0)
        {
           //setting the path code goes here
        }

解决方案

Edit: Apparently I was going for the quick answer instead of good information, I am adding more info to make this easier to read

There is an event for the combobox that fires everytime the selection changes. in the designer select your combobox, then the events tab and double click SelectionChanged.

if you simply need to access what has been selected from lets say a button click you can use as Rahul stated

Button1_Click(...)
{ 
    MessageBox.Show(comboBox1.SelectedItem.ToString()); 
}

or if you simply want to access the text that is currently displayed in the combobox

Button1_Click(...)
{ 
    MessageBox.Show(comboBox1.SelectedText); 
}

这篇关于知道什么选项,在下拉框在C#中被选中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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