| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3401 人关注过本帖
标题:怎么用Timer循环往窗体上添加控件
只看楼主 加入收藏
狂砍程序
Rank: 1
等 级:新手上路
帖 子:127
专家分:0
注 册:2007-9-27
结帖率:100%
收藏
 问题点数:0 回复次数:10 
怎么用Timer循环往窗体上添加控件

private void timer1_Tick(object sender, EventArgs e)
{

lb = new Label();
lb.Name = DateTime.Now.ToString();
lb.Text = newChar().ToString();

i = i + 1;
lb.Location = new Point(lb.Location.X + i * 2, lb.Location.Y);
this.Controls.Add(lb);
}

我这个方法只能添加一个,就不动了
?

搜索更多相关主题的帖子: Timer 控件 窗体 
2007-10-12 13:26
pacocai
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:1583
专家分:0
注 册:2007-3-12
收藏
得分:0 

控件名称都相同的怎么可能添加多个控件啊。

代码可以改成这样:
Button[] lb =new Label[100];

private void timer1_Tick(object sender, EventArgs e)
{
lb[i] = new Label();
lb[i].Name = DateTime.Now.ToString();
lb[i].Text = newChar().ToString();

i = i + 1;
lb.Location = new Point(lb.Location.X + i * 2, lb.Location.Y);
this.Controls.Add(lb);
}

这样可以动态的生成100个Label。要不还有一种方法就是直接写一个动态生成控件的类。我比较支持用类的方法来实现。


浮生若梦天边月,醉死如酒水中星。红楼一梦千人叹,岂让万夫空做贱。博客:http://hi.baidu.com/rxvip
2007-10-12 14:07
pacocai
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:1583
专家分:0
注 册:2007-3-12
收藏
得分:0 

类生成动态控件的代码:

form1.cs

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace DAddControl
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnRemove;
ButtonArray MyControlArray;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
MyControlArray = new ButtonArray(this);
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnAdd = new System.Windows.Forms.Button();
this.btnRemove = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(216, 0);
this.btnAdd.Name = "btnAdd";
this.btnAdd.TabIndex = 0;
this.btnAdd.Text = "Add Button";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnRemove
//
this.btnRemove.Location = new System.Drawing.Point(216, 240);
this.btnRemove.Name = "btnRemove";
this.btnRemove.TabIndex = 1;
this.btnRemove.Text = "Remove Button";
this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.btnRemove);
this.Controls.Add(this.btnAdd);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void btnAdd_Click(object sender, System.EventArgs e)
{
//from www.w3sky.com
//调用MyControlArray的AddNewButton方法
MyControlArray.AddNewButton();
//改变Button 0的BackColor属性
MyControlArray[0].BackColor = System.Drawing.Color.Red;
}

private void btnRemove_Click(object sender, System.EventArgs e)
{
MyControlArray.Remove();
}
}
}

ButtonArray.cs

using System;
using System.Windows.Forms;

namespace DAddControl
{
/// <summary>
/// ButtonArray 的摘要说明。
/// </summary>
public class ButtonArray
:System.Collections.CollectionBase
{
private readonly System.Windows.Forms.Form HostForm;

public ButtonArray()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

public ButtonArray(System.Windows.Forms.Form host)
{
HostForm = host;
//this.AddNewButton();
}

public System.Windows.Forms.Button AddNewButton()
{
//为Button类建立新的实例
System.Windows.Forms.Button aButton = new System.Windows.Forms.Button();
//将该按钮添加到集合的内部列表
this.List.Add(aButton);
//把控件集合中的按钮添加到被HostForm字段引用的窗体
HostForm.Controls.Add(aButton);
//设置该按钮对象的初始属性
aButton.Top = Count * 25;
aButton.Left = 100;
aButton.Tag = this.Count;
aButton.Text = "Button " + this.Count.ToString();
aButton.Click += new System.EventHandler(ClickHandler);
return aButton;
}

public System.Windows.Forms.Button this [int Index]
{
get
{
return (System.Windows.Forms.Button) this.List[Index];
}
}

public void Remove()
{
//检查以确保有按钮可以删除
if (this.Count > 0)
{
//from www.w3sky.com
// 从主窗体上的控件集合的数组按钮数组中删除最后一个
// 注意在访问数组时使用了默认属性
HostForm.Controls.Remove(this[this.Count -1]);
this.List.RemoveAt(this.Count -1);
}
}


public void ClickHandler(Object sender, System.EventArgs e)
{
System.Windows.Forms.MessageBox.Show("You have clicked button " +
((System.Windows.Forms.Button) sender).Tag.ToString());
}
}
}


浮生若梦天边月,醉死如酒水中星。红楼一梦千人叹,岂让万夫空做贱。博客:http://hi.baidu.com/rxvip
2007-10-12 14:10
crazymk
Rank: 1
等 级:新手上路
威 望:1
帖 子:343
专家分:0
注 册:2007-8-30
收藏
得分:0 
可以生成只不過兩個控件之間的距離太小都給覆蓋了。
用TextBox lb = new TextBox();能試驗出來。

すばらしいコードを書き出すのは楽しい事です
2007-10-12 14:18
pacocai
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:1583
专家分:0
注 册:2007-3-12
收藏
得分:0 
恩,应该也可能是这样。楼主的程序我没跑过,所以发言不太严谨,呵呵!楼主可以试下先把两个控件之间的间距调大试试,如果不行的话就用我提供的方法,当然间距也需要设大。基本上应该没什么问题了。

浮生若梦天边月,醉死如酒水中星。红楼一梦千人叹,岂让万夫空做贱。博客:http://hi.baidu.com/rxvip
2007-10-12 14:24
狂砍程序
Rank: 1
等 级:新手上路
帖 子:127
专家分:0
注 册:2007-9-27
收藏
得分:0 
哦,原来是这样啊
2007-10-12 14:34
bygg
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:乖乖的心中
等 级:版主
威 望:241
帖 子:13555
专家分:3076
注 册:2006-10-23
收藏
得分:0 
设置一下添加控件的left和top值

飘过~~
2007-10-13 14:06
virusswb
Rank: 1
等 级:新手上路
威 望:1
帖 子:342
专家分:0
注 册:2005-8-6
收藏
得分:0 
不是不动了,是你的方法问题啊,要设置控件的ID,位置,否则会重叠的

jorden008@
2007-10-13 17:20
april127
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2007-10-8
收藏
得分:0 
以下是引用pacocai在2007-10-12 14:07:41的发言:

控件名称都相同的怎么可能添加多个控件啊。

代码可以改成这样:
Button[] lb =new Label[100];

private void timer1_Tick(object sender, EventArgs e)
{
lb[i] = new Label();
lb[i].Name = DateTime.Now.ToString();
lb[i].Text = newChar().ToString();

i = i + 1;
lb.Location = new Point(lb.Location.X + i * 2, lb.Location.Y);
this.Controls.Add(lb);
}

这样可以动态的生成100个Label。要不还有一种方法就是直接写一个动态生成控件的类。我比较支持用类的方法来实现。

不错!

2007-10-14 00:54
梦心
Rank: 4
来 自:福建平和
等 级:贵宾
威 望:13
帖 子:1910
专家分:0
注 册:2007-5-11
收藏
得分:0 
添加的控件名称不能一样!
可以定义一个数组来区分名称

我清高和我骄傲的倔强,在风中大声的唱:我不听,我不听~~做我自己最特别,呼呼~~啦啦~~~
我的博客园地址: [url]http://[/url]
2007-10-14 11:24
快速回复:怎么用Timer循环往窗体上添加控件
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013011 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved