I tried coding my own InputBox class to work like MessageBox:
String InputBox.Show(String, String);
But I'm having trouble with it, here's the code:
Code:
public class InputBox : Form {
private static bool returned = false;
private static string retString = "";
private Label internText = new Label();
private TextBox tb1 = new TextBox();
private InputBox(string text, string title) {
this.Text = title;
internText.Text = text;
this.Size = new Size(450,150);
internText.Location = new Point(10,30);
tb1.Location = new Point(10,60);
tb1.Size = new Size(300, tb1.Size.Height);
this.ShowInTaskbar = false;
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
Button OK = new Button();
OK.Text = "OK";
Button cancl = new Button();
cancl.Text = "Cancel";
OK.Location = new Point(320,30);
cancl.Location = new Point(320,34+OK.Size.Height);
this.Controls.Add(internText);
this.Controls.Add(tb1);
this.Controls.Add(OK);
this.Controls.Add(cancl);
cancl.Click += new EventHandler(CancelClick);
OK.Click += new EventHandler(OKClick);
this.Closing += new CancelEventHandler(this_Close);
this.Show();
}
public static string Show(string text,string title) {
new InputBox(text, title);
while(!returned) {
Application.DoEvents();
}
return retString;
}
private void Cancel_Click(object s, EventArgs e) {
retString = "";
returned = true;
this.Dispose();
System.GC.Collect();
}
private void OK_Click(object s, EventArgs e) {
retString = tb1.Text;
returned = true;
this.Dispose();
System.GC.Collect();
}
private void this_Close(object s, CancelEventArgs e) {
this.Dispose();
System.GC.Collect();
}
}//end of class InputBox
The problem is, if I go like this:
string t = InputBox.Show("Something","Something");
string r = InputBox.Show("Something else","Something else");
two InputBoxes show up, but both string get assigned to the first box's result.
I think it has something to do with the Application.DoEvents() call in the static
Show method, but I don't know any other way to not have the program hang in the
while loop.
Any ideas?
Thanks,
- Mike
Enter your message below
Sign in or Join us (it's free).