C# 彈出子視窗同時關閉主視窗

來源:互聯網
上載者:User

在這方面有過困惑,然後自己總結了一下。僅供參考,歡迎交流!

 

方法一:[實現從logInForm傳值到MainForm,且點LogIn出現MainForm的同時把LogInForm關掉]
在Program.cs裡:
static   void   Main(string[]   args)  
  {  
   
  LoginForm   lf   =   new   LoginForm();  
  if(lf.ShowDialog()   ==   DialogResult.OK)  
    {  
     Application.Run(new   MainForm());//開啟你指定的視窗   
    }  
  }  
在登入視窗(LoginForm)中  
的LogIn按鈕事件裡加上
 this.DialogResult=DialogResult.OK;   即可

//---------------------------------------------------------------------------------------

方法二:[實現從logInForm傳值到MainForm,且點LogIn出現MainForm的同時把LogInForm關掉]

在MainForm上添加一個名為txtMainForm的textBox
在LogInForm上添加一個名為txtLogInForm的textBox,和一個名為LogIn的Button

一).啟動MainForm
  static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        } 

二).在LogInForm裡添加一個公用屬性用於傳值:
 public partial class LogInForm : Form
    {
        private string valueSend = "";         //新增成員變數
        public string ValueSend                //添加屬性
        {
            get { return valueSend; }
        }

        private void LogIn_Click(object sender, EventArgs e)      //點擊LogIn Button觸發
        {
            valueSend = this.txtLogInForm.Text.Trim();   //設屬性的值為LogInForm的txtLogInForm輸

入的值
            this.Close();                                //關掉LogInForm
            this.DialogResult = DialogResult.OK;  
        }
    }

三).在MainForm裡
 在MainForm的Load事件中寫如下代碼[用於在MainForm Show出來前把LogInForm的值收到且主LogInForm關閉]
 private void MainForm_Load(object sender, EventArgs e) 
        {
            LogInForm lf = new LogInForm();      //在MainForm Show出來前,先把LogInForm

Show出來
            if (lf.ShowDialog() == DialogResult.OK)
            {
                this.txtMainForm.Text = lf.ValueSend;   //MainForm的textBox用於接收來自LogInForm

的textBox輸入的值.
            }
        }

大功告成,主要用到了屬性來傳值,用Form_Load在Form_Show前的特點實現了關閉LogInForm的同時把MainForm

Show出來了.

 

//---------------------------------------------------------------------------------------

方法三:[實現從logInForm傳值到MainForm,且點LogIn出現MainForm的同時把LogInForm關掉]
這方法是在學線程的時候意外發現的.相對比較簡單,也好理解.

在主form即Form1上的button1裡建立一線程,如下:
 private void btnThread1_Click(object sender, EventArgs e)
        {
            Thread threadOne = new Thread(new ThreadStart(FirstMethod));
            threadOne.Start();
        }

然後寫委託函數:
//這是因為:
        // ThreadStart是.Net Framework定義的委託類型,即:public delegate void ThreadStart();
        public void FirstMethod()     //
        {
            Form2 fm2 = new Form2();
            fm2.ShowDialog();
        }

就這麼簡單就完了,你對Form1[主線程]做任何操作都將不會影響到Form2上的東西(當然也包括關閉Form1).

如果你說:我不按button或別的,我就需要在關閉Form1的同時彈出Form2呢?
換位一下思維就能知道了,把button事件裡的代碼寫在Form1的FormClosed()裡不就可以了.
即:
 private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Thread threadTwo = new Thread(new ThreadStart(FirstMethod));
            threadTwo.Start();
        }
        public void FirstMethod()
        {

            Form2 fm2 = new Form2();
            fm2.ShowDialog();
        }

//---------------------------------------------------------------------------------------

 

 

 

 

 

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.