我想在此代码中添加一个if语句,说明..
如果我单击此按钮,它将检查ListU.SelectedValue是否为空,如果它为空,则会显示一个消息框,说“请在继续之前选择一个名称”,如果它不为空,则代码然后运行。
我该怎么做?
这是按钮点击的代码。 (我知道,我的代码需要一些参数,我们暂时可以忽略它)
private void button4_Click(object sender, EventArgs e)
{
//update code//
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
SqlDataAdapter daCount = new SqlDataAdapter("select iCount from ComDet where cName = @cName", conn);
daCount.SelectCommand.Parameters.Add("@cName", SqlDbType.VarChar).Value = ListU.SelectedValue;
DataTable dtC = new DataTable();
daCount.Fill(dtC);
DataRow firstRow = dtC.Rows[0];
string x = firstRow["iCount"].ToString();
int y = Int32.Parse(x);
int z = y + 1;
SqlCommand cmdC = conn.CreateCommand();
cmdC.CommandText = "Update ComDet set iCount = " + z + ", ViewTime = '" + lblTime.Text + "', LastView = '" + txtUser2.Text + "' Where cName = '" + ListU.SelectedValue.ToString() + "'";
cmdC.ExecuteNonQuery();
conn.Close();
var ufdet = new UserFullDetail(ListU.SelectedValue.ToString());
ufdet.ShowDialog();
}
答案 0 :(得分:3)
private void button4_Click(object sender, EventArgs e)
{
if(ListU.SelectedValue == null || ListU.SelectedValue.ToString() == string.Empty)
{
MessageBox.Show("Select something from the listbox, please");
return;
}
.....
答案 1 :(得分:1)
试试这个
private void button4_Click(object sender, EventArgs e)
{
if(ListU.SelectedValue == null || ListU.SelectedValue.ToString() == string.Empty)
{
MessageBox.Show("Select something from the listbox, please");
}
else
{
//update code//
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
SqlDataAdapter daCount = new SqlDataAdapter("select iCount from ComDet where cName = @cName", conn);
daCount.SelectCommand.Parameters.Add("@cName", SqlDbType.VarChar).Value = ListU.SelectedValue;
DataTable dtC = new DataTable();
daCount.Fill(dtC);
DataRow firstRow = dtC.Rows[0];
string x = firstRow["iCount"].ToString();
int y = Int32.Parse(x);
int z = y + 1;
SqlCommand cmdC = conn.CreateCommand();
cmdC.CommandText = "Update ComDet set iCount = " + z + ", ViewTime = '" + lblTime.Text + "', LastView = '" + txtUser2.Text + "' Where cName = '" + ListU.SelectedValue.ToString() + "'";
cmdC.ExecuteNonQuery();
conn.Close();
var ufdet = new UserFullDetail(ListU.SelectedValue.ToString());
ufdet.ShowDialog();
}
}
答案 2 :(得分:1)
我更喜欢这个:
private void button4_Click(object sender, EventArgs e)
{
if (ListU.SelectedItem == null)
{
MessageBox.Show("Please pick a name before continuing.");
}
else
{
// Run the code.
}
}
这使得else条件显而易见,而不是像史蒂夫的例子那样暗示。
另一种方法是,如果在ListU中选择了某些内容,只启用button4,例如,
private void ListU_SelectedIndexChanged(object sender, System.EventArgs e)
{
button4.Enabled = ListU.SelectedItem != null;
}
答案 3 :(得分:0)
试试这个:
private void button4_Click(object sender, EventArgs e)
{
if(ListU.SelectedValue == null || string.IsNullOrEmpty(Convert.ToString(ListU.SelectedValue)))
{
MessageBox.Show("Select something from the listbox.");
return;
}
.....