private void Login_Load(object sender, EventArgs e)
{
//这是Load时绑定的数据
comboBox3.DataSource = ALogin.GetAdminID1().Tables[0];
comboBox3.ValueMember = "AdminID";
comboBox3.SelectedIndex = -1;
}
在登录时,我希望当用户没有选择时,提示“请选择.....”
string AdminID = comboBox3.SelectedValue.ToString().Trim();
//管理员登录
private void button3_Click(object sender, EventArgs e)
{
string AdminID = comboBox3.SelectedValue.ToString().Trim();
string PassWord = textBox4.Text.Trim();
string AdminType = comboBox2.SelectedValue.ToString().Trim();
if (comboBox3.SelectedValue.ToString().Length==0)
{
MessageBox.Show("管理员不能为空!", "提示!");
return;
} 是这样吗?不过我这样做提示错误:“未将对象引用到实例”
你组号判断一下,改为
if (comboBox3.SelectedValue==null || comboBox3.SelectedValue.ToString().Length==0)string AdminID = comboBox3.SelectedValue.ToString().Trim();这句也是一样的道理,最好判断一下comboBox3.SelectedValue是否为空
as
select AdminID from AdminInfo
if(combox1.text == "")
{
messageBox.show("");
return;
}
{
MessageBox.Show("管理员不能为空!", "提示!");
return;
}
我不赞成写成comboBox3.SelectedIndex = -1之类的东西,如果你绑定了数据,去点了一下后,自然就不可能为空了
这样子还不如加一个:
comboxBox3.Items.Insert(0,"--请选择--");
根据
comboxBox3.Text("请选择")来判断是否弹出对话框
MessageBox.Show("管理员不能为空!", "提示")
Return
End If
if (ComboBox3.SelectedValue == null)
{
MessageBox.Show("管理员不能为空!", "提示");
return;
}