WinForm messageboxbuttons 和 三级联动

时间:2022-06-01 21:14:29

 MessageBoxButtons:

常用:点击取消不执行任何操作,点击确定,,执行lable中的语句(是否删除时,常用)

点击按钮中的代码:

DialogResult dr= MessageBox.Show("是否继续?", "警告!!!", MessageBoxButtons.OKCancel);
if(dr==DialogResult.OK)
{
label1.Text = "今天天气不错!";
}

效果图:

三级联动:

三个ComboBox,经典:省-市-区/县

public class ChinaStates  //实体类
{
public string AreaCode { get; set; }
public string AreaName { get; set; }
public string ParentAreaCode { get; set; }

}

public class ChinaStatesData  //数据访问类
{
SqlConnection conn = null;
SqlCommand cmd = null;

public ChinaData()
{
conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123;");
cmd = conn.CreateCommand();
}

public List<ChinaStates> Select(string pcode)
{
List<ChinaStates> list = new List<ChinaStates>();
cmd.CommandText = "select *from ChinaStates where ParentAreaCode = @a";
cmd.Parameters.Clear();
cmd.Parameters.Add("@a", pcode);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
ChinaStates c = new ChinaStates()
{
AreaCode = dr[0].ToString(),
AreaName = dr[1].ToString(),
ParentAreaCode = dr[2].ToString()
};
list.Add(c);
}
}
conn.Close();
return list;
}}

主函数中调用:

AreaDataBind(comboBox1, "0001");
AreaDataBind(comboBox2, comboBox1.SelectedValue.ToString());
AreaDataBind(comboBox3, comboBox2.SelectedValue.ToString());

方法:

public void AreaDataBind(ComboBox cb, string Pcode)
{
cb.DataSource = new ChinaData().Select(Pcode);
cb.DisplayMember = "AreaName";
cb.ValueMember = "AreaCode";
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
AreaDataBind(comboBox2, comboBox1.SelectedValue.ToString());
}

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
AreaDataBind(comboBox3, comboBox2.SelectedValue.ToString());
}

WinForm messageboxbuttons 和 三级联动