c# - 通过调用不同类的方法在表单中创建标签动态布局
问题描述
正如标题实现的那样,我如何调用在表单中添加控件的方法,比如 Form1。我想调用该方法并在内部创建几个控件,例如调用它的表单中的文本框或标签
解决方案
您可以在静态类中准备控件并在两种表单中使用它,如下所示
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Controls.Add(MyControls.AddLabel());
}
}
public static class MyControls
{
public static Control AddLabel()
{
Label label = new Label
{
AutoSize = true,
Location = new Point(48, 47),
Name = "label1",
Size = new Size(46, 17),
TabIndex = 1,
Text = "label1"
};
return label;
}
}
推荐阅读
- java - 查看 LINUX 服务器上的 JVM 日志文件位置
- python - 在相关模型实例中存储和检索字段的默认值
- php - Facebook oauth/access_token 响应中缺少“expires_in”
- javascript - 如何使用复选框显示和隐藏某些选项卡
- javascript - Expo Client:快速刷新不起作用(React Native)
- javascript - Grunt 错误“Grunt 尝试加载 .coffee 文件,但未安装 CoffeeScript。”
- javascript - React Native - firebase 的可选值
- css - BULMA:如何使第一列的宽度相等?仅通过使用表?
- python - 日期栏正在返回熊猫系列
- python - 带有 where 参数的意外 numpy sum 行为