Monday, November 13, 2006

realize inputBox in C#.net by referencing VB.net component

this will add VB.NET runtime dependency in C# application, you can even add a reference to Microsoft.VisualBasic assembly and call InputBox function implemented there:

string aaa = Microsoft.VisualBasic.Interaction.InputBox("enter a name", "Inputbox Demo", "ROCKY", 100, 100);
MessageBox.Show(aaa);

Another way of doing it without referencing VB.net is to create a form that looks like an inputBox:


using System;

namespace MyNamespace
{
public class InputDialog : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtInput;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button cmdOK;
private System.Windows.Forms.Button cmdCancel;

public string Value
{
get
{
return txtInput.Text;
}
set
{
txtInput.Text = value;
}
}

public InputDialog()
{
// Required for Windows Form Designer support
InitializeComponent();
}

private void InitializeComponent()
{
this.txtInput = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.cmdOK = new System.Windows.Forms.Button();
this.cmdCancel = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// txtInput
//
this.txtInput.Location = new System.Drawing.Point(48, 16);
this.txtInput.Name = "txtInput";
this.txtInput.Size = new System.Drawing.Size(232, 20);
this.txtInput.TabIndex = 0;
this.txtInput.Text = "";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(8, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(33, 16);
this.label1.TabIndex = 1;
this.label1.Text = "Input:";
//
// cmdOK
//
this.cmdOK.DialogResult = System.Windows.Forms.DialogResult.OK;
this.cmdOK.Location = new System.Drawing.Point(69, 48);
this.cmdOK.Name = "cmdOK";
this.cmdOK.TabIndex = 2;
this.cmdOK.Text = "OK";
//
// cmdCancel
//
this.cmdCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.cmdCancel.Location = new System.Drawing.Point(149, 48);
this.cmdCancel.Name = "cmdCancel";
this.cmdCancel.TabIndex = 2;
this.cmdCancel.Text = "Cancel";
//
// Form1
//
this.AcceptButton = this.cmdOK;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.CancelButton = this.cmdCancel;
this.ClientSize = new System.Drawing.Size(292, 86);
this.Controls.Add(this.cmdOK);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtInput);
this.Controls.Add(this.cmdCancel);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
}
}

No comments: