我正在尝试在面板内添加一个控件(Label).请看代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace AddControlProgramatically
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Label lbl = new Label();
for (int x = 0; x <= 3; x++)
{
//create new label location after each loop
//by multiplying the new value of variable x by 5, so the new label
//control will not overlap each other.
lbl.Location = new System.Drawing.Point(52 + (x * 5), 58 + (x * 5));
//create new id and text of the label
lbl.Name = "label_" + x.ToString();
lbl.Text = "Label " + x.ToString();
this.panel1.Controls.Add(lbl);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)

这是表格.我想要完成的是以编程方式生成3个不同的控制标签.但正如您所看到的,它只显示最后一个.请帮我解决这个问题.我知道我的代码有问题(导致它不能正常工作).谢谢......
放入Label lbl = new Label();循环内部.
并使偏移更大,改变这个......
lbl.Location = new System.Drawing.Point(52 + (x * 5), 58 + (x * 5))
Run Code Online (Sandbox Code Playgroud)
...至:
lbl.Location = new System.Drawing.Point(52 + (x * 30), 58 + (x * 30))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16329 次 |
| 最近记录: |