关于窗口跳转,一个很好的例子

关于窗口跳转,一个很好的例子 我们做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异常,还没
来得及测试


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值