17

Any idea how to display textBox control in MessageBox.

I'm working on winforms projcet c#.

Thank you in advance.

Michael
  • 13,950
  • 57
  • 145
  • 288
  • 1
    Did you try anything? – Soner Gönül May 29 '13 at 10:52
  • 1
    Related posts - [What is the C# version of VB.net's InputDialog?](https://stackoverflow.com/q/97097/465053) & [Messagebox with input field](https://stackoverflow.com/q/10797774/465053) – RBT Aug 04 '21 at 09:51

9 Answers9

21

You can't. MessageBox is a special container designed to only show a message and buttons. Instead, you can create your own Form with whatever controls you want, and use .ShowDialog() on it.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
krillgar
  • 12,596
  • 6
  • 50
  • 86
16

You can simply add an Input box from VB.NET into your C# project. First add Microsoft.VisualBasic to your project References, then use the following code:

string UserAnswer = Microsoft.VisualBasic.Interaction.InputBox("Your Message ", "Title", "Default Response");

And that should work properly.

Erfan
  • 437
  • 5
  • 9
4

It will be better to add a new Form in you application which you can customize the way you want.

and just call it from where ever required.

Taj
  • 1,718
  • 1
  • 12
  • 16
2

you could create a classic win form that looks like a message box and opening it as a modal form perhaps using Form.ShowDialog

more info at

http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
2

You cannot customise the MessageBox, its better you use a popup designed using Windows Form separately and use its instance to invoke.

customPopup popup = new customPopup();
popup.ShowDialog();

Place your controls on the popup form and set their access modifiers to public if you want to access the textboxes or labels etc in your previous form.

customPopup popup = new customPopup();
popup.msgLabel.Text= "Your message";
popup.ShowDialog();
Jibran Khan
  • 3,236
  • 4
  • 37
  • 50
1

As I know there is no way to do that.

You can create a winform change it's style to look like a MessageBox and add your own controls.

Jahan Zinedine
  • 14,616
  • 5
  • 46
  • 70
0

Yes, as krillgar mentioned,you should create your own form. And 1. Encapsulate the form in a static class or function, so you may just call MyMessageBox.Show(). 2. The textbox should have readonly=true, so the end users won't be able to change the text displayed, while they could select text and copy to clipboard.

Regarding to item 2, I believe many Windows build applications and MS Office use such approach.

ZZZ
  • 2,752
  • 2
  • 25
  • 37
0
using Microsoft.VisualBasic;//add reference
var Password = Interaction.InputBox("Message", "Title" ,"information in textbox", -1,-1);

In the variable "password" it receives the information that is entered from the text box.

Remember to add the reference "Microsoft.VisualBasic" in the solution explorer

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • Please, provide an explanation of your answer in order to make it clearer as possible, not only for the OP, but also for the community (more in details: speak about the `InputBox` parameters) – xKobalt Aug 27 '20 at 13:18
-1

Solution in here, you can create windows form and design it, set form is dialog, when you call form, it is auto show. In form you design, you set value some parameter static where other class in project, but you should set when you close form design that, OK, come back form init call show dialog, you create interval call when have == null return call, when != null you stop call back and using parameter in class static it !

B.Syal
  • 69
  • 2
  • 5
  • While this might be a valuable hint to solve the problem, a good answer also demonstrates the solution. Please [edit] to provide example code to show what you mean. Alternatively, consider writing this as a comment instead. – Toby Speight Oct 02 '17 at 09:01