关于C#两个窗体的问题

2024-12-04 20:33:32
推荐回答(5个)
回答1:

先运行F1.再运行F2,关闭F1.这是不允许的,F1是主程序,关闭了F1,程序就会退出了.
当然有办法变通,
比如隐藏F1,首先运行F1,然后运行F2,再把F1隐藏,
又或者
把F2作为主程序,在program 里面的star()中改为F2
在运行F2后,马上把F2隐藏掉,同时F2的Load方法中,或构造函数中调用F1,把F1显示出来.点击F1近钮后,关闭F1,显示F2
C#显示和隐藏的方法分别是:
form.show();
form.hide();
第一中方法的代码大约是:
F1 button的click代码:
form2 f2=new form2();
f2.show();
this.hide()

第二种方法代码大约是:
f2 load函数在最后写上
this.hide()
form1 f1=new form1();
if(f1.showdialog()==dialogResult.OK)
{
this.show();
f1.close();
}
再在f1的点击按钮上添加返回dialogResult=OK的代码

回答2:

一般情况下只能隐藏,你实在要关闭的话就要写一个类

using System;
using System.Windows.Forms;

namespace Splash.Windows.Forms
{
///


/// The SplashApplicationContext is used to show a splash Form before the main Form is shown.
///

public class SplashApplicationContext : ApplicationContext
{
private Timer myTimer;
private Form myMainForm;
private Form mySplashForm;

///
/// Initializes a new instance of SplashContext.
///
/// A default timeout of 2000 miliseconds is used.
///

/// The main Form of the application to use for context
/// The splash Form of the application to use for context
public SplashApplicationContext(Form mainForm, Form splashForm) : this(mainForm, splashForm, 2000)
{
}

///
/// Initializes a new instance of SplashContext
///

/// The main Form of the application to use for context
/// The splash Form of the application to use for context
/// The time (in milliseconds) the splash Form is visible. Specify 0 to disable the timeout.
public SplashApplicationContext(Form mainForm, Form splashForm, int timeout) : base(splashForm)
{
myMainForm = mainForm;
mySplashForm = splashForm;

// Initialize the SplashForm
splashForm.ShowInTaskbar = false;
splashForm.FormBorderStyle = FormBorderStyle.None;
splashForm.StartPosition = FormStartPosition.CenterScreen;

// Initialize the Timer
if (timeout > 0)
{
myTimer = new Timer();
myTimer.Interval = timeout;
myTimer.Tick += new EventHandler(TimerEventProcessor);
myTimer.Start();
}
}

private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
{
CloseSplashForm();
}

///
/// Close the splash Form
///

public void CloseSplashForm()
{
if (MainForm != myMainForm)
{
myTimer.Stop();
MainForm.Close();
}
}

protected override void OnMainFormClosed(object sender, EventArgs e)
{
if (MainForm == myMainForm)
{
base.OnMainFormClosed(sender, e);
}
else
{
MainForm = myMainForm;
MainForm.Show();
}
}

///
/// gets the splash Form
///

public Form SplashForm
{
get { return mySplashForm; }
}

}
}
在那个启动文件里面调用

回答3:

楼上的 ,谁说不允许了? 是你自己的能力不允许而已.

我这写的就是Form2运行后Form1关闭

...给你个简单点的.

在FORM1中写一个公共属性

private bool loginIsOKE;

public bool LoginIsOKE
{
get { return loginIsOKE; }
set { loginIsOKE = value; }
}

然后在登录按钮中写
this.loginIsOK = true;
this.Close();

然后在Programs.cs中写

注释掉: //Application.Run(new Form1());

然后写:
Form1 f1= new Form1();
if(f1.loginIsOK == true)
{
Application.Run(new Form2());
}
else
{
Application.Exit();
}

回答4:

在Form1的button1单击事件里面加句代码Form1.Close() 或者Form1.Hide();

回答5:

把from1作为参数传递给form2
然后再form2的Closing事件里调用form1的Update()