Many vb/vb.net developers are used with the InpuBox(it displays a prompt and waits for the user to input text then returns a string containing the contents of the text box).....
In using C# InputBox is not available!!!! so i decided to create one.
So i might help someone who needs this kind of functionality!
clsInputBox.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace InputBoxCS
{
public class clsInputBox
{
private static Form frmInputBox = new Form();
private static Label lblPrompt = new Label();
private static Button btnOk = new Button();
private static Button btnCancel = new Button();
private static TextBox txtResponse = new TextBox();
///
/// Initialize the controls
///
static clsInputBox()
{
frmInputBox.StartPosition = FormStartPosition.CenterScreen;
frmInputBox.MinimizeBox = false;
frmInputBox.MaximizeBox = false;
frmInputBox.FormBorderStyle = FormBorderStyle.FixedDialog;
frmInputBox.Controls.AddRange(new Control[] { lblPrompt , btnOk , btnCancel, txtResponse});
frmInputBox.Size = new Size(346, 130);
frmInputBox.AcceptButton = btnOk;
frmInputBox.CancelButton = btnCancel;
btnOk.Size = new Size(50, 23);
btnOk.Location = new Point(278, 4);
btnCancel.Size = new Size(50, 23);
btnCancel.Location = new Point(278, 33);
lblPrompt.AutoSize = true;
lblPrompt.Location = new Point(12, 14);
txtResponse.Size = new Size(316, 20);
txtResponse.Location = new Point(12, 62);
}
public static DialogResult Show (string Prompt, string title, string defaultresponse, ref string value )
{
frmInputBox.Text = title;
lblPrompt.Text = Prompt;
btnOk.Text = "&OK";
btnCancel.Text = "&Cancel";
btnOk.DialogResult = DialogResult.OK;
btnCancel.DialogResult = DialogResult.Cancel;
txtResponse.Focus();
DialogResult result = frmInputBox.ShowDialog();
value = txtResponse.Text;
return result;
}
}
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace InputBoxCS
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
string value = "";
if (DialogResult.OK == InputBoxCS.clsInputBox.Show("this my prompt", "this is my title", "response", ref value))
{
MessageBox.Show(value);
}
}
}
}





0 comments:
Post a Comment