.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
WinForm中类似WebForm中的CheckBoxList控件

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

前些天,在.Net技术的论坛里面看到了有个帖子,我好像记得是怎么实现WinForm中类似WebForm中的CheckBoxList控件,我简单的实现了那样的一个控件

首先,你得建立一个控件项目,假如说是:

接着,你就添加一个类:CheckBoxCollection,它是个CheckBox的集合类

具体的代码如下

CheckBoxCollection.cs

using System;
using System.Collections;
using System.Windows.Forms;

namespace CheckListControl
{
 /// <summary>
 /// CheckBox的集合类
 /// </summary>
 public class CheckBoxCollection:System.Collections.CollectionBase
 {
  public CheckBoxCollection()
  {
   IList pIList=base.List;
  }

  public CheckBox this[int index]
  {
   get
   {
    return (CheckBox) List[index];
   }
  }
  public CheckBox Add(CheckBox obj)
  {
   base.List.Add(obj);
   return obj;
  }

  public void Remove(CheckBox obj)
  {
   base.List.Remove(obj);
  }
 }
}

然后,在CheckBoxList.cs文件中,定义全局变量

private CheckBoxCollection objCbc=new CheckBoxCollection();

 public event System.EventHandler CheckedChanged;

写自定义函数,外部接口

 

/// <summary>
  /// 新增一个CheckBox到控件
  /// </summary>
  /// <returns></returns>
  public CheckBox NewCheckBox()
  {
   lab.Visible=false;
   CheckBox cb=new CheckBox();
   cb.Name=GetName();
   cb.Text=cb.Name;
//   cb.Size=new Size(120,24);
   cb.Checked=false;
   cb.Visible=true;
   cb.CheckedChanged+=new EventHandler(CheckBox_CheckedChanged);//定义CheckedChanged事件,来捕捉它的事件
   int y=0;
   y=objCbc.Count * 24 + objCbc.Count * 8 + 12;//形成CheckBox的纵坐标
   cb.Location=new Point(12,y);
   objCbc.Add(cb);

   this.Controls.Add(cb);//添加CheckBox到控件

   int x=GetMaxWidth();//得到已经添加的CheckBox中的最大的宽度
   
   if(cb.Width >x )//如果现在添加的CheckBox的最大宽度大于已经添加的最大宽度,替换调x
   {
    x = cb.Width + 24;
   }

   this.Size=new Size(x ,y +12+24);//根据添加的CheckBox改变控件的大小

   return cb;
  }

  /// <summary>
  /// 自动形成新添加CheckBox的名称
  /// </summary>
  /// <returns></returns>
  private string GetName()
  {
   if(objCbc.Count>0)
   {
    ArrayList list=new ArrayList();
    for(int i=0;i<objCbc.Count;i++)
    {
     if(objCbc[i].Name.Trim().Length==9)
     {
      string str=objCbc[i].Name.Trim();
      if(str.Substring(0,8).ToLower()=="checkbox" && IsNumber(str.Substring(str.Length-1,1)))
      {
       list.Add(str.Substring(str.Length-1,1));
      }
     }
    }
    if(list.Count>0)
    {
     return "checkBox" + Convert.ToString(int.Parse(list[list.Count-1].ToString().Trim()) + 1);
    }
   }

   return "checkBox1";
  }

  /// <summary>
  /// 判断是否是阿拉伯数字
  /// </summary>
  /// <param name="strCompare"></param>
  /// <returns></returns>
  private bool IsNumber(string strCompare)
  {
   string strWord="0123456789";
   foreach(char chr in strWord)
   {
    if(strCompare==chr.ToString())
    {
     return true;
    }
   }
   return false;
  }

  /// <summary>
  /// 得到已经添加CheckBox中的最大宽度
  /// </summary>
  /// <returns></returns>
  private int GetMaxWidth()
  {
   int maxWidth=0;
   if(objCbc.Count>0)
   {
    for(int i=0;i<objCbc.Count;i++)
    {
     CheckBox cb=(CheckBox)objCbc[i];
     if(cb.Width>maxWidth)
     {
      maxWidth=cb.Width;
     }
    }
   }
   return maxWidth;
  }

//  [Browsable(true), Description("得到CheckBox集合"), Category("CheckList")]
//  public CheckBoxCollection CheckList
//  {
//   get
//   {
//    return objCbc;
//   }
//  }

   private void CheckBox_CheckedChanged(object sender, EventArgs e)
  {
   CheckedChanged(sender,new EventArgs());
  }

 

编译以后,就可以得到CheckListControl.dll;

添加新项目用于类的测试

在工具箱中->添加/移除项,把CheckListControl添加进来,然后拖CheckListControl到Form1中

form1.cs中

private void Form1_Load(object sender, System.EventArgs e)
  {
   CheckBox cb=checkBoxList1.NewCheckBox();
   cb.Name="chkFirst";
   cb.Text="第一个CheckBox";
   cb.Size=new Size(125,24);
   cb=checkBoxList1.NewCheckBox();
   cb.Name="chkSecond";
   cb.Text="第二个CheckBox";
   cb.Size=new Size(125,24);
  }

  private void checkBoxList1_CheckedChanged(object sender, System.EventArgs e)
  {
   CheckBox cb=(CheckBox)sender;
   MessageBox.Show("Name: " + cb.Name + "  Text: " +cb.Text);
  }

具体的就这样

其实,只是作了简单的一个CheckBoxList,具体很多还有其它的功能没有加上,希望网友指正,添加更多功能




相关文章

相关软件