我如何使用Form.ShowDialog?

Dan*_*Lip 10 c#

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

        }
Run Code Online (Sandbox Code Playgroud)

当我单击按钮时,我在新窗体中看到新窗体和textBox1,我可以输入textBox1,但我没有看到任何正常或取消按钮.我应该在新的表单设计器中手动添加它们吗?然后如何使用它们?

这是我的新表单中的代码,我想要做的是在新的表单textBox1中键入内容并将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
            {

            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

那么Form.ShowDialog的OK和CANCEL按钮在哪里?

Mar*_*all 24

您需要自己添加它们,您可以添加按钮Form并设置它们的DialogResult属性.这将返回DialogResult并关闭表单,而无需连接任何代码.下面是一个使用Method返回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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

窗体2

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    public string getText()
    {
        return textBox1.Text;
    }
}
Run Code Online (Sandbox Code Playgroud)


Lor*_*uer 6

鉴于您唯一的标签是 C#,并且您期望有一个“确定”和“取消”按钮,在我看来,您实际上正在寻找 MessageBox 函数。仅仅为了显示消息框对话框而创建和处理表单是不必要的。

if (MessageBox.Show("boxtext", "boxcaption" MessageBoxButtons.OKCancel) == DialogResult.OK) 
{
// Read the contents of testDialog's TextBox. 
// cl.AcceptButton.DialogResult = DialogResult.OK;
this.label4.Text = cl.textBox1Text;
}else
{
    this.label4.Text = "Cancelled";
}
Run Code Online (Sandbox Code Playgroud)

MessageBox是同名 WIN32 API 函数的包装器:

int WINAPI MessageBox(
  _In_opt_  HWND hWnd,
  _In_opt_  LPCTSTR lpText,
  _In_opt_  LPCTSTR lpCaption,
  _In_      UINT uType
);
Run Code Online (Sandbox Code Playgroud)

注意:如果您已经有窗口句柄/表单,请确保将其作为第一个参数传递给 MessageBox。