• winForm checkedListBox使用方法


        public partial class CheckListBoxTest : Form
        {
            public CheckListBoxTest()
            {
                InitializeComponent();
            }
    
            string connstr = "server=localhost;user id=root;password=****;persistsecurityinfo=True;database=student";
            private void Form1_Load(object sender, EventArgs e)
            {
                using (MySqlConnection conn = new MySqlConnection(connstr))
                {
                    MySqlDataAdapter sda = new MySqlDataAdapter("SELECT * FROM dc_commoncourseclass", conn);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
    
                    checkedListBox1.DataSource = dt;
                    checkedListBox1.ValueMember = "ClassId";
                    checkedListBox1.DisplayMember = "ClassName";
    
                    for(int i = 0; i < checkedListBox1.Items.Count;i++)
                    {
                        DataRowView dr = checkedListBox1.Items[i] as DataRowView;
                        WriteLine(dr["ClassName"] + "," + dr["ClassId"]);
                        
                        if ((int)dr["ClassId"] == 3)
                        {
                            //两种设置选中值的方法
                            checkedListBox1.SetItemChecked(i, true);
                            checkedListBox1.SetItemCheckState(1, CheckState.Indeterminate);
                        }
                    }
    
                    listBox1.SelectionMode = SelectionMode.MultiExtended;
                    listBox1.DataSource = dt;
                    listBox1.ValueMember = "ClassId";
                    listBox1.DisplayMember = "ClassName";
    
                }
    
                
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                string strText = string.Empty;
                foreach(var o in checkedListBox1.CheckedItems)
                {
                    //获取选定项的文本
                    strText += checkedListBox1.GetItemText(o) + ",";
    
                    //通过DataRowView获取值
                    DataRowView drv = o as DataRowView;
                    WriteLine(drv["ClassId"]);
                }
                WriteLine("选择文本:" + strText);
    
                //通过遍历选中项索引获取选中项的值
                string strValue = string.Empty;
                foreach (int i in checkedListBox1.CheckedIndices)
                {
                    checkedListBox1.SetSelected(i, true);
                    strValue += checkedListBox1.SelectedValue + ",";
                }
                WriteLine("选择值:" + strValue);
            }
    
            private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
            {
                System.Diagnostics.Process.Start("http://www.baidu.com");
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                foreach(var o in listBox1.SelectedItems)
                {
                    WriteLine(o);
                }
            }
        }
  • 相关阅读:
    hdu 3722 Card Game 二分图的最大权匹配
    如何将中国知网CNKI中的文献导入EndNote X6
    高性能以太网芯片W5500 数据手册 V1.0(二)
    (贪心5.1.2)POJ 2287 Tian Ji -- The Horse Racing
    [置顶] 斗地主算法的设计与实现--项目介绍&如何定义和构造一张牌
    Robotium学习笔记一
    Robotium学习笔记二
    Robotium学习笔记三
    做一个测试经理我还差多少?
    [每日一题] 11gOCP 1z0-053 :2013-10-9 backup with the KEEP option....................................33
  • 原文地址:https://www.cnblogs.com/superfeeling/p/12898004.html
Copyright © 2020-2023  润新知