Tuesday, December 7, 2010

C# InputBox

A lot of people who start programming might choose Visual Basic because, as the name suggests, it is a rather easy language to learn since there are a lot of words and fewer symbols than other languages like Java and C#.  As time goes on and they become better at programming, people might want to switch over to C# because Visual Basic is too verbose or because of C#'s similarity to Java.  Or you could just start with C#.  Either way, if you're just starting to use C#, you realize there is no built-in way to get input from the user through a dialog box in C#.

So I created my own InputBox.  And get this: it has the option of getting multiple responses at once in an array.  This is pretty useful for getting multi-parameter things from the user, such as getting the location of a new Point (it's much nicer to have the X and Y coordinate prompt in the same dialog than to have two separate dialogs).

HOW TO ADD NEW CLASSES TO YOUR PROJECT:

To add this class to your C# project, you should click: Project > Add New Item... (or Ctrl + Shift + A).  Select a "New Class."  Name it "InputBox.cs".  Then copy and paste the following code into your new class.


Alternatively, you can download the class (InputBox.cs) and add it to your class: Project > Add Exiting Item (or Shift + Alt + A).  Choose the InputBox.cs file you just downloaded.

Basically, Show(...) is an overloaded method that can return an array of values with the same dimensions of your prompt array or return a single value if your prompt is only a single string.  Note: the method returns null if the user presses cancel.

Examples:
string par = InputBox.Show("Give me a number:", "Prompt");    // shows a dialog box for getting a single value in return with the title "Prompt"
string[] pars = InputBox.Show(new string[] {"X:", "Y:"}, "");    // shows a dialog box for getting an X value (will be in pars[0]) and a Y value (will be in pars[1]) without a title because of the empty string in the title parameter 
Here is the code (sorry the spacing is awkward and the text is so small... it's just so that all code fits on one line... it'll look just fine after you copy and paste it into your C# class):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;




static class InputBox
{


    public static string[] Show(string[] pars, string title)
    {
        int size = pars.Length;
        Form frmInputBox = new Form();
        Label[] labels = new Label[size];
        TextBox[] texts = new TextBox[size];
        Button btnOK = new Button();
        Button btnCancel = new Button();


        frmInputBox.Text = title;
        frmInputBox.ClientSize = new Size(420, 100 + 40 * (size - 1));
        frmInputBox.FormBorderStyle = FormBorderStyle.FixedDialog;
        frmInputBox.MinimizeBox = false;
        frmInputBox.MaximizeBox = false;
        frmInputBox.AcceptButton = btnOK;
        frmInputBox.CancelButton = btnCancel;
        frmInputBox.StartPosition = FormStartPosition.CenterScreen;


        for (int i = 0; i < size; i++)
        {
            Label l = new Label();
            l.Text = pars[i];
            l.Location = new Point(10, 10 + 40 * i);
            l.AutoSize = true;
            labels[i] = l;
            TextBox t = new TextBox();
            t.Location = new Point(10, 25 + 40 * i);
            t.Size = new Size(400, 20);
            texts[i] = t;
        }


        btnOK.Text = "OK";
        btnOK.Location = new Point(255, 70 + 40 * (size - 1));
        btnOK.DialogResult = DialogResult.OK;


        btnCancel.Text = "Cancel";
        btnCancel.Location = new Point(335, 70 + 40 * (size - 1));
        btnCancel.DialogResult = DialogResult.Cancel;


        frmInputBox.Controls.AddRange(labels);
        frmInputBox.Controls.AddRange(texts);
        frmInputBox.Controls.AddRange(new Control[] {btnOK, btnCancel});


        DialogResult dialogResult = frmInputBox.ShowDialog();
        if (dialogResult == DialogResult.OK)
        {
            string[] results = new string[size];
            for (int i = 0; i < size; i++)
            {
                results[i] = texts[i].Text;
            }
            return results;
        }
        else
        {
            return null;
        }
    }


    public static string Show(string par, string title)
    {
        string[] temp = Show(new string[] {par}, title);
        if (temp == null)
        {
            return null;
        }
        else
        {
            return temp[0];
        }
    }
}

1 comment: