最近要研究winform(2.0)的控件和vc++ 2008的控件,先做的是winform控件.

随便找了个CheckedListBox来看看,本来预计20分钟搞定一个自定义List的绑定,然后捉出勾选的内容,结果用了25分钟完成.

后来想了解多点CheckedListBox的属性和功能,就上了MSDN查CheckedListBox类,查到了一个"示例",使用方法比较全面,可是没有自定义List的绑定和捉出勾选内容的部分,郁闷.

现在把两部分都贴出来,供日后使用哪个和参考.

一.网上找到的

1.checkedlistbox的绑定

  CheckedListBox应该是由ListBox扩展而来的?但在使用的时候,可能会发现——它不支持DataSource属性,不能像ListBox那样指定其数据源为一个DataTable。
        事实上,CheckedListBox像ListBox一样有DataSource属性,DisplayMember和ValueMemeber属性也都是有的,只是IntelliSense不能将其智能感知出来。

  因此,我们可以通过代码将CheckedListBox绑定.

 eg:

  this.checklbUser.DataSource = ds.Tables["table1"];
  this.checklbUser.DisplayMember = "UserName";
  this.checklbUser.ValueMember = "UserID";

2.如何获取checkedlistbox的DisplayMember和ValueMember

for(int i=0; i < checklbUser.Items.Count; i )
{
if(checklbUser.GetItemChecked(i)==true)
{
MessageBox.Show(checklbUser.GetItemText(checklbUser.Items[i]));//获取DisplayMember

MessageBox.Show( ds.Tables["table1"].Rows[i]["UserID"].ToString() );//获取ValueMember

//其实上面用ds捉出数据,未免太牵强了.

}
}

二.微软找到的示例.开一个winformApplication,把program.cs的内容清空,贴上下面的代码,自己看咯.

