C#中怎么通过子窗体向父窗体中用代码生成的控件中传值 本帖最后由 liana_z 于 2011-03-14 14:29:40 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 新建子窗体时,把主窗体的面板传递到子窗体。。panel1子窗体点击:btn = new Button();btn.Parent = this.panel1;btn.Name = "btn" + i.ToString();btn.Text = "btn" + i.ToString();panel1.Controls.Add(btn);还有设置下location按钮位置```````` 同时可以设置.Text什么的内容 sglogin 你实现了啊。指点一下吧!先谢谢了 给个例子吧 public Label CreateLabel(int intLeft, int intTop, int intLength, string strName, string strText) { Label ctrlLabel = new Label(); ctrlLabel.ID = "lbl" + strName; ctrlLabel.Style.Add("LEFT", intLeft.ToString() + "px"); ctrlLabel.Style.Add("TOP", intTop.ToString() + "px"); ctrlLabel.Style.Add("Width", intLength.ToString() + "px"); ctrlLabel.Style.Add("POSITION", "absolute"); ctrlLabel.ToolTip = strText; ctrlLabel.Text = strText.Substring(0, 20); ; _PanelContainer.Controls.Add(ctrlLabel); return ctrlLabel; } 把新窗体要设置的参数做为属性,showdialog后,根据设置的内容在创建控件FrmParameter fPar = new FrmParameter();if (fPar.ShowDialog() == DialogResult.Ok){ if (fPar.ControlType =="Button") //ControlType为FrmParameter中声明的自定义属性 { Button btn = new Button(); ... this.xxx.Controls.Add(btn) }} 谢谢你,但是我真的没明白你想告诉我什么。我想要的是在子窗体内完成主窗体的控件的添加或者是能实现将主窗体代码添加的控件的部分参数的赋值弄个简单个。子窗体在主窗体动态建立按钮,在主窗体下建立一个方法//控件名,控件文本,宽度,高度,位置上,位置左public void AddBtn(string name,strint text,int width,int height,int top,int left){btn = new Button();btn.Parent = this.panel1;btn.Name = naem;btn.Text = text;btn.Width=width;btn.height=height;btn.top=top;btn.left=left;this.panel1.Controls.Add(btn);}主窗体在新建子窗体时把主窗体传递过去form1 f1=new form1(mdiform)f1 窗体上不是有新建控件的各参数文件本框;f1 点击时调用mdiform.AddBtn(textbox1.text,textbox2.text.....) #region Form3 form3 = new Form3(); form3.mainFrom = this; form3.Visible = true; PictureBox pic = new PictureBox(); pic.Name = "i"; pic.Left = 100; pic.Top = 20; pic.Width = 100; pic.Height = 100; //ColorDialog colorDialog = new ColorDialog(); //colorDialog.AllowFullOpen = true; //colorDialog.FullOpen = true; //colorDialog.ShowHelp = true; //colorDialog.Color = Color.Black;//初始化当前文本框中的字体颜色,当用户在ColorDialog对话框中点击\"取消\"按钮 //colorDialog.ShowDialog(); //pic.BackColor = colorDialog.Color; pic.BackColor = Color.Bisque; pic.Parent = this; pic.Location = new Point(10 + i, 10 + i); //显示位置 pic.MouseDown += new MouseEventHandler(pic_MouseDown); pic.MouseMove += new MouseEventHandler(pic_MouseMove); pic.MouseUp += new MouseEventHandler(pic_MouseUp); this.Controls.Add(pic); #endregion #region TextBox tb = new TextBox(); tb.Name = "ii"; tb.Left = 100; tb.Top = 20; tb.Width = 100; tb.ReadOnly = true; tb.Text = form3.textBox1.Text; //tb.BackColor = colorDialog.Color; tb.BackColor = Color.BurlyWood; //tb.Font= tb.Parent = this; tb.Location = new Point(20 + i, 20 + i); //显示位置 tb.MouseDown += new MouseEventHandler(pic_MouseDown); tb.MouseMove += new MouseEventHandler(pic_MouseMove); tb.MouseUp += new MouseEventHandler(pic_MouseUp); this.Controls.Add(tb); #endregion #region Label lab = new Label(); lab.Name = "ii"; lab.Left = 100; lab.Top = 20; lab.Width = 100; //lab.Height = 100; lab.Text = form3.textBox2.Text; //lab.BackColor = colorDialog.Color; lab.BackColor = Color.DarkViolet; //lab.Font= lab.Parent = this; lab.Location = new Point(30 + i, 30 + i); //显示位置 lab.MouseDown += new MouseEventHandler(pic_MouseDown); lab.MouseMove += new MouseEventHandler(pic_MouseMove); lab.MouseUp += new MouseEventHandler(pic_MouseUp); this.Controls.Add(lab); #endregion我的问题就是我在子窗体中的设置根本没管用 讲父窗口对象传给子窗口,子窗口Create控件,父窗口就会显示了。 弄个简单个。子窗体在主窗体动态建立按钮,在主窗体下建立一个方法//控件名,控件文本,宽度,高度,位置上,位置左public void AddBtn(string name,strint text,int width,int height,int top,int left){btn = new Button();btn.Parent = this.panel1;btn.Name = naem;btn.Text = text;btn.Width=width;btn.height=height;btn.top=top;btn.left=left;this.panel1.Controls.Add(btn);}主窗体在新建子窗体时把主窗体传递过去form1 f1=new form1(mdiform)f1 窗体上不是有新建控件的各参数文件本框;f1 点击时调用mdiform.AddBtn(textbox1.text,textbox2.text.....)8#这个还不行啊我怀具了。飘过~~~~~~~~~ 窗体问题 大家都用什么加密狗? C# 中二维数组 怎么取其中一维 简单获取panel中控件的id 各位使用 C# 的,提个问题!!! 一个Windows Service在什么情况下会异常结束? winfotrm 画图 C#中的Base是什么作用? 请求高手解答:在Windows Service中如何进行异步监听MSMQ消息? C# 窗体 求助:VS插件开发:将子菜单加入Project的右键菜单 求助:关于Web引用
btn.Parent = this.panel1;
btn.Name = "btn" + i.ToString();
btn.Text = "btn" + i.ToString();
panel1.Controls.Add(btn);还有设置下location按钮位置````````
{
Label ctrlLabel = new Label();
ctrlLabel.ID = "lbl" + strName;
ctrlLabel.Style.Add("LEFT", intLeft.ToString() + "px");
ctrlLabel.Style.Add("TOP", intTop.ToString() + "px");
ctrlLabel.Style.Add("Width", intLength.ToString() + "px");
ctrlLabel.Style.Add("POSITION", "absolute");
ctrlLabel.ToolTip = strText;
ctrlLabel.Text = strText.Substring(0, 20); ;
_PanelContainer.Controls.Add(ctrlLabel);
return ctrlLabel;
}
FrmParameter fPar = new FrmParameter();
if (fPar.ShowDialog() == DialogResult.Ok)
{
if (fPar.ControlType =="Button") //ControlType为FrmParameter中声明的自定义属性
{
Button btn = new Button();
...
this.xxx.Controls.Add(btn)
}
}
//控件名,控件文本,宽度,高度,位置上,位置左
public void AddBtn(string name,strint text,int width,int height,int top,int left)
{
btn = new Button();
btn.Parent = this.panel1;
btn.Name = naem;
btn.Text = text;
btn.Width=width;
btn.height=height;
btn.top=top;
btn.left=left;
this.panel1.Controls.Add(btn);
}主窗体在新建子窗体时把主窗体传递过去
form1 f1=new form1(mdiform)f1 窗体上不是有新建控件的各参数文件本框;
f1 点击时调用mdiform.AddBtn(textbox1.text,textbox2.text.....)
Form3 form3 = new Form3();
form3.mainFrom = this;
form3.Visible = true;
PictureBox pic = new PictureBox();
pic.Name = "i";
pic.Left = 100;
pic.Top = 20;
pic.Width = 100;
pic.Height = 100;
//ColorDialog colorDialog = new ColorDialog();
//colorDialog.AllowFullOpen = true;
//colorDialog.FullOpen = true;
//colorDialog.ShowHelp = true;
//colorDialog.Color = Color.Black;//初始化当前文本框中的字体颜色,当用户在ColorDialog对话框中点击\"取消\"按钮
//colorDialog.ShowDialog();
//pic.BackColor = colorDialog.Color;
pic.BackColor = Color.Bisque;
pic.Parent = this;
pic.Location = new Point(10 + i, 10 + i); //显示位置
pic.MouseDown += new MouseEventHandler(pic_MouseDown);
pic.MouseMove += new MouseEventHandler(pic_MouseMove);
pic.MouseUp += new MouseEventHandler(pic_MouseUp);
this.Controls.Add(pic);
#endregion
#region
TextBox tb = new TextBox();
tb.Name = "ii";
tb.Left = 100;
tb.Top = 20;
tb.Width = 100;
tb.ReadOnly = true;
tb.Text = form3.textBox1.Text;
//tb.BackColor = colorDialog.Color;
tb.BackColor = Color.BurlyWood;
//tb.Font=
tb.Parent = this;
tb.Location = new Point(20 + i, 20 + i); //显示位置
tb.MouseDown += new MouseEventHandler(pic_MouseDown);
tb.MouseMove += new MouseEventHandler(pic_MouseMove);
tb.MouseUp += new MouseEventHandler(pic_MouseUp);
this.Controls.Add(tb);
#endregion
#region
Label lab = new Label();
lab.Name = "ii";
lab.Left = 100;
lab.Top = 20;
lab.Width = 100;
//lab.Height = 100;
lab.Text = form3.textBox2.Text;
//lab.BackColor = colorDialog.Color;
lab.BackColor = Color.DarkViolet;
//lab.Font=
lab.Parent = this;
lab.Location = new Point(30 + i, 30 + i); //显示位置
lab.MouseDown += new MouseEventHandler(pic_MouseDown);
lab.MouseMove += new MouseEventHandler(pic_MouseMove);
lab.MouseUp += new MouseEventHandler(pic_MouseUp);
this.Controls.Add(lab);
#endregion我的问题就是我在子窗体中的设置根本没管用
父窗口就会显示了。
弄个简单个。子窗体在主窗体动态建立按钮,在主窗体下建立一个方法
//控件名,控件文本,宽度,高度,位置上,位置左
public void AddBtn(string name,strint text,int width,int height,int top,int left)
{
btn = new Button();
btn.Parent = this.panel1;
btn.Name = naem;
btn.Text = text;
btn.Width=width;
btn.height=height;
btn.top=top;
btn.left=left;
this.panel1.Controls.Add(btn);
}主窗体在新建子窗体时把主窗体传递过去
form1 f1=new form1(mdiform)f1 窗体上不是有新建控件的各参数文件本框;
f1 点击时调用mdiform.AddBtn(textbox1.text,textbox2.text.....)
8#这个还不行啊我怀具了。飘过~~~~~~~~~