码迷,mamicode.com
首页 > Windows程序 > 详细

WinForm messageboxbuttons 和 三级联动

时间:2016-09-26 23:03:35      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:

 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 和 三级联动

标签:

原文地址:http://www.cnblogs.com/hcx999/p/5910953.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!