【问题标题】:How do I use Form.ShowDialog?如何使用 Form.ShowDialog?
【发布时间】:2012-09-08 18:34:06
【问题描述】:
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 按钮。我应该在新的表单设计器中手动添加它们吗?那么如何使用它们呢?

这是我的新表单中的代码,我想做的是在新表单 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
            {
               
            }
        }
    }
}

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

【问题讨论】:

    标签: c#


    【解决方案1】:

    您需要自己添加它们,您可以将按钮添加到您的Form 并设置它们的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();
            }
        }
    }
    

    Form2

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
    
        public string getText()
        {
            return textBox1.Text;
        }
    }
    

    【讨论】:

      【解决方案2】:

      鉴于您唯一的标记是 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";
      }
      

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

      int WINAPI MessageBox(
        _In_opt_  HWND hWnd,
        _In_opt_  LPCTSTR lpText,
        _In_opt_  LPCTSTR lpCaption,
        _In_      UINT uType
      );
      

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

      【讨论】:

        【解决方案3】:

        https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.form.dialogresult?view=net-5.0

        提供了显示“确定”和“取消”按钮如何链接的最佳方式 您需要手动添加 2 个按钮,例如 button1 & button2

            // Set the accept button of the form to button1.
              form1.AcceptButton = button1;
             // Set the cancel button of the form to button2.
               form1.CancelButton = button2;
        

        Acceptbutton,button1被认为是OK按钮。 CancelButton,button2被认为是Cancel按钮。

        坦率地说,没有一个答案明确提到这一点。 所以,我不得不提出这个答案。

        更进一步,你可以查看 MSDN 文档,

        https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.form.dialogresult?view=net-5.0

        【讨论】:

          【解决方案4】:

          你想要的是visualbasic命名空间的输入框,是的,你可以在c#中使用它

          Microsoft.VisualBasic.Interaction.InputBox

          【讨论】:

            【解决方案5】:

            如果您从基本表单类创建表单,您需要在按钮的属性中定义一个返回DialogResult 的按钮。

            这些在FileDialogMessageBox 等中最有用。其中类是MS 定义的形式。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-03-27
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多