How to Use Data Validation to Populate a Multi-Level Drop Down

Data validation is a technique that processes data and corrects any formatting issues that may interfere with other parts of the program. For example, data validation can ensure that only numbers entered into a text box are passed on to another part of the program, such as a drop-down list. Any non-numeric or mixed alphanumeric data can be tossed out using data validation. What is left over is good data that can then populate a drop-down list. An easy way to get started with this is to use the .Net framework, which has many built-in functions that simplify form creation.

Things You'll Need

  • Computer with Visual Studio 2010 installed
Show More

Instructions

    • 1

      Open Visual Studio 2010 by clicking on its program icon. When it loads, select “File/New/Project “and click “Visual C#/Windows Forms Application.” A new Windows Form Application project is created, and a blank Windows Form appears in the main editor window.

    • 2

      Locate the “Toolbar” panel, which is to the right of the main editor window. This lists all of the graphical user interface controls that you can add to your form.

    • 3

      Select “TextBox” and drag it onto the form. Release the mouse button to place it. This is where users will enter data that will then be validated. Valid data will be passed on to the drop-down list, which is called a “ComboBox” in the .Net framework.

    • 4

      Select “ComboBox” and drag it onto the form. Place it like you placed the “TextBox,” by releasing the mouse button on the form. The “ComboBox” will only accept valid data from the “TextBox.”

    • 5

      Click the “TextBox” to highlight it. Locate the “Properties” panel, which is next to the “Toolbar” panel. Click on the tiny lightning bolt to display all of the events associated with the “TextBox” control.

    • 6

      Double-click the “TextChanged” event. The main editor window immediately switches to source code view, and displays a brand-new, auto-generated “TextChanged” event. The code for this event looks like this:

      private void textBox1_TextChanged(object sender, EventArgs e)
      {

      }

    • 7

      Create an integer variable named “data.” This will store the validated data before adding it to the “ComboBox.” Write the following inside the curly brackets of the “TextChanged” event.

      int data = 0;

    • 8

      Use the “TryParse” function to validate data. This function returns a true value if a text string passed to it contains only numbers. Otherwise, it returns false. You can place it inside an “if” statement to populate the drop-down list in cases in which the “TextBox” contains only numbers. Write the following below the integer variable declaration:

      if (Int32.TryParse(textBox1.Text, out data) )
      { comboBox1.Items.Add(data); }

    • 9

      Click the green “Play” button to compile and run the program. A Windows Form appears, with a text box and a drop-down, or combo, box. Type letters and numbers into the text box. As you type, only the numbers are added to the drop-down box. Every number adds a new level to the drop-down box, making it a multi-level box.

Related Searches:

References

Comments

Related Ads

Featured