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

首頁 > 編程 > .NET > 正文

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

2020-01-18 01:34:04
字體:
來源:轉載
供稿:網友
調用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();
        }
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 体育| 苍溪县| 庆安县| 平潭县| 泰顺县| 佛教| 灵寿县| 龙海市| 双牌县| 吐鲁番市| 临高县| 景东| 南昌县| 新田县| 井冈山市| 三穗县| 丰都县| 商河县| 静宁县| 上栗县| 仁化县| 普洱| 万安县| 大理市| 新干县| 内黄县| 佳木斯市| 黎川县| 沂源县| 望奎县| 阳原县| 阳曲县| 阿坝| 于田县| 肇庆市| 浮山县| 天气| 称多县| 赞皇县| 汉阴县| 安塞县|