在WinForm中,怎么实现在一个窗体中向另一个窗体动态添加控件

在WinForm中,怎么实现在一个窗体中向另一个窗体动态添加控件,第1张

动态添加控件,自己要 *** 作的就是new一个控件出来,然后this.controls.add(控件)方法添加进去,利用Location属性确定其起始位置,用size属性确定其大小,附代码双击鼠标添加testbox

private void from1_DoubleClick(object sender, EventArgs e)

{

TextBox mytestbox = new TextBox()

mytestbox.Location = PointToClient(MousePosition)

mytestbox.Size = new Size(100,50)//自己调整

this.Controls.Add(mytestbox)//添加,添到哪里自己调整

}

打开Microsoft Visual Studio 2010

新建解决方案,

添加项目——动态生成控件

窗体布局

窗体设置合适的大小

确定控件的基本属性

1、假设要添加15个button按钮;

2、基本属性如下:

Name:名称 myButton

Text:显示文本 按钮(i)

Size:大小 50*50

Location:窗体位置

Location属性如何动态变化?(分析如图)

假设要把15个按钮,每5个一行生成,那么就需要让Location属性动态变化?怎么办?

经过分析,确定X的坐标为:50+i%5*100

Y的坐标如何动态确定?

1、定义一个行变量;

2、Y的坐标为:50+row*100

3、增加判断换行条件:i % 5 == 0 &&i != 0

输入代码试试看?ok!运行效果如图

if

(c

is

Label||c

is

RadioButton)

{

string

Str

=

((Label)c).Text

string

Stt

=

((RadioButton)c).Text

list.Add(Str)

list.Add(Stt)

}

这是有问题啊,得分别判断是label还是RadioButton

然后再添加到list中。而且一次只能循环一个控件也不能一次就能把两中同时都能添加进去啊

应该这样改吧,你参考一下:

if

(c

is

Label)

{

string

Str

=

((Label)c).Text

list.Add(Stt)

}

if

(c

is

RadioButton)

{

string

Stt

=

((RadioButton)c).Text

list.Add(Str)

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11312827.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-15
下一篇2023-05-15

发表评论

登录后才能评论

评论列表(0条)

    保存