传输数据到处理程序
问题描述:
我有Form1与Form1.cs文件,它调用Helpers.cs中的方法。该方法将引起表单的实例作为参数,然后创建按钮和文本框并为按钮分配处理程序。 ?如何按钮处理程序启动时传递文本框的文本值处理方法Helpers.cs有这样的方法:传输数据到处理程序
public static void startpage(Form form)
{
try
{
var Tip = new Label() { Text = "Input instance name",
Location = new Point(50, 50), AutoSize = true };
var StartConnection = new LinkLabel() { Text = "Connect",
Location = new Point(50, 100), AutoSize = true};
var InstanceInput = new TextBox() { Text = "INSTANCENAME",
Location = new Point(100, 70), MaxLength = 1000, Width = 200,
BorderStyle=BorderStyle.FixedSingle};
StartConnection.Click += new EventHandler(nextpage);
Helpers.AddControlsOnForm(form,
new Control[] {Tip,StartConnection,InstanceInput });
}
catch(Exception ex)
{ MessageBox.Show("Error occured. {0}",ex.Message.ToString()); }
}
public static void nextpage(Object sender, EventArgs e)
{
//I want to work with instance name and form there
}
答
最简单的方法是将文本框实例连接到LinkLabel控件的Tag
财产和访问它处理程序:
public static void startpage(Form form)
{
try
{
var Tip = new Label() { Text = "Input instance name",
Location = new Point(50, 50), AutoSize = true };
var InstanceInput = new TextBox() { Text = "INSTANCENAME",
Location = new Point(100, 70), MaxLength = 1000, Width = 200,
BorderStyle=BorderStyle.FixedSingle};
var StartConnection = new LinkLabel() { Text = "Connect",
Location = new Point(50, 100), AutoSize = true, Tag = InstanceInput };
StartConnection.Click += new EventHandler(nextpage);
Helpers.AddControlsOnForm(form,
new Control[] {Tip,StartConnection,InstanceInput });
}
catch(Exception ex)
{ MessageBox.Show("Error occured. {0}",ex.Message.ToString()); }
}
public static void nextpage(Object sender, EventArgs e)
{
var text = ((sender as LinkLabel).Tag as TextBox).Text;
}
在任何情况下,你要么必须存储实例某处(在这种情况下,在标签属性)或搜索窗体的Controls集合,然后找到所需的控制。
显示你的工作..人们无法阅读你的想法。 – 2013-04-28 11:43:30
你可以看到代码。 – Wido 2013-04-28 12:06:28