如何动态在panel里面创建控件,并且得到创建控件的值 着急 请大家帮帮忙 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 类似下面代码: PictureBox pic = new PictureBox(); pic.Parent = panel1; pic.Dock = DockStyle.Fill; 楼上说的都可以,直接拖进去也可以,得到值就用document.getElement(""); 给你一个我以前写的方法,是动态添加radioButton的,你稍做修改就可以用了 /// <summary> /// 动态创建控件及事件处理程序 /// </summary> /// <param name="Datas">数据集</param> /// <param name="panel">Panel控件</param> /// <param name="rdoName">动态生成控件前缀名</param> private void InitComponents(string[] Datas, Panel panel, string rdoName) { panel.Controls.Clear(); int count = 0; RadioButton radioButton; foreach (string data in Datas) { radioButton = new RadioButton(); radioButton.Location = new System.Drawing.Point(20, count * 20); radioButton.Name = rdoName + Datas[count]; radioButton.Size = new System.Drawing.Size(150, 20); radioButton.TabIndex = count; radioButton.Text = Datas[count]; radioButton.Tag = Datas[count]; radioButton.UseVisualStyleBackColor = true; if (count == 0) radioButton.Checked=true; panel.Controls.Add(radioButton); count++; } } #region 生成控件 string lblGuiNamePrefix = "lblGui_"; string txtGuiNamePrefix = "txtGui_"; string lblDbNamePrefix = "lblDb_"; string txtDbNamePrefix = "txtDb_"; int last_index = 1; int left = 14; int top = 40; void CreateNewControl() { Label lbl = new Label(); lbl.Name = lblGuiNamePrefix + last_index.ToString(); lbl.Text = "显示值"; lbl.Top = top + 5; lbl.Left = left; lbl.Width = 60; this.Controls.Add(lbl); TextBox txtGui = new TextBox(); txtGui.Name = txtGuiNamePrefix + last_index.ToString(); txtGui.Text = ""; txtGui.Top = top; txtGui.Left = lbl.Right + 5; txtGui.Width = 80; this.Controls.Add(txtGui); Label lbl2 = new Label(); lbl2.Name = lblDbNamePrefix + last_index.ToString(); lbl2.Text = "数据库值"; lbl2.Top = top + 5; lbl2.Left = txtGui.Right + 5; lbl2.Width = 60; this.Controls.Add(lbl2); TextBox txtDb = new TextBox(); txtDb.Name = txtDbNamePrefix + last_index.ToString(); txtDb.Text = ""; txtDb.Top = top; txtDb.Left = lbl2.Right + 5; txtDb.Width = 80; txtDb.KeyDown += new KeyEventHandler(txtDb_KeyDown); this.Controls.Add(txtDb); last_index += 1; top += 30; btnCreate.Top = top; btnClear.Top = top; } void txtDb_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { CreateNewControl(); } } #endregion 加控件时:panel.Controls.Add(...得到值时:遍历panel.Controls C#键盘钩子问题 DataGridView和ListBox的问题 MS官网提供VS2010简体中文下载了。。。 请教谁用C# Winform做过画图程序? 面试题求解!~ 怎么链接到Access2007数据库上? 如何判断DataTable中是否存在某列? 存不存在一个存放menuItem的容器?不是mainMenu 急求哪位前辈帮我指点一下 C# vs2012设置问题 XAML设计器加载不了 大家帮帮忙,一个关于radiobutton的问题
pic.Parent = panel1;
pic.Dock = DockStyle.Fill;
/// <summary>
/// 动态创建控件及事件处理程序
/// </summary>
/// <param name="Datas">数据集</param>
/// <param name="panel">Panel控件</param>
/// <param name="rdoName">动态生成控件前缀名</param>
private void InitComponents(string[] Datas, Panel panel, string rdoName)
{
panel.Controls.Clear();
int count = 0;
RadioButton radioButton;
foreach (string data in Datas)
{
radioButton = new RadioButton();
radioButton.Location = new System.Drawing.Point(20, count * 20);
radioButton.Name = rdoName + Datas[count];
radioButton.Size = new System.Drawing.Size(150, 20);
radioButton.TabIndex = count;
radioButton.Text = Datas[count];
radioButton.Tag = Datas[count];
radioButton.UseVisualStyleBackColor = true;
if (count == 0)
radioButton.Checked=true;
panel.Controls.Add(radioButton);
count++;
}
}
string lblGuiNamePrefix = "lblGui_";
string txtGuiNamePrefix = "txtGui_";
string lblDbNamePrefix = "lblDb_";
string txtDbNamePrefix = "txtDb_"; int last_index = 1;
int left = 14;
int top = 40; void CreateNewControl()
{
Label lbl = new Label();
lbl.Name = lblGuiNamePrefix + last_index.ToString();
lbl.Text = "显示值";
lbl.Top = top + 5;
lbl.Left = left;
lbl.Width = 60;
this.Controls.Add(lbl); TextBox txtGui = new TextBox();
txtGui.Name = txtGuiNamePrefix + last_index.ToString();
txtGui.Text = "";
txtGui.Top = top;
txtGui.Left = lbl.Right + 5;
txtGui.Width = 80;
this.Controls.Add(txtGui); Label lbl2 = new Label();
lbl2.Name = lblDbNamePrefix + last_index.ToString();
lbl2.Text = "数据库值";
lbl2.Top = top + 5;
lbl2.Left = txtGui.Right + 5;
lbl2.Width = 60;
this.Controls.Add(lbl2); TextBox txtDb = new TextBox();
txtDb.Name = txtDbNamePrefix + last_index.ToString();
txtDb.Text = "";
txtDb.Top = top;
txtDb.Left = lbl2.Right + 5;
txtDb.Width = 80;
txtDb.KeyDown += new KeyEventHandler(txtDb_KeyDown);
this.Controls.Add(txtDb); last_index += 1;
top += 30; btnCreate.Top = top;
btnClear.Top = top;
} void txtDb_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
CreateNewControl();
}
}
#endregion
得到值时:遍历panel.Controls