namespace WindowsApplication1
{
   using System;
   using System.Drawing;
   using System.Collections;
   using System.ComponentModel;
   using System.Windows.Forms;
   using System.Data;
   using System.IO ;

public class Form1 : System.Windows.Forms.Form
   {
      private System.Windows.Forms.CheckedListBox checkedListBox1;
      private System.Windows.Forms.TextBox textBox1;
      private System.Windows.Forms.Button button1;
      private System.Windows.Forms.Button button2;
      private System.Windows.Forms.ListBox listBox1;
      private System.Windows.Forms.Button button3;
        private System.ComponentModel.Container components;

public Form1()
      {
            InitializeComponent();

// Sets up the initial objects in the CheckedListBox.
            string[] myFruit = {"Apples", "Oranges","Tomato"};
         checkedListBox1.Items.AddRange(myFruit);

// Changes the selection mode from double-click to single click.
         checkedListBox1.CheckOnClick = true;
      }

protected override void Dispose( bool disposing )
   {
  if( disposing )
  {
   if (components != null)
   {
     components.Dispose();
   }
  }
  base.Dispose( disposing );
   }

private void InitializeComponent()
      {
         this.components = new System.ComponentModel.Container();
         this.textBox1 = new System.Windows.Forms.TextBox();
         this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
         this.listBox1 = new System.Windows.Forms.ListBox();
         this.button1 = new System.Windows.Forms.Button();
         this.button2 = new System.Windows.Forms.Button();
         this.button3 = new System.Windows.Forms.Button();
         this.textBox1.Location = new System.Drawing.Point(144, 64);
         this.textBox1.Size = new System.Drawing.Size(128, 20);
         this.textBox1.TabIndex = 1;
         this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
         this.checkedListBox1.Location = new System.Drawing.Point(16, 64);
         this.checkedListBox1.Size = new System.Drawing.Size(120, 184);
         this.checkedListBox1.TabIndex = 0;
         this.checkedListBox1.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.checkedListBox1_ItemCheck);
         this.listBox1.Location = new System.Drawing.Point(408, 64);
         this.listBox1.Size = new System.Drawing.Size(128, 186);
         this.listBox1.TabIndex = 3;
         this.button1.Enabled = false;
         this.button1.Location = new System.Drawing.Point(144, 104);
         this.button1.Size = new System.Drawing.Size(104, 32);
         this.button1.TabIndex = 2;
         this.button1.Text = "Add Fruit";
         this.button1.Click += new System.EventHandler(this.button1_Click);
         this.button2.Enabled = false;
         this.button2.Location = new System.Drawing.Point(288, 64);
         this.button2.Size = new System.Drawing.Size(104, 32);
         this.button2.TabIndex = 2;
         this.button2.Text = "Show Order";
         this.button2.Click += new System.EventHandler(this.button2_Click);
         this.button3.Enabled = false;
         this.button3.Location = new System.Drawing.Point(288, 104);
         this.button3.Size = new System.Drawing.Size(104, 32);
         this.button3.TabIndex = 2;
         this.button3.Text = "Save Order";
         this.button3.Click += new System.EventHandler(this.button3_Click);
         this.ClientSize = new System.Drawing.Size(563, 273);
         this.Controls.AddRange(new System.Windows.Forms.Control[] {this.listBox1,
                                                        this.button3,
                                                        this.button2,
                                                        this.button1,
                                                        this.textBox1,
                                                        this.checkedListBox1});
         this.Text = "Fruit Order";
      }

[STAThread]
      public static void Main(string[] args)
      {
         Application.Run(new Form1());
      }

// Adds the string if the text box has data in it.
      private void button1_Click(object sender, System.EventArgs e)
      {
         if(textBox1.Text != "")
         {
            if(checkedListBox1.CheckedItems.Contains(textBox1.Text)== false)
               checkedListBox1.Items.Add(textBox1.Text,CheckState.Checked);
            textBox1.Text = "";
         }

}
      // Activates or deactivates the Add button.
      private void textBox1_TextChanged(object sender, System.EventArgs e)
      {
         if (textBox1.Text == "")
         {
            button1.Enabled = false;
         }
         else
         {
            button1.Enabled = true;
         }

}

// Moves the checked items from the CheckedListBox to the listBox.
      private void button2_Click(object sender, System.EventArgs e)
      {
         listBox1.Items.Clear();
         button3.Enabled=false;
         for (int i=0; i< checkedListBox1.CheckedItems.Count;i++)
         {
            listBox1.Items.Add(checkedListBox1.CheckedItems[i]);
         }
         if (listBox1.Items.Count>0)
            button3.Enabled=true;

}
        // Activates the move button if there are checked items.
      private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
      {
         if(e.NewValue==CheckState.Unchecked)
         {
            if(checkedListBox1.CheckedItems.Count==1)
            {
               button2.Enabled = false;
            }
         }
         else
         {
            button2.Enabled = true;
         }
      }

// Saves the items to a file.
      private void button3_Click(object sender, System.EventArgs e)
      {  
         // Insert code to save a file.
         listBox1.Items.Clear();
         IEnumerator myEnumerator;
         myEnumerator = checkedListBox1.CheckedIndices.GetEnumerator();
         int y;
         while (myEnumerator.MoveNext() != false)
         {
            y =(int) myEnumerator.Current;
            checkedListBox1.SetItemChecked(y, false);
         }
         button3.Enabled = false ;
      }       
    }
}

转载于:https://www.cnblogs.com/heimark/archive/2010/08/04/1792277.html

