当前位置: 移动技术网 > IT编程>开发语言>.net > C#,winform,ShowDialog,子窗体向父窗体传值

C#,winform,ShowDialog,子窗体向父窗体传值

2018年04月21日  | 移动技术网IT编程  | 我要评论

亭亭玉立逗朝阳,卡特十佳扣篮,上位练肉小棉袄

调用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();
        }
    }
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网