InputBox Class

  • 18 years ago

    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(OK
    Click);


           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

  • 18 years ago

    Michael.... i'd suggest some significant changes to the way you've written that. how about... (i haven't tested this but you should get the idea )


    Code:
    public class InputBox : Form {


      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";
          OK.DialogResult = DialogResult.OK;
          Button cancl = new Button();
          cancl.Text = "Cancel";
          cancl.DialogResult = DialogResult.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);
      }


      public string InputText {
          get { return tb1.Text; }
      }


      public static string Show(string text,string title) {
          InputBox myDialog = new InputBox(text, title);
          DialogResult result = myDialog.ShowDialog();
          if (result == DialogResult.OK) {
              return myDialog.InputText;
          } else {
          return "";
          }
      }


    }//end of class InputBox



    likewise, i really wouldn't use those GC.Collect calls either... they're really "expensive", and the Gc will collect objects when it needs to anyway.

  • 18 years ago

    Thanks for the code, it only took a little tweaking to get it
    perfect.


    The ShowDialog() method is something I'll needed to
    remember.


    thanks again,
    - Mike

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“A computer lets you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila” - Mitch Ratcliffe