C# 创建返回值的自定义对话框的最简单方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9569489/
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
Easiest way to create a custom dialog box which returns a value?
提问by Ahmad
I want to create a custom dialog box for my C# project .. I want to have a DataGridView in this custom dialog box, and there will also be a button .. When the user clicks this button, an integer value is returned to the caller, and the dialog box then terminates itself ..
我想为我的 C# 项目创建一个自定义对话框 .. 我想在这个自定义对话框中有一个 DataGridView,并且还会有一个按钮 .. 当用户点击这个按钮时,一个整数值被返回给调用者, 然后对话框自行终止..
How can I achieve this ?
我怎样才能做到这一点?
采纳答案by Bas
There is no prompt dialog box in C#. You can create a custom prompt box to do this instead.
C#中没有提示对话框。您可以创建一个自定义提示框来执行此操作。
public static class Prompt
{
public static int ShowDialog(string text, string caption)
{
Form prompt = new Form();
prompt.Width = 500;
prompt.Height = 100;
prompt.Text = caption;
Label textLabel = new Label() { Left = 50, Top=20, Text=text };
NumericUpDown inputBox = new NumericUpDown () { Left = 50, Top=50, Width=400 };
Button confirmation = new Button() { Text = "Ok", Left=350, Width=100, Top=70 };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.Controls.Add(inputBox);
prompt.ShowDialog();
return (int)inputBox.Value;
}
}
Then call it using:
然后使用以下方法调用它:
int promptValue = Prompt.ShowDialog("Test", "123");
回答by Steve
- On your button set the DialogResult property to DialogResult.OK
- On your dialog set the AcceptButton property to your button
- Create a public property in your form called Result of int type
- Set the value of this property in the click event of your button
Call your dialog in this way
using(myDialog dlg = new myDialog()) { if(dlg.ShowDialog() == DialogResult.OK) { int result = dlg.Result; // whatever you need to do with result } }
- 在您的按钮上将 DialogResult 属性设置为 DialogResult.OK
- 在您的对话框中,将 AcceptButton 属性设置为您的按钮
- 在您的表单中创建一个名为 Result of int 类型的公共属性
- 在按钮的单击事件中设置此属性的值
以这种方式调用您的对话框
using(myDialog dlg = new myDialog()) { if(dlg.ShowDialog() == DialogResult.OK) { int result = dlg.Result; // whatever you need to do with result } }
回答by Karim Mirshahi
//combo box dialog c#
//
public static string DialogCombo(string text,DataTable comboSource,string DisplyMember,string ValueMember)
{
//comboSource = new DataTable();
Form prompt = new Form();
prompt.RightToLeft = RightToLeft.Yes;
prompt.Width = 500;
prompt.Height = 200;
Label textLabel = new Label() { Left = 350, Top = 20, Text = text };
ComboBox combo = new ComboBox { Left = 50, Top = 50, Width = 400 };
combo.DataSource = comboSource;
combo.ValueMember = ValueMember;
combo.DisplayMember = DisplyMember;
Button confirmation = new Button() { Text = "?????", Left = 350, Width = 100, Top = 70 };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.Controls.Add(combo);
prompt.ShowDialog();
return combo.SelectedValue.ToString();
}
回答by Karim Mirshahi
public static DialogResult InputBox(string title, string promptText, ref string value,bool isDigit=false)
{
Form form = new Form();
Label label = new Label();
TxtProNet textBox = new TxtProNet();
if (isDigit == true)
textBox.TypeNumricOnly = true;
textBox.Width = 1000;
Button buttonOk = new Button();
Button buttonCancel = new Button();
form.Text = title;
label.Text = promptText;
textBox.Text = value;
buttonOk.Text = "OK";
buttonCancel.Text = "Cancel";
buttonOk.DialogResult = DialogResult.OK;
buttonCancel.DialogResult = DialogResult.Cancel;
label.SetBounds(9, 20, 372, 13);
textBox.SetBounds(12, 36, 372, 20);
buttonOk.SetBounds(228, 72, 75, 23);
buttonCancel.SetBounds(309, 72, 75, 23);
label.AutoSize = true;
textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
form.ClientSize = new Size(396, 107);
form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.StartPosition = FormStartPosition.CenterScreen;
form.MinimizeBox = false;
form.MaximizeBox = false;
form.AcceptButton = buttonOk;
form.CancelButton = buttonCancel;
DialogResult dialogResult = form.ShowDialog();
value = textBox.Text;
return dialogResult;
}
回答by Su Llewellyn
One uses the MessageBox object for this type of behavior. In code, if you asked a Yes/No question to a user, it looks like this.
对于这种类型的行为,可以使用 MessageBox 对象。在代码中,如果您向用户询问是/否问题,它看起来像这样。
In the example, I'm asking the user if he or she wishes to save their changed document in a method called AskToSave().
在这个例子中,我询问用户他或她是否希望在名为AskToSave()的方法中保存他们更改的文档。
private void AskToSave()
{
DialogResult result = MessageBox.Show("Save Document?", "Wait!",MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if(result == DialogResult.No)
{
return;
}
else
{
SaveDocument();
}
}
回答by Gurvir Lochab
public partial class DialogFormDisplay : Form
{
public DialogFormDisplay()
{
InitializeComponent();
}
string dialogcode;
public void Display(string code, string title, string description)
{
dialogcode = code;
switch (code)
{
case "YesNo":
btnLeft.Text = "Yes";
btnLeft.BackColor = Color.ForestGreen;
btnLeft.ForeColor = Color.White;
btnRight.Text = "No";
btnRight.BackColor = Color.Red;
btnRight.ForeColor = Color.White;
break;
default:
break;
}
displayTitle.Text = title;
displayMessage.Text = description;
}
private void btnLeft_Click(object sender, EventArgs e)
{
dialogResultLeft(dialogcode);
}
private void btnRight_Click(object sender, EventArgs e)
{
dialogResultRight(dialogcode);
}
void dialogResultLeft(string code)
{
switch (code)
{
case "YesNo":
DialogResult = DialogResult.Yes;
MessageBox.Show("You pressed " + DialogResult);
break;
default:
break;
}
}
void dialogResultRight(string code)
{
switch (code)
{
case "YesNo":
DialogResult = DialogResult.No;
MessageBox.Show("You pressed " + DialogResult);
break;
default:
break;
}
}
}
You can check this out on https://github.com/gurvirlochab/CustomNotificationsHere is a sample screenshot of the DialogBox
您可以在https://github.com/gurvirlochab/CustomNotifications上查看这里是 DialogBox 的示例屏幕截图

