-2

I am wondering if c# has build-in input dialog, this dialog only has one(or only a few) text input and OK and Cancel button.

So I don't need to add a new form for such a simple job.

camino
  • 10,085
  • 20
  • 64
  • 115
  • I assume that you are talking about the framework and not the language itself, but then for what technology? WinForms, WPF, ASP.NET etc... – Steve Dec 25 '21 at 00:01
  • Winform, Thanks! – camino Dec 25 '21 at 00:09
  • no you have to make your own – pm100 Dec 25 '21 at 00:15
  • The duplicate answers your question, however I prefer to not add the reference to Microsoft.VisualBasic in my applications. So I have ended to copy [one of the many already done examples](https://www.bing.com/search?q=InputBox%20for%20C%23&qs=n&form=QBRE&sp=-1&pq=inputbox%20for%20c%23&sc=1-15&sk=&cvid=122C3B118C9D498A92C4226D80509A1D) that you can retrieve on the net and put it a utility library that I can reuse in many apps. – Steve Dec 25 '21 at 00:20

1 Answers1

0

You can use System.Windows.Forms.MessageBox.Show to create a message box and specify that you only want an Ok and Cancel button:

using System.Windows.Forms;
MessageBox.Show("Prompt", "Caption", MessageBoxButtons.OKCancel);

The Show method returns the button clicked as a System.Windows.Forms.DialogResult:

DialogResult rst = MessageBox.Show("Prompt", "Caption", MessageBoxButtons.OKCancel);
if(rst == DialogResult.OK)
    MessageBox.Show("You clicked OK");

If you are asking how to create an input box, then the question has already been asked here What is the C# version of VB.net's InputDialog?, though I would recommend that you create your own version of an input box, because the VBA version is pretty ugly - that way you can also style the input box according to your desire.

  • 1
    I think the OP wants something where they can type some text and get the input back – Steve Dec 25 '21 at 00:06
  • 1
    @Steve If he wants to get the input back, then he is asking a duplicate question, and the question should be flagged as a duplicate. See [What is the C# version of VB.net's InputDialog?](https://stackoverflow.com/questions/97097/what-is-the-c-sharp-version-of-vb-nets-inputdialog). –  Dec 25 '21 at 00:16
  • _"though I would recommend that you create your own version of an input box."_ - why is that? –  Dec 25 '21 at 00:20
  • @MickyD Because the VBA version is pretty ugly. I usually create a simple form, that can be configured like a MessageBox can. I have updated the answer. –  Dec 25 '21 at 00:31