Chinaunix首页 | 论坛 | 博客
  • 博客访问: 362390
  • 博文数量: 94
  • 博客积分: 2500
  • 博客等级: 少校
  • 技术积分: 823
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-04 16:49
文章分类

全部博文(94)

文章存档

2015年(1)

2011年(1)

2010年(3)

2008年(8)

2007年(55)

2006年(26)

我的朋友

分类:

2007-09-21 18:26:56

本文将分三部分:
1。关于动态数组ArrayList;
2。动态添加控件;
3。以上两部分的综合;
 
1。关于动态数组ArrayList;
普通数组在声明的时候必须指定其容量大小,而ArrayList可动态变化大小,特别是在不知道最大值的时候可以使用ArrayList,因为普通数组在声明的时候必定指定了容量大小,这个大小值不一定合适,太大则浪费空间,小了又容易出现不够的情况,而ArrayList可随时改变大小。
注意,ArrayList也有个缺点,它的默认初始大小为16,如果其中的内容超过了此大小,则新分配的空间将成2的指数次方增长,如16,32,64,128……,可以使用TrimToSize()将其容量大小设置为实际数目。
ArrayList在使用性能上也会比数组低一些。
 
注意,使用ArrayList必须添加命名空间System.Collections;
 
ArrayList有3个实例化方法
1)new ArrayList();具有默认初始容量16
2)new ArrayList(c);
ICollection c:System.Collections.ICollection,它的元素被复制到新列表中。
3)new ArrayList(capacity); int capacity:初始容量。
 
其他方法:
向ArrayList中添加一个元素:abc.Add(...);
向ArrayList中添加一个数组:abc.AddRange(...);
 
ArrayList容量:abc.Capacit;
ArrayList中实际包含的元素数:abc.Count;
将ArrayList的容量调整为实际元素数目的大小:abc.TrimToSize();
 
返回整个ArrayList中第一个匹配项的从零开始的索引:abc.IndexOf(...);
返回整个ArrayList中最后一个匹配项的从零开始的索引:abc.LastIndexOf(...);
 
将元素插入 System.Collections.ArrayList 的指定索引处:abc.Insert(...);
将集合中的某个元素插入ArrayList 的指定索引处:abc.InsertRange(...);
 
删除元素:
abc.Remove(...);
abc.RemoveAt(...);
abc.RemoveRange(...);
从ArrayList 中移除所有元素:abc.Clear();
 
反转各元素的顺序:abc.Reverse();
排序:abc.Sort();
 
转化成普通数组:abc.ToArray();
 
 
使用ArrayList一开始遇到的困难是其中元素的使用。例如:定义了下面的 buttonArrayList:
private ArrayList buttonList = new ArrayList();
buttonList.Add(new Button());
但如果此时想使用Button的属性,是做不到的,如 buttonList[0].Text="OK",是使用不了的,因为buttonList[0]中没有Text属性,而且任何属于Button的属性和方法都不能通过buttonList[0]来调用。那怎样才能使用Button的属性和方法呢?就是先声明一个Button对象,然后将该对象的指针指向buttonList[0],然后通过这个对象来调用Button的属性和方法。如:
private Button button0;
button0 = (Button)buttonList[0];
button0.Text="button1";
button0 = (Button)buttonList[1];
button0.Text="button2";
等等。如果ArrayList中包含的是其他类型的元素,其使用方法也与此类似。
 
 
2。动态添加控件
动态添加控件的方法可仿照在VS.NET中设计窗体时自动生成的代码,在Form1.Designer.cs文件中。例如:要添加一个GroupBox容器并在其中添加一个comboBox控件。
//在窗体类中声明变量
private GroupBox groupBox1;
private ComboBox comboBox1;
//在构造函数的InitializeComponent()方法之后创建各对象并添加到窗体上
groupBox1=New GroupBox();
comboBox1=New ComboBox();
 
this.SuspendLayout();//在窗体或容器上绘制新的控件时必须先调用SuspendLayout()这个方法,在绘制结束后再调用ResumeLayout(false)和PerformLayout()两个方法。
groupBox1.SuspendLayout();
 
groupBox1.Controls.Add(comboBox1);//将comboBox控件添加到groupBox1容器
groupBox1.Location = new Point(12, 64);//相对于窗体左上角的位置
groupBox1.Size = new Size(466, 45);
groupBox1.Text = "Restriction";
 
comboBox1.FormattingEnabled = true;
comboBox1.Location = new Point(3, 15);//相对于父容器即groupBox1左上角的位置
comboBox1.Size = new Size(135, 20);
comboBox1.Text = "Select feilds";
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
 
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();//groupBox1上的控件添加完成后调用这两个方法
Controls.Add(groupBox1);//将groupBox1添加到窗体上
 
this.ResumeLayout(false);
this.PerformLayout();//窗体上的所有控件全部添加完成后调用这两个方法
 
3。以上两个两部分的综合;
例:在窗体上放置两个固定按钮addButton和delButton,其作用是动态添加Button控件数组,addButton添加一个按钮,delButton减少一个按钮,当点击不同的动态添加的按钮时显示其各自的名称。
 
    public partial class Form1 : Form
    {
        private ArrayList buttonList = new ArrayList();
        private Button button0;
        public Form1()
        {
            InitializeComponent();
        }

        //add a button
        private void addButton_Click(object sender, EventArgs e)
        {
            buttonList.Add(new Button());//列表中增加新的一项
            button0 = (Button)buttonList[buttonList.Count - 1];//用button0来获得其对象的应用
            //开始绘制按钮
            this.SuspendLayout();
            button0.Location = new Point(12, 12 + 35 * (buttonList.Count - 1));
            button0.Size = new Size(65, 23);
            button0.Text = "Button" + (buttonList.Count - 1);
            button0.Click += new EventHandler(button0_Click);//订阅事件。所有这样动态添加的按钮都订阅了同样一个事件button0_Click,但是不同按钮被点击之后做出的反应却是不一样的,这些判断和反应是在button0_Click中完成的。
            this.Controls.Add(button0);
            this.ResumeLayout(false);
            this.PerformLayout();
        }
        //del a button
        private void delButton_Click(object sender, EventArgs e)
        {
            if (buttonList.Count >= 1)//如果还可以继续删除
            {
                button0 = (Button)buttonList[buttonList.Count - 1];
                this.SuspendLayout();
                this.Controls.Remove(button0);
                buttonList.Remove(button0);
                this.ResumeLayout(false);
                this.PerformLayout();
            }
            else
                MessageBox.Show("There is no button!");
        }
        //button0事件:同样的代码对可不同的按钮做出不同的反应。判断的方法是判断点击事件的发出者。
        private void button0_Click(object sender, EventArgs e)
        {
            Button buttonSender = (Button)sender;//sender是object类型,使用前应先将其转换为对应的类型,这样才能调用具体的属性和方法。
            int i;//第i个按钮被按下
            for (i = 0; i < buttonList.Count; i++)
            {
                button0 = (Button)buttonList[i];
                if (buttonSender.Equals(button0))//判断点击事件的发出者是不是当前的button0。
                    break;
            }
            //此时得到了i的值,即被按下的按钮的顺序号
            MessageBox.Show("This is button" + i);
        }
    }
阅读(1962) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~