winform控件的学习相关推荐

  1. winform界面嵌入dwg图纸_WPF中使用WinForm控件预览DWG文件(学习笔记)

    操作环境:XP,C# ,.Net4.0和VS2010中测试 WinForm中使用DWGThumbnail不用这么麻烦,下面讨论的是在WPF中使用,WPF中无法直接引用DWGThumbnail.ocx来 ...

  2. 如何设置Winform控件的ClientRectangle

    最近学习制作WinForm控件,自己动手写控件的时候才发现System.Windows.Forms.Control 竟然没有提供默认的border绘制.记得以前用API做控件的时候,只需要设置空间窗口 ...

  3. winform checkbox要点击两次_开源C# Winform控件库SunnyUI强力推荐

    本站(https://dotnet9.com)曾介绍过一款Winform开源控件库<HZHControls>,文章发布后不少朋友热情的咨询相关控件库信息,由此看来Winform在大家心中的 ...

  4. 开源C# Winform控件库《SunnyUI》强力推荐

    本站(https://dotnet9.com)曾介绍过一款Winform开源控件库<HZHControls>,文章发布后不少朋友热情的咨询相关控件库信息,由此看来Winform在大家心中的 ...

  5. 《Dotnet9》系列-开源C# Winform控件库1《HZHControls》强力推荐

    大家好,我是Dotnet9小编,一个从事dotnet开发8年+的程序员.我最近在写dotnet分享文章,希望能让更多人看到dotnet的发展,了解更多dotnet技术,帮助dotnet程序员应用dot ...

  6. c# 开发winform控件

    c#开发winform控件 工作中常常遇到这样的问题,为了达到某种特殊的需求,我们常要定制一些控件.写控件让人头大的一件事,开始写控件总是很难的.不过有人已经给我们开了一个好头,跟着学习一下: 转链接 ...

  7. WINFORM控件开发 来源:博客园 作者:纶巾客

    (1)开篇 我本人不是专业的控件开发人员,只是在平常的工作中,需要自己开发一些控件.在自己开发WinForm控件的时候,没有太多可以借鉴的资料,只能盯着MSDN使劲看,还好总算有些收获.现在我会把这些 ...

  8. C# Winform控件库分享,免费开源,支持中文!(附DLL及教程)

    这款控件包是基于MaterialSkin2二次开发的,可以更换想要的皮肤主题,一键转换暗色系,还拥有非常炫酷的动画,非常好看,原本的MaterialSkin2是国外团队开发的,不支持中文,所以我在里面 ...

  9. Winform控件开发(1)——Label(史上最全)

    文章目录 前言: 一.属性 1.Name 属性 2.AllowDrop 属性 3.Anchor 属性 4.AutoEllipsis 属性 5.autosize 属性 6.backcolor 属性 7. ...

最新文章

  1. 两个表的更新、表的复制
  2. Codeforces Global Round 13 E. Fib-tree
  3. 自定义Button防止重复提交
  4. 记账本开发小计(四)
  5. 在阿里云服务器中安装配置mysql数据库完整教程
  6. 学会Web前端,高薪工作任你挑!
  7. usercontroller.java,springboot controller 参数绑定
  8. NYOJ234吃土豆(双层动态规划)
  9. 微信浏览器(jssdk)自定义分享按钮,自定义链接,图片,描述等
  10. 微博直播场景下,如何实现百万并发的答题互动系统
  11. 阿里云短信申请流程以及配置
  12. MySQL查询优化方法
  13. uniapp实战项目 (仿知识星球App) - - 配置开发工具和全局css样式
  14. 我的世界java骷髅马_我的世界:骨灰级玩家指令召唤骷髅马,不必等雷劈!还能直接驯服!...
  15. 有了这6个东西之后,学Python还愁学不成?楼下大爷都入门了
  16. Mysql.cnf配置详解
  17. c语言怎么清空标准输入缓冲区,C语言中如何清空输入输出缓冲区
  18. 学习UE4动画蓝图:实现双脚贴地
  19. Python3 基础语法介绍
  20. take candy from a baby 唾手可得

热门文章

  1. 计算机网络——OSI参考模型和网络的排错
  2. 深度学习的实用层面 —— 1.3 机器学习基础
  3. leetcode - 94. 二叉树的中序遍历
  4. leetcode - 121.买卖股票的最佳时机
  5. Docker学习六:综合实践
  6. kaggle实战—泰坦尼克(五、模型搭建-模型评估)
  7. 蔡高厅老师 - 高等数学阅读笔记 - 16 定积分的应用(旋转积、平面曲线的弧长、阿基米德螺旋、旋转体的侧面积、定积分物理应用-变力做功) -(71、72、73)
  8. 编译:splint 遇到的问题: undefined reference to `yywrap'
  9. 未签名程序使用java_java applets(未签名)可以创建/读取cookie吗?
  10. linux系统优化 io,Linux硬件IO的优化简介