当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > C#,winform,ShowDialog,子窗体向父窗体传值

ASP.NET
FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用方法
.NET 常用功能和代码小结
在 .NET Framework 2.0 中未处理的异常导致基于 ASP.NET 的应用程序意外退出
asp.net IList查询数据后格式化数据再绑定控件
asp.net sql存储过程
asp.net 简单实现禁用或启用页面中的某一类型的控件
asp.net(c#)获取内容第一张图片地址的函数
The remote procedure call failed and did not execute的解决办法
ASP.NET 在线文件管理
asp.net 读取并修改config文件实现代码
ASP.NET Cookie 操作实现
asp.net Silverlight中的模式窗体
Silverlight中动态获取Web Service地址
asp.net Silverlight应用程序中获取载体aspx页面参数
asp.net 水晶报表隔行换色实现方法
asp.net 获取Gridview隐藏列的值
手动把asp.net的类生成dll文件的方法
asp.net 使用ObjectDataSource控件在ASP.NET中实现Ajax真分页
动态指定任意类型的ObjectDataSource对象的查询参数
asp.net Md5的用法小结

ASP.NET 中的 C#,winform,ShowDialog,子窗体向父窗体传值


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 180 ::
收藏到网摘: n/a

窗体的show方法,没有给调用代码任何通知,如果需要通知,使用showdialog是一种好的选择。 在调用show方法后,show方法后面的代码会立即执行, 调用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();
}
}
}