惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
Martin Fowler
Martin Fowler
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
博客园_首页
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
MongoDB | Blog
MongoDB | Blog
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
雷峰网
雷峰网
D
Docker
博客园 - 司徒正美
S
SegmentFault 最新的问题
M
MIT News - Artificial intelligence
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
U
Unit 42
J
Java Code Geeks
A
About on SuperTechFans
N
Netflix TechBlog - Medium
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security Affairs
I
Intezer
Cisco Talos Blog
Cisco Talos Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
B
Blog RSS Feed
P
Privacy & Cybersecurity Law Blog
T
Tenable Blog
T
Threatpost
H
Hacker News: Front Page
G
Google Developers Blog
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
L
Lohrmann on Cybersecurity
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
A
Arctic Wolf
S
Secure Thoughts
GbyAI
GbyAI
NISL@THU
NISL@THU
S
Security @ Cisco Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Webroot Blog
Webroot Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
O
OpenAI News
Spread Privacy
Spread Privacy
Application and Cybersecurity Blog
Application and Cybersecurity Blog

博客园 - 朱煜

项目经理与客户沟通的宜与忌 转 FileStream Read File filestream read方法 循环读取固定文件 c#描述异常处理语句try、catch、finally执行时的相互关系 C#计算文件的MD5值实例 C#获取文件MD5字符串 金旭亮老师的Scoekt编程摘要 多个文件合并成一个大文件后,无法打开,需要从以下几个方面找出问题? 腰围2尺1,2,3,4,5,6,7,8寸分别等于是多少厘米/英寸(对照表) 字符编码中的英文字母、汉字占有的字节长度。 Socket Programming in C#--Conclusion Socket Programming in C#--Server Side Socket Programming in C#--Multiple Sockets Socket Programming in C#--Getting Started Socket Programming in C#--Introduction C#中的is,as关键字 委托/事件与观察者模式 发布符合 .NET Framework 准则的事件(C# 编程指南) 在职研究生硕士学位证书查询和认证方式
Winfrom动态创建控件
朱煜 · 2013-11-07 · via 博客园 - 朱煜

FlowLayoutPanel flowLayoutPanel1 = new FlowLayoutPanel();
for (int i = 0; i < 9; i++)
{
    Button btn = new Button();
    btn.Height = 100;
    btn.Width = 100;
    btn.Cursor = Cursors.Hand;  
    btn.Name = btn.Text = "button" + (i + 1);

    flowLayoutPanel1.Dock = DockStyle.Left;
    flowLayoutPanel1.Width = 330;
    flowLayoutPanel1.Controls.Add(btn);
}
this.Controls.Add(flowLayoutPanel1);  

FlowLayoutPanel flowLayoutPanel1 = new FlowLayoutPanel();//为什么要添加这样一个控件?
FlowLayoutPanel是一个用于自动排列控件的panel,它会自动的排列在它里面的控件,默认是从左到右,从上到下,用这个控件,就是为了方便不用自己写代码控制button的位置

flowLayoutPanel1.Dock = DockStyle.Left;//这个又是什么意思啊?
这句是让FlowLayoutPanel控件停靠在主窗体上,停靠的方式并不是占满整个主窗体,而是靠左,也就是左边固定在窗体的最左边,右边的大小自己控制,为什么不全屏停靠是因为,这个控件无法让在它里面的控件自动换行,如果窗体很大,那一排就会自动放上多于3个button,所以需要手动设置FlowLayoutPanel的宽度,让它一排只能装下3个button,第4个就自动换到下一行了

this.Controls.Add(flowLayoutPanel1);//为什么要使用这个添加?
这句是将FlowLayoutPanel控件放到窗体中,动态创建的FlowLayoutPanel控件一开始并没有放到窗体中
就像动态创建的button,一开始并没有放到FlowLayoutPanel控件中一样,需要flowLayoutPanel1.Controls.Add(btn)把button放到FlowLayoutPanel中一样

事件的方法很简单,但事件有很多种,每一种的代码稍有点区别,你可以使用一个简便的方法,就是你先用静态的方式,就是弄一个button在设计器上,在事件窗口中双击创建一个事件,然后去Designer中找到自动添加的这行代码,然后复制到程序中,然后把界面还原回来。
如button的点击事件
this.button1.Click += new System.EventHandler(this.button1_Click);
// 控件名(也就是控件的Name,不是Text).Click += new System.EventHandler(响应事件的方法名)
然后就是创建响应方法,方法名要与注册事件时写的方法名一样
private void button1_Click(object sender, EventArgs e)
{
}
取消事件,跟注册事件一样,只是把+=换成-=
this.button1.Click -= new System.EventHandler(this.button1_Click);
程序执行过这一行代码后,就不会再响应点击事件了

像上面的动态的添加9个button的例子,你需要在for里面添加代码,不过要写在btn.Name = btn.Text = "button" + (i + 1);后面,因为要先赋了控件名以后,再添加事件
btn.Name.Click += new System.EventHandler(this.button1_Click);
这样9个button的点击都会进入到private void button1_Click(object sender, EventArgs e)这个方法