怎样用其他类中的方法,来关闭主程序的窗体?(很急的问) 我是想调A.close();但是,我无法从B类的方法中获得A的对象:(高手如果有已经实现了的麻烦帖个完整的代码,先谢过啦! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不好意思,我还是没有试出来:(我把我大概的想法说一下:A是程序的主窗体(用默认的方法自动生成的),包含按钮a,B是子窗体,包含按钮b,需要点击a显示B,再点击b关掉主窗体A,我在B的构造函数里用A类的对象做参数,但是还是不能在b的点击事件中找到A的实例,报错说Null Reference,我怎样才能使用带有main()函数的那个类呀? A关了B也得完玩。方法一using System;using System.Windows.Forms;using System.IO;class Form1 : Form{ private Button btn; public static Form FHandle; public Form1() { initCompont(); } private void initCompont() { FHandle = this; this.Text = "Form1"; btn = new Button(); btn.Text = "Open Form2"; btn.Click += new EventHandler(this.btn_Click); this.Controls.Add(btn); } void btn_Click(object sender, EventArgs e) { Form2 form2 = new Form2(); //form2.Parent = this; form2.Show(); } static void Main() { Application.Run(new Form1()); }}class Form2 : Form{ private Button btn; public Form2() { initCompont(); } private void initCompont() { this.Text = "Form2"; btn = new Button(); btn.Text = "Close Form1"; btn.Click += new EventHandler(this.btn_Click); this.Controls.Add(btn); } void btn_Click(object sender, EventArgs e) { Form1.FHandle.Close(); }}方法二using System;using System.Windows.Forms;using System.IO;class Form1 : Form{ private Button btn; public Form1() { initCompont(); } private void initCompont() { this.Text = "Form1"; btn = new Button(); btn.Text = "Open Form2"; btn.Click += new EventHandler(this.btn_Click); this.Controls.Add(btn); } void btn_Click(object sender, EventArgs e) { Form2 form2 = new Form2(); form2.Opener = this; form2.Show(); } static void Main() { Application.Run(new Form1()); }}class Form2 : Form{ private Button btn; public Form Opener; public Form2() { initCompont(); } private void initCompont() { this.Text = "Form2"; btn = new Button(); btn.Text = "Close"; btn.Click += new EventHandler(this.btn_Click); this.Controls.Add(btn); } void btn_Click(object sender, EventArgs e) { this.Opener.Close(); }} 多谢,能留个email吗?以后好请你喝酒:) RS232远程连结问题 【求助】如何在文本框中查看网页源码 c#程序cpu资源过大如何解决依个问题? 打印机控制切刀问题 关于UltrawinGrid 表头问题!很急…… 流量控制问题 如何把浮点数3.333333转换为浮点数3.33 VB 里面 &H8000& 是什么类型的数据 在C#怎么定义?? 怎么在原先List元素上进行加减操作后返回结果? 一个窗体的问题 请大家评评此书! 请教字符串分段的算法
我把我大概的想法说一下:
A是程序的主窗体(用默认的方法自动生成的),包含按钮a,
B是子窗体,包含按钮b,
需要点击a显示B,再点击b关掉主窗体A,我在B的构造函数里用A类的对象做参数,但是还是不能在b的点击事件中找到
A的实例,报错说Null Reference,
我怎样才能使用带有main()函数的那个类呀?
using System;
using System.Windows.Forms;
using System.IO;
class Form1 : Form
{
private Button btn;
public static Form FHandle;
public Form1()
{
initCompont();
}
private void initCompont()
{
FHandle = this;
this.Text = "Form1";
btn = new Button();
btn.Text = "Open Form2";
btn.Click += new EventHandler(this.btn_Click);
this.Controls.Add(btn);
}
void btn_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
//form2.Parent = this;
form2.Show();
}
static void Main()
{
Application.Run(new Form1());
}
}
class Form2 : Form
{
private Button btn;
public Form2()
{
initCompont();
}
private void initCompont()
{
this.Text = "Form2";
btn = new Button();
btn.Text = "Close Form1";
btn.Click += new EventHandler(this.btn_Click);
this.Controls.Add(btn);
}
void btn_Click(object sender, EventArgs e)
{
Form1.FHandle.Close();
}
}方法二
using System;
using System.Windows.Forms;
using System.IO;
class Form1 : Form
{
private Button btn;
public Form1()
{
initCompont();
}
private void initCompont()
{
this.Text = "Form1";
btn = new Button();
btn.Text = "Open Form2";
btn.Click += new EventHandler(this.btn_Click);
this.Controls.Add(btn);
}
void btn_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Opener = this;
form2.Show();
}
static void Main()
{
Application.Run(new Form1());
}
}
class Form2 : Form
{
private Button btn;
public Form Opener;
public Form2()
{
initCompont();
}
private void initCompont()
{
this.Text = "Form2";
btn = new Button();
btn.Text = "Close";
btn.Click += new EventHandler(this.btn_Click);
this.Controls.Add(btn);
}
void btn_Click(object sender, EventArgs e)
{
this.Opener.Close();
}
}