C# 我如何使用 Form.ShowDialog?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12444086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do i use Form.ShowDialog?
提问by Daniel Lip
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();
}
When i click the button i see the new Form and the textBox1 in the new Form and i can type in the textBox1 something but i dont see anywhere an OK or CANCEL buttons. Should i add them manualy in the new Form designer ? And how to use them then ?
当我单击按钮时,我会在新表单中看到新表单和 textBox1,我可以在 textBox1 中输入一些内容,但我在任何地方都看不到“确定”或“取消”按钮。我应该在新的表单设计器中手动添加它们吗?那么如何使用它们呢?
This is the code in my new Form what i wanted to do is to type something in the new Form textBox1 and pass the text in the textBox1 to Form1 label4.
这是我的新表单中的代码,我想要做的是在新表单 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
{
}
}
}
}
So where are the OK and CANCEL buttons of the Form.ShowDialog ?
那么 Form.ShowDialog 的 OK 和 CANCEL 按钮在哪里?
采纳答案by Mark Hall
You will need to add them yourself, you can add the buttons to your Formand set their DialogResultProperty. This will return the DialogResult and close the Form without you having to wire up any code. Here is an example using a Method to return the Value of The TextBox on Form2(There are two Buttons on Form2 with their DialogResults set to Cancel and Ok respectivly).
您需要自己添加它们,您可以将按钮添加到您的Form并设置它们的DialogResult属性。这将返回 DialogResult 并关闭表单,而无需连接任何代码。这是一个使用方法返回 Form2 上 TextBox 的值的示例(Form2 上有两个按钮,它们的 DialogResults 分别设置为 Cancel 和 Ok)。
Form1
表格1
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
表格2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public string getText()
{
return textBox1.Text;
}
}
回答by Fredou
what you want is the inputbox of visualbasic namespace and yes you can use it in c#
你想要的是visualbasic命名空间的输入框,是的,你可以在c#中使用它
回答by corn3lius
if you create an form from the basic form class you need to define a button that returns the DialogResultin the properties of the button.
如果从基本表单类创建表单,则需要定义一个按钮,该按钮返回按钮DialogResult属性中的 。
Those are most useful in the FileDialog, MessageBoxetc.. where the class is a MS defined form.
那些是在最有用的FileDialog,MessageBox等等。其中类是定义MS形式。
回答by Lorenz Lo Sauer
Given that your only tag is C#, and you expect an OK and CANCEL button, it seems to me that you are actually looking for the MessageBox function. Creating and disposing a Form just for the sake of showing a messagebox dialog is uncessary.
鉴于您唯一的标签是 C#,并且您希望有一个 OK 和 CANCEL 按钮,在我看来,您实际上是在寻找 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";
}
MessageBoxis a wrapper for the same-named WIN32 API Function:
MessageBox是同名 WIN32 API 函数的包装器:
int WINAPI MessageBox(
_In_opt_ HWND hWnd,
_In_opt_ LPCTSTR lpText,
_In_opt_ LPCTSTR lpCaption,
_In_ UINT uType
);
Note: If you already have a window-handle / Form make sure to pass it as the first parameter to MessageBox.
注意:如果您已经有一个窗口句柄/表单,请确保将其作为第一个参数传递给 MessageBox。

