当前位置:编程学习 > C#/ASP.NET >>

C#中如何让窗口自动跳转到另外一个窗口

如文章标题。。我想在我的程序开始之前显示我的form1然后三秒后自动跳转到我的form2(主程序) --------------------编程问答-------------------- 这里给出源代码:

Form1

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

namespace FormJumpSample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            timer1.Interval = 3000; //3秒
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Hide();
            Form2 form2Temp = new Form2(this);
            form2Temp.Show();
        }
    }
}


Form2

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

namespace FormJumpSample
{
    public partial class Form2 : Form
    {
        private Form1 _form1;

        public Form2(Form1 form1)
        {
            _form1 = form1;
            InitializeComponent();
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            _form1.Dispose();
        }
    }
}


这里为了Form2关闭后整个程序都能退出则通过构造函数把Form1传到了Form2并且在Form2关闭的时候也释放Form1资源。 --------------------编程问答-------------------- 建议楼主跳转时把timer关掉 --------------------编程问答-------------------- 我把Enable属性设成false了,通过start方法触发Tick事件。这样timer只会触发一次。 --------------------编程问答--------------------      form1的代码
        
public form1(){
           InitializeComponent();
        }
        private void form1_Load(object sender, EventArgs e) {

            Control.CheckForIllegalCrossThreadCalls = false;
            Thread t = new Thread(new ThreadStart(MySleep));
            t.Start();
        }
        public void MySleep() {
            Thread.Sleep(3000);
            this.Close();
        }


form2的代码

  
     public form1() {
            form1 myform1 = new form1();
            myform1 .ShowDialog();
            InitializeComponent();
        }


程序应先启动form2 
 Program.cs中应是
  Application.Run(new form2());
--------------------编程问答-------------------- 好意思上面代码把构造函数form2 写成form1了 改下

         public form2() {
            form1 myform1 = new form1();
            myform1 .ShowDialog();
            InitializeComponent();
        } --------------------编程问答-------------------- 还是觉得用线程好 --------------------编程问答--------------------
引用 1 楼 zhouqingprsc 的回复:
这里给出源代码:

Form1
C# codeusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace FormJumpSample
{publicpartialclass Form1 : Form
    {public Form1()
        {
            InitializeComponent();
        }privatevoid Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled=false;
            timer1.Interval=3000;//3秒            timer1.Start();
        }privatevoid timer1_Tick(object sender, EventArgs e)
        {this.Hide();
            Form2 form2Temp=new Form2(this);
            form2Temp.Show();        }
    }
}
变颜色的这一句,如果用showdialog()方法,不用show()方法,是不是就可以不显式调用Dispose()方法了 --------------------编程问答--------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            new frmLogo().ShowDialog();
        }

        class frmLogo : Form
        {
            Timer T = new Timer();

            public frmLogo()
            {
                this.Text = "LOGO!!!"; 
                T.Interval = 3000;
                T.Tick += new EventHandler(T_Tick);
                T.Enabled = true;
            }

            void T_Tick(object sender, EventArgs e)
            {
                this.Close(); 
            }
        }
    }
}
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,