我收到此错误:“索引-1没有值。”单击组合框时 [英] I'm getting this error: "Index -1 does not have a value." when clicking a combobox
问题描述
当我点击winforms中的网格视图的组合框(下拉列表)时,我收到此错误。
索引 - 1没有价值。
请帮助我......我被困了很长时间......谷歌搜索没有运气。 :(
谢谢。
I''m getting this error when i click on the combobox (dropdown) the grid view in the winforms.
Index -1 does not have a value.
Please help me in this... I''m Stuck for a long time... No luck in googling. :(
Thanks.
推荐答案
当没有选择任何项目时,返回的索引值SelectedIndex属性为-1
在事件处理程序的顶部使用它之前检查它:
When no item is selected, the index value that is returned by the SelectedIndex property is -1
At the top of your event handler check it before you use it:
ComboBox cb = sender as ComboBox;
if (cb != null && cb.SelectedIndex >= 0)
{
...
}
嗯,我们确实需要看一些代码来帮助解决这个问题,但下面是一个快速演示:
Hi,
Umm we really do need to see some code to help on this but a quick demo below:
namespace WindowsFormsApplication160
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("index = "+comboBox1.SelectedIndex.ToString()+"\nvalue = "+comboBox1.Text);
}
}
}
如果在阅读时组合框中没有值,则返回-1。如果您有数据,请确保显示。要显示值,请使用以下内容:
This will return -1 if there is no value in the Combo box when you read it. If you have data in it make sure it is shown. To show a value use the below:
comboBox1.SelectedIndex = 1;
这将获取您输入到组合框Items值的数据列表中的第二个值。要在C#中充分使用组合框,请参阅
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox(v = VS.90)的.aspx [ ^ ]
希望有所帮助。
格伦
That will take the second value in the list of data that you have entered to the combo box Items value. For a full use of combo boxes in C# see
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox(v=VS.90).aspx[^]
Hope that helps.
Glenn
这篇关于我收到此错误:“索引-1没有值。”单击组合框时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!