关于窗口跳转,一个很好的例子 我们做winform时,有时需要做类似于窗口转换的需求,于 上一步、下一步、返回,或转到之前的窗口 想web页面跳转一样,这里我在codeproject看到一个好的例子,在这里和大家分享
public class Program
{
private static MainFormManager mainFormManager;
public static MainFormManager MainFormManager
{
get { return mainFormManager; }
}
public Program()
{
mainFormManager = new MainFormManager();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
mainFormManager.CurrentForm = new FormMain();
Application.Run(mainFormManager);
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
new Program();
}
}
我们先改写static void Main(),为了在转换窗体时,当前窗口关闭不退出应用程序,
然后新建一个控制类用来调用要转换的的窗体
public class MainFormManager : ApplicationContext
{
protected bool exitAppOnClose;
public Form CurrentForm
{
get {return MainForm;}
set
{
if (MainForm != null)
{
// close the current form, but don't exit the application
exitAppOnClose=false;
MainForm.Close();
exitAppOnClose=true;
}
// switch to the new form
MainForm=value;
MainForm.Show();
}
}
public MainFormManager()
{
exitAppOnClose=true;
}
// when a form is closed, bring up the first form in the array,
// or close the application.
protected override void OnMainFormClosed(object sender, EventArgs e)
{
if (exitAppOnClose)
{
base.OnMainFormClosed(sender, e);
}
}
}
这样,你只要写一句话在【下一步】按钮click事件
Program. MainFormManager.CurrentForm = new Form1();
就相当于form1.show()了
代码很简单有效,但我们要传对象给窗体时,还有跨线程操作子窗体可能有问题,要防止Dispose异常,还没
来得及测试