?
Solved

C# InputBox Class

Posted on 2003-03-07
9
Medium Priority
?
4,262 Views
Last Modified: 2008-03-03
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
Comment
Question by:DiRN
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 2

Expert Comment

by:Hummusx
ID: 8088379
You are probably going to need to post some more code.  Are some of these methods/members shared?
0
 

Author Comment

by:DiRN
ID: 8088675
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
 
LVL 2

Expert Comment

by:Hummusx
ID: 8088963
InputBox inherits from Form?  Have you tried using the form's show method instead of Application.Run?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:DiRN
ID: 8089036
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
 

Author Comment

by:DiRN
ID: 8089592
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
 
LVL 4

Expert Comment

by:bgungor
ID: 8089661
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
 

Author Comment

by:DiRN
ID: 8089765
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
 
LVL 4

Accepted Solution

by:
bgungor earned 200 total points
ID: 8116700
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
 

Author Comment

by:DiRN
ID: 8121323
Bg,

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

DiRN
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question