博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Csharp:Windowsform using CheckedListBox Datasource
阅读量:5344 次
发布时间:2019-06-15

本文共 5399 字,大约阅读时间需要 17 分钟。

1         ///   2         ///   3         ///   4         ///   5         ///   6         private void ListboxCheckboxForm_Load(object sender, EventArgs e)  7         {  8   9             //设置CheckedListBox中第i项的Checked状态 10              11             DataTable dt = new DataTable(); 12             dt.Columns.Add("id", typeof(Guid)); 13             dt.Columns.Add("name", typeof(string)); 14             dt.Rows.Add(Guid.NewGuid(), "geovindu"); 15             dt.Rows.Add(Guid.NewGuid(), "duf"); 16             dt.Rows.Add(Guid.NewGuid(), "涂聚文"); 17             dt.Rows.Add(Guid.NewGuid(), "tujwen"); 18              19  20             //checkedListBox1.Items.Add(""); 21             //checkedListBox1.Items.Insert(0, ""); 22             checkedListBox1.DataSource = dt; 23             checkedListBox1.DisplayMember = "name"; 24             checkedListBox1.ValueMember = "id"; 25  26             checkedListBox1.SetItemCheckState(1, CheckState.Checked); 27         } 28         ///  29         /// 獲取選擇的項 30         ///  31         ///  32         ///  33         private void button1_Click(object sender, EventArgs e) 34         { 35  36  37  38             //1 39             string checkedText = string.Empty; 40             for (int i = 0; i < this.checkedListBox1.CheckedItems.Count; i++) 41             { 42                 this.checkedListBox1.SetSelected(i, true); 43                 checkedText += (String.IsNullOrEmpty(checkedText) ? "" : ",") + this.checkedListBox1.GetItemText(this.checkedListBox1.Items[i]) + "[" +this.checkedListBox1.SelectedValue.ToString()+"]"; 44             } 45             MessageBox.Show(checkedText); 46  47             //2 48             for (int i = 0; i < checkedListBox1.Items.Count; i++) 49             { 50  51                 //如果checkedListBox1的第i项被选中, 52  53                 //则显示checkedListBox1对应的值 54  55                 if (checkedListBox1.GetItemChecked(i)) 56                 { 57                    // MessageBox.Show(checkedListBox1.GetItemText(checkedListBox1.Items[i]) + "[" + this.checkedListBox1.SelectedValue.ToString()+"]"); 58   59                 } 60  61             } 62  63             //3 64             string strCollected = string.Empty; 65  66             for (int i = 0; i < checkedListBox1.Items.Count; i++) 67             { 68  69                 if (checkedListBox1.GetItemChecked(i)) 70                 { 71  72                     if (strCollected == string.Empty) 73                     { 74  75                         strCollected = checkedListBox1.GetItemText(checkedListBox1.Items[i]); 76  77                     } 78  79                     else 80                     { 81  82                         strCollected = strCollected + "/" + checkedListBox1.GetItemText(checkedListBox1.Items[i]); 83  84                     } 85  86                 } 87  88             } 89             //MessageBox.Show(strCollected); 90         } 91  92  93         ///  94         /// 設定是否全選 95         ///  96         ///  97         ///  98         private void select_all_CheckedChanged(object sender, EventArgs e) 99         {100             if (select_all.Checked)101             {102                 for (int j = 0; j < checkedListBox1.Items.Count; j++)103                     checkedListBox1.SetItemChecked(j, true);104 105             }106             else107             {108                 for (int j = 0; j < checkedListBox1.Items.Count; j++)109                     checkedListBox1.SetItemChecked(j, false);110 111             }112         }113         /// 114         /// 獲取選擇的項115         /// 116         /// 117         /// 118         private void button2_Click(object sender, EventArgs e)119         {120             string checkedText = string.Empty;121             for (int i = 0; i < this.checkedListBox1.Items.Count; i++)122             {123                 if (this.checkedListBox1.GetItemChecked(i))124                 {125                     this.checkedListBox1.SetSelected(i, true);126                     checkedText += (String.IsNullOrEmpty(checkedText) ? "" : ",") +"["+this.checkedListBox1.SelectedValue.ToString()+"]" + this.checkedListBox1.GetItemText(checkedListBox1.Items[i]);127                 }128             }129             MessageBox.Show(checkedText);130         }131         /// 132         /// 設置選擇項133         /// 134         /// 135         /// 136         private void button3_Click(object sender, EventArgs e)137         {138 139 140             checkedListBox1.DataSource = null;            141             DataTable dt = new DataTable();142             dt.Columns.Add("id", typeof(Guid));143             dt.Columns.Add("name", typeof(string));144             dt.Columns.Add("check", typeof(bool));145 146             dt.Rows.Add(Guid.NewGuid(), "geovindu",false);147             dt.Rows.Add(Guid.NewGuid(), "duf",true);148             dt.Rows.Add(Guid.NewGuid(), "涂聚文",false);149             dt.Rows.Add(Guid.NewGuid(), "tujwen",true);150 151             checkedListBox1.DataSource = dt;152             checkedListBox1.DisplayMember = "name";153             checkedListBox1.ValueMember = "id";  154             //155             for (int i = 0; i < dt.Rows.Count; i++)156             {157 158                 checkedListBox1.SetItemChecked(i, (bool)dt.Rows[i]["check"]);159             }160 161         }162     }

 

转载于:https://www.cnblogs.com/geovindu/archive/2013/06/05/3119262.html

你可能感兴趣的文章
[Android学习系列16]Android把php输出的json加载到listview
查看>>
20145205 《信息安全系统设计基础》第14周学习总结
查看>>
6)添加一个窗口的图标
查看>>
POJ - 1422 Air Raid 二分图最大匹配
查看>>
Road Map
查看>>
正则替换中的一个Bug
查看>>
HI3531uboot开机画面 分类: arm-linux-Ubunt...
查看>>
制作U盘启动CDLinux 分类: 生活百科 ...
查看>>
strcpy函数里的小九九
查看>>
搭建ssm过程中遇到的问题集
查看>>
OpenLayers绘制图形
查看>>
tp5集合h5 wap和公众号支付
查看>>
Flutter学习笔记(一)
查看>>
iOS10 国行iPhone联网权限问题处理
查看>>
洛谷 P1991 无线通讯网
查看>>
[HIHO1184]连通性二·边的双连通分量(双连通分量)
查看>>
Codeforces Round #178 (Div. 2) B. Shaass and Bookshelf 【动态规划】0-1背包
查看>>
SparkStreaming 源码分析
查看>>
【算法】—— 随机音乐的播放算法
查看>>
mysql asyn 示例
查看>>