InputBox in VC#.net?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In VB, we have InputBox to allow a user to input text. Do we have a similar
one in VC#.net? Obviously, MessageBox can not do it.

Thanks.
 
Hi Andrew

I wrote a simple class to do this. let me know if you want me to post it...

Usage was similar to MessageBox - it was InputBox.Show(), returning a
String...so we had a static Show method on a class called Input Box, a
private constructor to create an instance, and handle the ShowDialog in the
static show method.

Nigel Armstrong
 
Andrew said:
In VB, we have InputBox to allow a user to input text. Do we have a
similar
one in VC#.net? Obviously, MessageBox can not do it.

If you are lazy, add a reference to "Microsoft.VisualBasic.dll" and use
'Microsoft.VisualBasic.Interaction.InputBox'...
 
If you want, you can actually still use the VB InputBox. Just include a
reference to the "Microsoft.VisualBasic.dll" assembly.

string s = Microsoft.VisualBasic.Interaction.InputBox("Message", "Title",
"Default", 0, 0);
 
Yes, please...

Nigel Armstrong said:
Hi Andrew

I wrote a simple class to do this. let me know if you want me to post it...

Usage was similar to MessageBox - it was InputBox.Show(), returning a
String...so we had a static Show method on a class called Input Box, a
private constructor to create an instance, and handle the ShowDialog in the
static show method.

Nigel Armstrong
 
Hi Andrew

Apologies for the length of this post...

Built using the design time environment...

HTH

Nigel Armstrong

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace CSharpInputBox
{
/// <summary>
/// Summary description for InputBox.
/// </summary>
public class InputBox : System.Windows.Forms.Form
{
private System.Windows.Forms.Button buttonOK;
private System.Windows.Forms.Button buttonCancel;
private System.Windows.Forms.Label promptLabel;
private System.Windows.Forms.TextBox inputTextBox;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

private InputBox()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

private static InputBox inputBox = null;
public static string Show(string Prompt, string Title, string
DefaultResponse)
{
if (inputBox == null)
{
inputBox = new InputBox();
}
inputBox.promptLabel.Text = Prompt;
inputBox.Text = Title;
inputBox.inputTextBox.Text = DefaultResponse;
if (inputBox.ShowDialog() == DialogResult.OK)
{
return inputBox.inputTextBox.Text;
}
else
{
return String.Empty;
}
}

public static string Show(string Prompt)
{
return Show(Prompt, Application.ProductName, String.Empty);
}

public static string Show(string Prompt, string Title)
{
return Show(Prompt, Title, String.Empty);
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.buttonOK = new System.Windows.Forms.Button();
this.buttonCancel = new System.Windows.Forms.Button();
this.promptLabel = new System.Windows.Forms.Label();
this.inputTextBox = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// buttonOK
//
this.buttonOK.DialogResult = System.Windows.Forms.DialogResult.OK;
this.buttonOK.Location = new System.Drawing.Point(304, 16);
this.buttonOK.Name = "buttonOK";
this.buttonOK.TabIndex = 0;
this.buttonOK.Text = "OK";
//
// buttonCancel
//
this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.buttonCancel.Location = new System.Drawing.Point(304, 48);
this.buttonCancel.Name = "buttonCancel";
this.buttonCancel.TabIndex = 1;
this.buttonCancel.Text = "Cancel";
//
// promptLabel
//
this.promptLabel.Location = new System.Drawing.Point(8, 16);
this.promptLabel.Name = "promptLabel";
this.promptLabel.Size = new System.Drawing.Size(288, 104);
this.promptLabel.TabIndex = 2;
this.promptLabel.Text = "label1";
//
// inputTextBox
//
this.inputTextBox.Location = new System.Drawing.Point(8, 128);
this.inputTextBox.Name = "inputTextBox";
this.inputTextBox.Size = new System.Drawing.Size(368, 20);
this.inputTextBox.TabIndex = 3;
this.inputTextBox.Text = "";
//
// InputBox
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(384, 164);
this.Controls.Add(this.inputTextBox);
this.Controls.Add(this.promptLabel);
this.Controls.Add(this.buttonCancel);
this.Controls.Add(this.buttonOK);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "InputBox";
this.Text = "InputBox";
this.ResumeLayout(false);

}
#endregion
}
}
 
Back
Top