通过代码将按钮添加到我的表单
Add buttons by code to my form
我需要通过代码创建 x 个按钮,并在加载表单时将它们放入 groupBox 中,每个按钮都有自己的单击和右键单击事件 (contextMenuStrip)
我在此处看到了有关动态添加按钮的其他问题,它们只允许创建固定数量的按钮,但对我而言,每次打开表单时该数量都会有所不同。
如果不可能,请建议我另一种方法。
这是 vb.net 示例,但您可以使用一些在线转换工具轻松转换。
Dim lp As Integer = 0, tp As Integer = 0 'lp-left position, tp-top position in Your groupBox
For x = 1 to 20
Dim btn As New Button
btn.Name = "btn" + x.ToString
btn.Text = x.ToString
btn.Left = lp
btn.Top = tp
btn.Size = New Size(30, 30)
AddHandler btn.Click, AddressOf btnClick
groupBox.Controls.Add(btn)
tp += btn.Height + 10
Next
Private Sub btnClick(sender As Object, e As EventArgs)
Dim btn As Button = DirectCast(sender, Button)
'for example
If btn.Name = "btn1" Then
'do something if You click on first button, ...
End If
End Sub
这是如何动态创建 20 个按钮并将它们放入组框的基本示例...因此您可以根据需要调整按钮的位置和大小。使用 AddHandler
您可以定义右键单击等...您将看到所提供的内容。
在这个例子中,按钮将一个放在另一个下面,依此类推。按钮文本将是数字。把这个代码放在 Form_Load
.
并且,当您在 Form1_Load
下打开您的表单时,您可以定义您需要多少个按钮。
在这个例子中(运行 它在 LinqPAD 上)我使用 FlowLayout 容器自动将按钮放在您的表单表面上,并且对于创建的每个按钮我定义了一个自定义操作,当您的按钮是按下
void Main()
{
Form f = new Form();
FlowLayoutPanel fp = new FlowLayoutPanel();
fp.FlowDirection = FlowDirection.LeftToRight;
fp.Dock = DockStyle.Fill;
f.Controls.Add(fp);
// Get the number of buttons needed and create them in a loop
int numOfButtons = GetNumOfButtons();
for (int x = 0; x < numOfButtons; x++)
{
Button b = new Button();
// Define what action should be performed by the button X
b.Tag = GetAction(x);
b.Text = "BTN:" + x.ToString("D2");
b.Click += onClick;
fp.Controls.Add(b);
}
f.ShowDialog();
}
// Every button will call this event handler, but then you look at the
// Tag property to decide which code to execute
void onClick(object sender, EventArgs e)
{
Button b = sender as Button;
int x = Convert.ToInt32(b.Text.Split(':')[1]);
Action<int, string> act = b.Tag as Action<int, string>;
act.Invoke(x, b.Text);
}
// This stub creates the action for the button at index X
// You should change it to satisfy your requirements
Action<int, string> GetAction(int x)
{
return (int i, string s) => Console.WriteLine("Button " + i.ToString() + " clicked with text=" + s);
}
// Another stub that you need to define the number of buttons needed
int GetNumOfButtons()
{
return 20;
}
我需要通过代码创建 x 个按钮,并在加载表单时将它们放入 groupBox 中,每个按钮都有自己的单击和右键单击事件 (contextMenuStrip)
我在此处看到了有关动态添加按钮的其他问题,它们只允许创建固定数量的按钮,但对我而言,每次打开表单时该数量都会有所不同。
如果不可能,请建议我另一种方法。
这是 vb.net 示例,但您可以使用一些在线转换工具轻松转换。
Dim lp As Integer = 0, tp As Integer = 0 'lp-left position, tp-top position in Your groupBox
For x = 1 to 20
Dim btn As New Button
btn.Name = "btn" + x.ToString
btn.Text = x.ToString
btn.Left = lp
btn.Top = tp
btn.Size = New Size(30, 30)
AddHandler btn.Click, AddressOf btnClick
groupBox.Controls.Add(btn)
tp += btn.Height + 10
Next
Private Sub btnClick(sender As Object, e As EventArgs)
Dim btn As Button = DirectCast(sender, Button)
'for example
If btn.Name = "btn1" Then
'do something if You click on first button, ...
End If
End Sub
这是如何动态创建 20 个按钮并将它们放入组框的基本示例...因此您可以根据需要调整按钮的位置和大小。使用 AddHandler
您可以定义右键单击等...您将看到所提供的内容。
在这个例子中,按钮将一个放在另一个下面,依此类推。按钮文本将是数字。把这个代码放在 Form_Load
.
并且,当您在 Form1_Load
下打开您的表单时,您可以定义您需要多少个按钮。
在这个例子中(运行 它在 LinqPAD 上)我使用 FlowLayout 容器自动将按钮放在您的表单表面上,并且对于创建的每个按钮我定义了一个自定义操作,当您的按钮是按下
void Main()
{
Form f = new Form();
FlowLayoutPanel fp = new FlowLayoutPanel();
fp.FlowDirection = FlowDirection.LeftToRight;
fp.Dock = DockStyle.Fill;
f.Controls.Add(fp);
// Get the number of buttons needed and create them in a loop
int numOfButtons = GetNumOfButtons();
for (int x = 0; x < numOfButtons; x++)
{
Button b = new Button();
// Define what action should be performed by the button X
b.Tag = GetAction(x);
b.Text = "BTN:" + x.ToString("D2");
b.Click += onClick;
fp.Controls.Add(b);
}
f.ShowDialog();
}
// Every button will call this event handler, but then you look at the
// Tag property to decide which code to execute
void onClick(object sender, EventArgs e)
{
Button b = sender as Button;
int x = Convert.ToInt32(b.Text.Split(':')[1]);
Action<int, string> act = b.Tag as Action<int, string>;
act.Invoke(x, b.Text);
}
// This stub creates the action for the button at index X
// You should change it to satisfy your requirements
Action<int, string> GetAction(int x)
{
return (int i, string s) => Console.WriteLine("Button " + i.ToString() + " clicked with text=" + s);
}
// Another stub that you need to define the number of buttons needed
int GetNumOfButtons()
{
return 20;
}