子窗体中怎么调用父窗体中代码生成的按钮对象
子窗体中如何调用父窗体中代码生成的按钮对象?
在子窗体Form2_MouseClick事件中如调用父窗体中生成的按钮对象:如下
子窗体:
private void Form2_MouseClick(object sender, MouseEventArgs e)
{
btn.Location = new System.Drawing.Point(e.X, e.Y);
btn.Parent = this;
}
父窗体:
public void button1_Click(object sender, EventArgs e)
{
Button btn = new Button();
btn.Text = "测试";
}
为什么在字窗体中无法调用按钮对象btn
很急,那位朋友指点一下,不胜感激!
------解决方案--------------------
btn 是方法内的局部变量,外面访问不到。将btn在
public Button btn;
public void button1_Click(object sender, EventArgs e)
{
btn = new Button();
btn.Text = "测试";
}
在子窗体Form2_MouseClick事件中如调用父窗体中生成的按钮对象:如下
子窗体:
private void Form2_MouseClick(object sender, MouseEventArgs e)
{
btn.Location = new System.Drawing.Point(e.X, e.Y);
btn.Parent = this;
}
父窗体:
public void button1_Click(object sender, EventArgs e)
{
Button btn = new Button();
btn.Text = "测试";
}
为什么在字窗体中无法调用按钮对象btn
很急,那位朋友指点一下,不胜感激!
------解决方案--------------------
btn 是方法内的局部变量,外面访问不到。将btn在
public Button btn;
public void button1_Click(object sender, EventArgs e)
{
btn = new Button();
btn.Text = "测试";
}