Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4326
  • Last Modified:

C# InputBox Class

I wrote library containing an InputBox class for C# but I can only call it once during a program.  I access it from a program using the following code:

      string input = Input.InputBox.Show (s);

This then accesses the Show() method:

      box = new InputBox (s);
      Application.Run (box);
      return box.GetInput;

This code calls the appropriate constructor, then displays the InputBox form, and then returns the value set with the EventHandler:

      this.GetInput = this.textbox.Text;
      this.Close ();

The first time I call it, this code will work without any trouble.  If I then make another call to Input.InputBox.Show, however, I can see the form flash on the screen and then close immediately, returning an empty string.  If I change "this.Close ();" to "this.Hide ();" I get the same result.

Any thoughts as to why the form won't remain open?
0
DiRN
Asked:
DiRN
  • 5
  • 2
  • 2
1 Solution
 
HummusxCommented:
You are probably going to need to post some more code.  Are some of these methods/members shared?
0
 
DiRNAuthor Commented:
Here's more detailed code:

[test.cs]

using Input;

namespace Test {
  public class test {
    static void Main () {
      string question = InputBox.Show ("Enter the question");
      string answer = InputBox.Show ("Enter the answer");
     
      MessageBox.Show (question + " " + answer);
    }
  }
}

[InputBox.cs]

    public string GetInput {
      get { return this.input; }
      set { this.input = value; }
    }

    private void button_Click (object sender, EventArgs e) {
      this.GetInput = this.textbox.Text;
      this.Close ();
    }

   public static string Show (string s[1, string s2]) {
      InputBox box = new InputBox (s[1, s2]);
      Application.Run (box);
      return box.GetInput;
    }

The constructors call initialize the form and set input (private string) to the empty string.  There really isn't any more code to show (aside from the constructors and the function that does most of the initialization of the form).
0
 
HummusxCommented:
InputBox inherits from Form?  Have you tried using the form's show method instead of Application.Run?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
DiRNAuthor Commented:
Yes, InputBox does inherit from Form.  I tried the Show () method.  Using Show () instead of Application.Run () causes all calls to InputBox.Show () to run and then return empty strings (I don't have a chance to do anything to the form).  After the last form returns its empty string, all of the forms close.
0
 
DiRNAuthor Commented:
I've been playing around with the OnLoad () method for InputBox.  If I place a call to MessageBox.Show () (and nothing else) inside it, aside from the first call to InputBox.Show (), the MessageBox appears after the form has closed.
0
 
bgungorCommented:
You are using an instance of the class within the class declaration.  This is code written as VB (pre .NET) behaves, not as how .NET behaves.

Change your code to look like the following (note: use Show instead of ShowDialog if you don't need modal input boxes).  By the way, I created this in the designer and added some features (like the AcceptButton to close your form).  Paste this into a new .cs file and take a look.:

[test.cs]

using Input;

namespace Test {
 public class test {
   static void Main () {
     InputBox frmInput = new InputBox("Enter the question");
     frmInput.ShowDialog();
     string question = frmInput.Value;
     frmInput = new InputBox("Enter the answer");
     frmInput.ShowDialog();
     string answer = frmInput.Value;
     MessageBox.Show (question + " " + answer);
   }
 }
}

[InputBox.cs]

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

namespace Input
{
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class InputBox : System.Windows.Forms.Form
      {
            private System.Windows.Forms.Label lblQuery;
            private System.Windows.Forms.TextBox txtInput;
            private System.Windows.Forms.Button cmdClose;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;

            public InputBox(string queryValue)
            {
                  lblQuery.Text = queryValue;
                  //
                  // 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 );
            }

            #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.lblQuery = new System.Windows.Forms.Label();
                  this.txtInput = new System.Windows.Forms.TextBox();
                  this.cmdClose = new System.Windows.Forms.Button();
                  this.SuspendLayout();
                  //
                  // lblQuery
                  //
                  this.lblQuery.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right);
                  this.lblQuery.Location = new System.Drawing.Point(16, 8);
                  this.lblQuery.Name = "lblQuery";
                  this.lblQuery.Size = new System.Drawing.Size(208, 16);
                  this.lblQuery.TabIndex = 0;
                  //
                  // txtInput
                  //
                  this.txtInput.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right);
                  this.txtInput.Location = new System.Drawing.Point(8, 32);
                  this.txtInput.Name = "txtInput";
                  this.txtInput.Size = new System.Drawing.Size(216, 20);
                  this.txtInput.TabIndex = 1;
                  this.txtInput.Text = "";
                  //
                  // cmdClose
                  //
                  this.cmdClose.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
                  this.cmdClose.Location = new System.Drawing.Point(8, 64);
                  this.cmdClose.Name = "cmdClose";
                  this.cmdClose.TabIndex = 2;
                  this.cmdClose.Text = "Close";
                  //
                  // Form1
                  //
                  this.AcceptButton = this.cmdClose;
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(240, 102);
                  this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                                              this.cmdClose,
                                                                                                              this.txtInput,
                                                                                                              this.lblQuery});
                  this.MinimumSize = new System.Drawing.Size(248, 136);
                  this.Name = "Form1";
                  this.Text = "Input";
                  this.ResumeLayout(false);

            }
            #endregion

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

      }
}
0
 
