如何并排动态添加控件? [英] How can I dynamically add a controls side by side?
本文介绍了如何并排动态添加控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何动态添加带有边距的PictureBox控件?
首选项:是否可以链接边距的大小(以像素为单位)到文本框的输入?
Hi, How do I dynamically add the PictureBox control side by side with a margin?
Preference: Could it be possible to link the size of the margin in pixels to a textbox's input?
推荐答案
我假设您正在使用WinForm
我创建了DemoForm并添加了它FlowLayoutPanel
(面板)和NumericUpDown
(numericUpDownMargin)
这里是一些代码
I assume that you are using WinForm
I have created DemoForm and added on itFlowLayoutPanel
(panel) andNumericUpDown
(numericUpDownMargin)
here is some code
public partial class DemoForm : Form
{
private List<PictureBox> pictures;
private int n = 16;
public DemoForm()
{
InitializeComponent();
// then form is resing, panel is also resizing and all picture boxes will change their location
panel.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
numericUpDownMargin.ValueChanged += MarginValueChanged;
pictures = new List<PictureBox>();
// adding picture boxes with images
for(int i=0; i<n; i++)
{
var picture = new PictureBox();
var bmp = new Bitmap(@"path to your image");
picture.Image = bmp;
picture.Size = new Size(150,150);
pictures.Add(picture);
panel.Controls.Add(picture);
picture.Visible = true;
}
}
// changing margin of all pictures boxes
private void MarginValueChanged(object sender, EventArgs e)
{
int margin = (int) numericUpDownMargin.Value;
panel.SuspendLayout();
foreach (var pictureBox in pictures)
pictureBox.Margin = new Padding(margin);
panel.ResumeLayout();
}
}
这篇关于如何并排动态添加控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文