- private void button2_Click(object sender,EventArgs e)
- {
- ChangeLink cl = new ChangeLink();
- // Show testDialog as a modal dialog and determine if DialogResult = OK.
- if (cl.ShowDialog() == DialogResult.OK)
- {
- // Read the contents of testDialog's TextBox.
- // cl.AcceptButton.DialogResult = DialogResult.OK;
- this.label4.Text = cl.textBox1Text;
- }
- else
- {
- this.label4.Text = "Cancelled";
- }
- cl.Dispose();
- }
当我点击按钮,我看到新的窗体和textBox1在新窗体,我可以输入textBox1的东西,但我没有看到任何一个OK或CANCEL按钮.我应该在新的表格设计师手册中添加它们吗?那么怎么用呢?
这是我的新表单中的代码,我想做的是在新的FormBox1中键入一些内容,并将textBox1中的文本传递给Form1 label4.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace GatherLinks
- {
- public partial class ChangeLink : Form
- {
- public ChangeLink()
- {
- InitializeComponent();
- }
- public string textBox1Text
- {
- get
- {
- return textBox1Text = textBox1.Text;
- }
- set
- {
- }
- }
- }
- }
那么Form.ShowDialog的OK和CANCEL按钮在哪里?
解决方法
您将需要自己添加,您可以将按钮添加到窗体并设置其
DialogResult
属性.这将返回DialogResult并关闭窗体,而无需连接任何代码.以下是使用方法返回Form2上的TextBox的值的示例(Form2上有两个按钮,其DialogResults设置为Cancel并且Ok).
Form1中
- public partial class Form1 : Form
- {
- Form2 frm2;
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender,EventArgs e)
- {
- frm2 = new Form2();
- DialogResult dr = frm2.ShowDialog(this);
- if (dr == DialogResult.Cancel)
- {
- frm2.Close();
- }
- else if (dr == DialogResult.OK)
- {
- textBox1.Text = frm2.getText();
- frm2.Close();
- }
- }
- }
窗体2
- public partial class Form2 : Form
- {
- public Form2()
- {
- InitializeComponent();
- }
- public string getText()
- {
- return textBox1.Text;
- }
- }