我有一个问题,当我尝试在用户未更改comboBox索引时停止代码时,我的代码不起作用
private void button3_Click(object sender, EventArgs e)
{
{
if (comboBox2.SelectedIndex == null)
{
MessageBox.Show("Please complete the fields");
return;
}
}
{
if (comboBox6.SelectedIndex == null)
{
MessageBox.Show("Please complete the fields");
return;
}
}
{
pictureBox2.Image = Soccer_Studio.Properties.Resources.Default;
}
{
if (comboBox6.SelectedIndex == 0)
{
Bitmap bmp = new Bitmap(pictureBox2.Image);
Graphics g = Graphics.FromImage(bmp);
Image newImage = Image.FromFile(@"Database\Logos\Competitions\BPL.png");
RectangleF rect = new RectangleF(610.0F, 17.0F, newImage.Width, newImage.Height);
g.DrawImage(newImage, rect);
g.Flush();
pictureBox2.Image = bmp;
}
}
}
我的应用程序是关于选择特定索引时的情况。绘制特定图像 如果组合框是空的..显示一个显示的Messeage Box(请填写字段)
答案 0 :(得分:1)
属性SelectedIndex
是int
,因此以下语句始终为false,其中的代码将从不执行:
if (comboBox2.SelectedIndex == null)
而是检查值-1:
if (comboBox2.SelectedIndex == -1)
作为旁注,由于您的标题引用了ComboBox
文字,您还可以查看Text
属性:
if (String.IsNullOrEmpty(comboBox2.Text))
答案 1 :(得分:0)
否则我会这样做......
public Form1()
{
InitializeComponent();
comboBox1.Items.Add("Please select an Item");
comboBox1.Items.Add("Item1");
comboBox1.Items.Add("Item2");
comboBox1.Items.Add("Item3");
comboBox1.Items.Add("Item4");
comboBox1.SelectedIndex= 0;
}
然后如果SelectedIndex == 0拒绝....
我想到了愚蠢的用户:)