-1

I have a windows form application. Based on a users age(inputting), I want to highlight one of the following labels "child, preteen, teen, adult" based on what they enter for age. I currently have a textbox for age that submits the users age to a label further down the form.

Here is what I am using: txtAge lblChild (<12) lblPreTeen(13 to 15) lblTeen(16 to 18) lblAdult(18>) btnSubmit

Thanks. I am new to coding and still grasping the basics.

2 Answers2

2

I'd recommend changing your TextBox to a NumericUpDown (called numAge), if possible. Go to the properties of the NumericUpDown in the Form editor and click the Events button (lightning bolt). If you double-click the ValueChanged option, it will create the stub for the following method:

private void numAge_ValueChanged(object sender, EventArgs e)
    {
        if (numAge.Value > 0 && numAge.Value < 13)
        {
            // Child
            // Highlight label
        }
        else if (numAge.Value > 12 && numAge.Value < 16)
        {
            // Pre-Teen
            // Highlight label
        }
        else if (numAge.Value > 15 && numAge.Value < 19)
        {
            // Teen
            // Highlight label
        }
        else if (numAge.Value > 18)
        {
            // Adult
            // Highlight label
        }
        else
        {
            // Clear the highlights
        }
    }

If you must use a TextBox, use the TextChanged method. That way you don't need the Submit Button:

private void txtAge_TextChanged(object sender, EventArgs e)
    {
        int txtAgeValue = 0;
        if (!string.IsNullOrWhiteSpace(txtAge.Text))
        {
            txtAgeValue = int.Parse(txtAge.Text);
        }
        if (txtAgeValue > 0 && txtAgeValue < 13)
        {
            // Child
            // Highlight label
        }
        else if (txtAgeValue > 12 && txtAgeValue < 16)
        {
            // Pre-Teen
            // Highlight label
        }
        else if (txtAgeValue > 15 && txtAgeValue < 19)
        {
            // Teen
            // Highlight label
        }
        else if (numAge.Value > 18)
        {
            // Adult
            // Highlight label
        }
        else
        {
            // Clear the highlights
        }
    }
George
  • 172
  • 9
0

on the text box enter event you can update relevant label colors with a few if statements.

CoderM
  • 81
  • 10