面板 1 上的动态按钮 - 位置
dynamic buttons on panel1 - locations
所以我有一个带有可滚动面板的应用程序,它在面板上动态列出文件夹。在文件夹列表下方,我还有一个按钮。单击按钮后,它会在每个文件夹列表旁边动态创建一个按钮。我的每个按钮的 Y 值每次都增加 =+ 26 - 如果我的面板还不可滚动,这一切都很好。一旦面板变得可滚动并且我向下滚动然后继续添加更多按钮,它会跳转几个空格然后只添加更多按钮。我认为这与更改为 Y 位置的滚动有关?我该如何解决这个问题?
C# WinForms
感谢和问候。
private void button1_Click(object sender, EventArgs e)
{
Button newButton = new Button();
newButton.Location = new System.Drawing.Point(0, (y+6));
buttons.Add(newButton);
y += 20;
panel1.Controls.Add(newButton);
}
您可以尝试从最后一个按钮动态获取按钮的 y 位置:本例中为 (i-1),
private void button1_Click(object sender, EventArgs e)
{
Button newButton = new Button();
newButton.Location = new System.Drawing.Point(0, buttons[i -1].Location.Y + 26);
i++;
buttons.Add(newButton);
panel1.Controls.Add(newButton);
}
您可以使用 class 创建和管理按钮,例如
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
public class CreateButtons
{
/// <summary>
/// Size to create each button
/// </summary>
/// <returns></returns>
public Size ButtonSize { get; set; }
/// <summary>
/// Provides a reference to your buttons
/// </summary>
/// <returns></returns>
public List<Button> Buttons { get; set; }
/// <summary>
/// base name of buttone e.g. btn, cmd etc.
/// </summary>
/// <returns></returns>
public string ButtonBaseName { get; set; }
/// <summary>
/// Used to spread out buttons
/// </summary>
/// <returns></returns>
public int Base { get; set; }
public int BaseAddition { get; set; }
/// <summary>
/// Count of buttons
/// </summary>
/// <returns></returns>
public int ButtonCount { get; set; }
/// <summary>
/// control to place buttons onto
/// </summary>
/// <returns></returns>
public Control ParentControl { get; set; }
public CreateButtons(string BaseName)
{
this.ButtonBaseName = BaseName;
}
/// <summary>
/// Create single button and add it to the list of buttons property
/// </summary>
/// <param name="item"></param>
public void CreateSingleButton(string item)
{
if (Buttons == null)
{
Buttons = new List<Button>();
}
Button b = new Button
{
Name = string.Concat(ButtonBaseName, item),
Text = item,
Size = ButtonSize,
Location = new Point(25, this.Base),
Parent = ParentControl,
Visible = true
};
// wire up an event or have an event it gets wired too
b.Click += (object s, EventArgs e) =>
{
//Button tb = (Button)s;
//MessageBox.Show(tb.Text);
};
this.ParentControl.Controls.Add(b);
Buttons.Add(b);
Base += BaseAddition;
}
}
在带有滚动面板的表单设置中,一个列表框(显示我们可以访问所有创建的按钮),一个允许您通过为每个按钮命名来测试它的文本框。用于创建添加到列表中的单个按钮的按钮。
using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace CreateDynamicButtons1_cs
{
public partial class Form1 : Form
{
private CreateButtons buttons = new CreateButtons("btn");
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
buttons.ParentControl = panel1;
buttons.Base = 10;
buttons.BaseAddition = 35;
buttons.ButtonSize = new Size(50, 25);
}
private void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(textBox1.Text))
{
buttons.CreateSingleButton(textBox1.Text);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (buttons.Buttons != null)
{
listBox1.DataSource =
buttons.Buttons.Select(item => item.Name).ToList();
}
}
}
}
只需将您的动态按钮行替换为以下内容:
panel1.AutoScroll = true;
Button myButton = new Button();
myButton.Location = new Point(
panel1.AutoScrollPosition.X,
panel1.AutoScrollPosition.Y + (y+6));
y += 20;
panel1.Controls.Add(myButton);
所以我有一个带有可滚动面板的应用程序,它在面板上动态列出文件夹。在文件夹列表下方,我还有一个按钮。单击按钮后,它会在每个文件夹列表旁边动态创建一个按钮。我的每个按钮的 Y 值每次都增加 =+ 26 - 如果我的面板还不可滚动,这一切都很好。一旦面板变得可滚动并且我向下滚动然后继续添加更多按钮,它会跳转几个空格然后只添加更多按钮。我认为这与更改为 Y 位置的滚动有关?我该如何解决这个问题?
C# WinForms
感谢和问候。
private void button1_Click(object sender, EventArgs e)
{
Button newButton = new Button();
newButton.Location = new System.Drawing.Point(0, (y+6));
buttons.Add(newButton);
y += 20;
panel1.Controls.Add(newButton);
}
您可以尝试从最后一个按钮动态获取按钮的 y 位置:本例中为 (i-1),
private void button1_Click(object sender, EventArgs e)
{
Button newButton = new Button();
newButton.Location = new System.Drawing.Point(0, buttons[i -1].Location.Y + 26);
i++;
buttons.Add(newButton);
panel1.Controls.Add(newButton);
}
您可以使用 class 创建和管理按钮,例如
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
public class CreateButtons
{
/// <summary>
/// Size to create each button
/// </summary>
/// <returns></returns>
public Size ButtonSize { get; set; }
/// <summary>
/// Provides a reference to your buttons
/// </summary>
/// <returns></returns>
public List<Button> Buttons { get; set; }
/// <summary>
/// base name of buttone e.g. btn, cmd etc.
/// </summary>
/// <returns></returns>
public string ButtonBaseName { get; set; }
/// <summary>
/// Used to spread out buttons
/// </summary>
/// <returns></returns>
public int Base { get; set; }
public int BaseAddition { get; set; }
/// <summary>
/// Count of buttons
/// </summary>
/// <returns></returns>
public int ButtonCount { get; set; }
/// <summary>
/// control to place buttons onto
/// </summary>
/// <returns></returns>
public Control ParentControl { get; set; }
public CreateButtons(string BaseName)
{
this.ButtonBaseName = BaseName;
}
/// <summary>
/// Create single button and add it to the list of buttons property
/// </summary>
/// <param name="item"></param>
public void CreateSingleButton(string item)
{
if (Buttons == null)
{
Buttons = new List<Button>();
}
Button b = new Button
{
Name = string.Concat(ButtonBaseName, item),
Text = item,
Size = ButtonSize,
Location = new Point(25, this.Base),
Parent = ParentControl,
Visible = true
};
// wire up an event or have an event it gets wired too
b.Click += (object s, EventArgs e) =>
{
//Button tb = (Button)s;
//MessageBox.Show(tb.Text);
};
this.ParentControl.Controls.Add(b);
Buttons.Add(b);
Base += BaseAddition;
}
}
在带有滚动面板的表单设置中,一个列表框(显示我们可以访问所有创建的按钮),一个允许您通过为每个按钮命名来测试它的文本框。用于创建添加到列表中的单个按钮的按钮。
using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace CreateDynamicButtons1_cs
{
public partial class Form1 : Form
{
private CreateButtons buttons = new CreateButtons("btn");
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
buttons.ParentControl = panel1;
buttons.Base = 10;
buttons.BaseAddition = 35;
buttons.ButtonSize = new Size(50, 25);
}
private void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(textBox1.Text))
{
buttons.CreateSingleButton(textBox1.Text);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (buttons.Buttons != null)
{
listBox1.DataSource =
buttons.Buttons.Select(item => item.Name).ToList();
}
}
}
}
只需将您的动态按钮行替换为以下内容:
panel1.AutoScroll = true;
Button myButton = new Button();
myButton.Location = new Point(
panel1.AutoScrollPosition.X,
panel1.AutoScrollPosition.Y + (y+6));
y += 20;
panel1.Controls.Add(myButton);