国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

C#,winform,ShowDialog,子窗體向父窗體傳值

2024-07-10 13:22:13
字體:
來源:轉載
供稿:網友
調用showdialog方法后,調用代碼被暫停執行,等到調用showdialog方法的窗體關系后再繼續執行。而且窗體可以返回一個dialogresult值,他描述了窗體關閉的原因,例如OK,Cancel,yes,no等。為了讓窗體返回一個dialogresult,必須設置窗體的dialogresult值,或者在窗體的一個按鈕上設置dialogresult屬性。

例子:
下面是子窗體代碼,要求輸入phone,然后會返回給父窗體。

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

namespace WindowsApplication1
{
    public partial class Phone : Form
    {
        public Phone()
        {
            InitializeComponent();
            btnOK.DialogResult = DialogResult.OK;
            btnOK.DialogResult = DialogResult.Cancel;
        }
        public string PhoneNumber
        {
            get { return textBox1.Text; }
            set { textBox1.Text = value; }
        }
        private void Phone_Load(object sender, EventArgs e)
        {

        }
    }
}

不包含任何處理按鈕單擊事件的代碼,因為設置了每個按鈕的dialogresult屬性,所以單擊OK或者Cancel按鈕后,窗體就消失了。下面的代碼顯示了父窗體中調用Phone對話框的方法。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form7 : Form
    {
        public Form7()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Phone frm = new Phone();
            frm.ShowDialog();
            if (frm.DialogResult == DialogResult.OK)
            {
                label1.Text = "Phone number is " + frm.PhoneNumber;

            }
            else if (frm.DialogResult == DialogResult.Cancel)
            {
                label1.Text = "form was canceled";

            }
            frm.Close();
        }
    }
}

看起來非常簡單,創建新的Phone對象frm,在調用frm.showdialog方法是,代碼停止,等待phone窗體返回,接著檢查phone窗體的dialogresult屬性,由于窗體還沒有釋放,是不可見的,所以仍可以訪問公共屬性phonenumber,一旦獲取了需要的數據,就可以嗲用窗體的close方法。
一切正常,但是如果返回的格式不正確怎么辦,就要把showdialog方法放在循環中,就可以再次調用,讓用戶重新輸入,就可以得到正確的值。

上面的代碼改成下面的即可。

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

namespace WindowsApplication1
{
    public partial class Form7 : Form
    {
        public Form7()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Phone frm = new Phone();

            while (true)
            {
                frm.ShowDialog();
                if (frm.DialogResult == DialogResult.OK)
                {
                    label1.Text = "Phone number is " + frm.PhoneNumber;
                    if (frm.PhoneNumber.Length == 8 || frm.PhoneNumber.Length == 12)
                    {
                        break;
                    }
                    else
                    {
                        MessageBox.Show("");
                    }
                }
                else if (frm.DialogResult == DialogResult.Cancel)
                {
                    label1.Text = "form was canceled";
                    break;
                }
            }
            frm.Close();
        }
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 灌云县| 休宁县| 临桂县| 逊克县| 陆川县| 抚顺县| 昭觉县| 吉水县| 和政县| 乡城县| 武山县| 定陶县| 桃江县| 高邑县| 平和县| 桃江县| 海林市| 哈密市| 南充市| 织金县| 安泽县| 德钦县| 沐川县| 金塔县| 浦县| 莱西市| 沁阳市| 江安县| 九台市| 新和县| 库伦旗| 舟曲县| 西充县| 洪泽县| 时尚| 屯昌县| 龙海市| 拜泉县| 永宁县| 虹口区| 西畴县|