DiRNAuthor Commented:
bgungor,

Thanks for the code, but it seems to have the same problem as replacing Application.Run () with Show () in my code.  Also, this isn't the exact functionality I'm looking for.  I'm looking to mimic calls VB6's InputBox as best I can (e.g., string s = InputBox.Show (<prompt>[, <title>]);)

Here's my complete code:

[test.cs]
using System;
using System.Windows.Forms;

using Input;

namespace Test {
  public class test {
    static void Main () {
      string question = InputBox.Show ("Enter the question");
      string answer = InputBox.Show ("Enter the answer");
     
      MessageBox.Show (question + " " + answer);
    }
  }
}

[InputBox.cs]
//////////////////////////////////////////////////////
//                                                  //
// InputBox                                         //
// C# DLL to mimic Visual Basic InputBox            //
// InputBox.cs                                      //
//                                                  //
// Copyright ) 2003 Andy Dirnberger                 //
// copyright ) 2003 DiRNonline.NET                  //
//                                                  //
// Created March 6, 2003                            //
// Last Modified March 8, 2003                      //
//                                                  //
// **NOTE**                                         //
// Even though this file contains Visual C# code,   //
// it was not created with Microsoft Visual C# .NET //
// Therefore, this file may not open properly in    //
// the Visual C# IDE.  If you wish to use the IDE,  //
// please use caution and verify that code is       //
// handled properly by it.                          //
//                                                  //
// This code is compiled at the command line:       //
//   csc /target:library InputBox.cs                //
//                                                  //
//////////////////////////////////////////////////////

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

namespace Input {
  public class InputBox : Form {
    private Container components = null;

    private Label label;
    private TextBox textbox;
    private Button button;

    private string input;
   
    public InputBox (string s) {
      Init ();
      this.label.Text = s;
      input = "";
    }

    public InputBox (string s1, string s2) {
      Init ();
      this.label.Text = s1;
      this.Text = s2;
      input = "";
    }

    protected override void Dispose (bool disposing) {
      if (disposing)
        if (components != null)
          components.Dispose ();
      base.Dispose (disposing);
    }

    private void Init () {
      this.label = new Label ();
      this.textbox = new TextBox ();
      this.button = new Button ();

      this.SuspendLayout ();

      this.label.Location = new Point (10, 10);
      this.textbox.Location = new Point (10, 35);
      this.button.Location = new Point (315, 33);

      this.label.Size = new Size (380, 20);
      this.textbox.Size = new Size (300, 25);

      this.button.Text = "OK";

      this.button.Click += new System.EventHandler (this.button_Click);

      Controls.Add (this.label);
      Controls.Add (this.textbox);
      Controls.Add (this.button);

      this.Size = new Size (400, 100);

      this.MaximizeBox = false;
      this.Name = "InputBox";
     
      this.ResumeLayout (false);
    }
   
    private void button_Click (object sender, EventArgs e) {
      this.GetInput = this.textbox.Text;
      this.Close ();
    }
 
/*
    protected override void OnLoad (EventArgs e) {
      MessageBox.Show ("OnLoad");
    }
*/

    public string GetInput {
      get { return this.input; }
      set { this.input = value; }
    }

    public static string Show (string s) {
      InputBox box = new InputBox (s);
      Application.Run (box);
      return box.GetInput;
    }

    public static string Show (string s1, string s2) {
      InputBox box = new InputBox (s1, s2);
      Application.Run (box);
      return box.GetInput;
    }
  }
}
0
 
bgungorCommented:
In order to mimic the VB 6 InputBox, you will have to override the Form class' Show method, basically calling ShowDialog inside that function, then determining the return value when the form is closed and returning the input value from the Show method.

You can't avoid the Application.Run in a Windows application because that is what makes the MessageLoop occur.  Attempting to do so will play havoc with your application.  Instead, leave your Application.Run alone, but put the name of your main form inside the call to Application.Run(MainForm), and call the input box from your main form.

Perhaps it is how I am trying to resolve your issue, but you cannot view .NET the same way VB 6 operated.  VB 6 did what it did mainly to get around the limitations that the language inherently has.  Don't take me wrong, I have worked with VB since version 3, and it has a big place in my heart, but now it is time to let go of what held VB back and grab tightly onto .NET.

Let me know if I can make this clearer.

Bg
0
 
DiRNAuthor Commented:
Bg,

Thanks for the help.  I actually replaced the Application.Run (box) line with box.ShowDialog ().  Now it works.

DiRN
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now