這應該讓你走。
將此代碼添加到您的表單。你可以使用這個代碼來做任何你想要的按鈕或任何東西。但是你也許應該閱讀FlowLayoutPanel或者GroupBox來讓它在現實中工作。
Point _imagePos = new Point(10,10);
int _imageCounter = 1;
private void NewPictureBox(string pathToImg, string imageName)
{
var img = new PictureBox
{
Name = "imageBox" + _imageCounter,
ImageLocation = pathToImg,
Left = _imagePos.X,
Top = _imagePos.Y,
SizeMode = PictureBoxSizeMode.StretchImage,
Height = 50,
Width = 50
};
var txt = new TextBox
{
Text = imageName,
Left = _imagePos.X,
Top = img.Bottom + 10
};
this.Controls.Add(img);
this.Controls.Add(txt);
_imageCounter++;
_imagePos.Y += 10 + img.Height + txt.Height;
}
private void Form1_Load(object sender, EventArgs e)
{
NewPictureBox(@"C:\test\QuestionMark.jpg", "image1");
NewPictureBox(@"C:\test\QuestionMark.jpg", "image2");
}