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