zoukankan      html  css  js  c++  java
  • WinForm messageboxbuttons 和 三级联动

     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());
    }

  • 相关阅读:
    通过唯一ID实现简单的日志跟踪实现
    从零单排入门机器学习:Octave/matlab的经常使用知识之矩阵和向量
    zoj 1671 Walking Ant
    JDBC基础
    Android从源码看ListView的重用机制
    JavaScript设计模式 Item9 --适配器模式Adapter
    C++11新特性之 std::forward(完美转发)
    [组合数]求组合数的几种方法总结
    HDU 4005 The war(双连通好题)
    Workspace in use or cannot be created, choose a different one.--错误解决的方法
  • 原文地址:https://www.cnblogs.com/hcx999/p/5910953.html
Copyright © 2011-2022 走看看