C#中,想在Form1中点击一个按钮弹出一个对话框,对话框可以设置参数
C#中,想在Form1中点击一个按钮弹出一个对话框,对话框可以设置参数,参数选好后点击确定,对话框退出,但是希望所选的参数保存下来,以方便Form1中使用。怎么写,最好有代码
//form1public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnSetting_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); if (frm2.ShowDialog() == DialogResult.OK) { double x = double.Parse(frm2.X); double y = double.Parse(frm2.Y); double z = double.Parse(frm2.Z); MessageBox.Show(x.ToString() + " " + y.ToString() + " " + z.ToString()); } } }
//form2
public partial class Form2 : Form { public string X { get; set; } public string Y { get; set; } public string Z { get; set; } public Form2() { InitializeComponent(); } private void btnOK_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtX.Text)) { MessageBox.Show("请输入X的值"); return; } if (string.IsNullOrEmpty(txtY.Text)) { MessageBox.Show("请输入Y的值"); return; } if (string.IsNullOrEmpty(txtZ.Text)) { MessageBox.Show("请输入Z的值"); return; } X = txtX.Text; Y = txtY.Text; Z = txtZ.Text; DialogResult = DialogResult.OK; } }
热门标签: