Hi,

I was comparing the features visual Basic & visual studio at that time i found that in VB provide the facility of message box.

But

in VS we don’t have the facility of input box.

The input box is really very helpful if we want to take some input from user then we can use it then why this facility is not provided to VS.

Can u tell me.

and what are the alternatives for that in VS.

Thank u

Virusisfound

Recommended Answers

All 12 Replies

The inputbox is language specific and is not a feature missing from "VS" but the "C#" language. It is very easy to roll your own input box in C# which is something i did a while back and reuse.
Just create a form with a label, a textbox and your standard OK/Cancel buttons. Open it modally and set the appropriate dialogresult value and add a property to retrieve the input value.
Feel free to ask if you need more detail on any of those steps...not sure what level you are at with C# :)

Here's a nice one:

http://www.csharp-examples.net/inputbox/

Example use:

// this will hold the value retrieved from the input box
string value = string.Empty;

DialogResult result = InputBox("Username prompt", "Please enter your username:", ref value);

if(result == DialogResult.OK)
{
   MessageBox.Show(value);
}
commented: great link, neat inputbox +1

You can still use VB object and functions in C#.
Add a reference to "Microsoft.VisualBasic" (Project Menu - Add Reference - .Net tab).
Then you can use Microsoft.VisualBasic.Interaction.InputBox(...)

[Edit] - Using farooqaaa's or Ryshad's idea is more efficient as it does not require linking to the VB library.

Hi,

Can u explain me how can i use inputBox in C#.

If all you need from VB is the InputBox then I would recommend looking at the sample code farooqaaa referenced rather then linking VB.

farooqaaa already gave you a link to some code that implements an InputBox in C# and provided an example of how to use it.

Try this first.

If you have trouble, start a new thread showing your code and explaining what the problem is and someone will probably help you.

Hi,

i tried the above given code but it show me an error message "Inputbox in not in current context" .

Post your code and identify where the error occurs.

Ok,

string value = string.Empty;

            DialogResult result = [B]InputBox[/B]("Username prompt", "Please enter your username:", ref value);

            if (result == DialogResult.OK)
            {
                MessageBox.Show(value);
            }

Error 1 The name 'InputBox' does not exist in the current context

You have not added the code for the InputBox from the link farooqaaa gave you.
Add the code from the second grey box from the below link in to your project.
http://www.csharp-examples.net/inputbox/

Add this code above "public Form1() {....":

public static DialogResult InputBox(string title, string promptText, ref string value)
{
  Form form = new Form();
  Label label = new Label();
  TextBox textBox = new TextBox();
  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;
}

thank u,

I am very sorry i was tinking that only that code work..

thank u for helping me..

Its working now.

yhank u once again..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.