C# 自定义对话框_笔记

新建一个项目,添加Form2。

设置如下属性:


this.ControlBox = false;


this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;


this.MaximizeBox = false;


this.MinimizeBox = false;

添加如下两个按钮

将画面属性中两个属性做如下设置:

this.AcceptButton = this.button1;

this.CancelButton = this.button2;

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace WindowsFormsApplication23

{


public
partial
class
Form2 : Form

{


public
string s1 = null;


public Form2()

{

InitializeComponent();

}

 


private
void button1_Click(object sender, EventArgs e)

{


this.s1 = this.textBox1.Text;


this.DialogResult = DialogResult.OK;

}

 


private
void button2_Click(object sender, EventArgs e)

{


this.DialogResult = DialogResult.Cancel;

}

}

}

Form2做如下设置

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace WindowsFormsApplication23

{


public
partial
class
Form1 : Form

{


public Form1()

{

InitializeComponent();

}

 


private
void button1_Click(object sender, EventArgs e)

{


Form2 f2 = new
Form2();


if(f2.ShowDialog()==DialogResult.OK)

{


this.Text = f2.s1;

}


else

{


this.Text = null;

}

f2.Dispose();

}

}

}

徐大军

发表评论

电子邮件地址不会被公开。 必填项已用*标注