第一个窗体关闭打开第二个窗体

King_NaNa 2008-01-26 03:20:23
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace tree
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form2());//先启动2号窗体
}
}
}
************************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace tree
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{
Form1 f1 = new Form1(this);//实例1号窗体,那里面的this有用什么用呢?
f1.ShowDialog(this);//这个地方什么要用ShowDialog,为什么要用Show(this)就不行呢?
}
}
}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace tree
{
public partial class Form1 : Form
{
Form2 f2;
public Form1(Form2 f2)
{
this.f2 = f2;//这样有什么用处呢?
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.Close();
}

private void button2_Click(object sender, EventArgs e)
{
this.textBox1.Clear();
this.textBox2.Clear();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
f2.Show();
this.Close();
}
}
}
...全文
166 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
eyesinbook 2008-01-26
  • 打赏
  • 举报
回复
Form1 f1 = new Form1(this);//实例1号窗体,那里面的this有用什么用呢?
==================================================================
加this 是因为 From1中自定义的的构造函数是带参数的,这里的This表示Form2对象



f1.ShowDialog(this);//这个地方什么要用ShowDialog,为什么要用Show(this)就不行呢?
==============================================================================
这里有两个窗体:From1和From2,把个作为模式对话框来显示,运行的时候就只显示一个窗体了



this.f2 = f2;//这样有什么用处呢?
======================================================
构造函数的基本写法,为该类的属性赋值

sunshine_anycall 2008-01-26
  • 打赏
  • 举报
回复
Form2 是 Form1的一个成员实例,所以通过Form1的属性可以控制Form2

再Form2的加载函数中,首先执行的是Form1的加载。也就是要加载Form2就必加载Form1。
再Form1关闭的时候,也就是再Form_closing中Form2显示出来,Form1关闭。
Cubar 2008-01-26
  • 打赏
  • 举报
回复
在这里是把Form2当作一个Form1的一个属性,这样的话就可以在Form1里操作Form2了!

110,568

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