天天看點

C#中在主窗體中用ShowDialog方法顯示子窗體的使用技巧

顯示和關閉子窗體:

方法1:源代碼: EatGood.FoodManageUI.MessageForm.ConfirmDelFeelkindOrFeelsForm feelkindform = new MessageForm.ConfirmDelFeelkindOrFeelsForm(); //建立一個窗體對象

feelkindform.ShowDialog() ; '//顯示窗體

 子窗體中按鈕屬性設定: button1.DialogResult = DialogResult.OK;    button2.DialogResult = DialogResult.Cancel;

方法2:源代碼: EatGood.FoodManageUI.MessageForm.ConfirmDelFeelkindOrFeelsForm feelkindform = new MessageForm.ConfirmDelFeelkindOrFeelsForm(); //建立一個窗體對象

feelkindform.ShowDialog() ; '//顯示窗體

子窗體按鈕事件:

 private void button1_Click(object sender, EventArgs e)

        {

            this.Close();

        }

        private void button2_Click(object sender, EventArgs e)

        {

            this.Close();

        }

方法1與方法2結合

例子1:①在主窗體中點選删除按鈕,②彈出删除窗體 ,提示是否删除該記錄,③點選删除窗體的确定按鈕,執行删除該記錄的代碼,并且關閉該删除窗體,

④點選删除窗體中取消按鈕關閉該删窗體 要實作這種效果的代碼:

首先:設定删除窗體:确定按鈕的屬性:Dialogresult為OK,取消按鈕的屬性:Dialogresult為Cancel

源代碼1: EatGood.FoodManageUI.MessageForm.ConfirmDelFeelkindOrFeelsForm feelkindform = new MessageForm.ConfirmDelFeelkindOrFeelsForm(); //建立一個窗體對象

     if (feelkindform.ShowDialog() == DialogResult.Cancel) //點選取消 

       {

      //取消按鈕中自己需要的操作

    //

       }   

      // 确定按鈕中的操作

      //

 例子1是彈出删除提示窗體的典型代表:

轉載于:https://www.cnblogs.com/a1991322/archive/2012/11/21/2780782.html

c#