You are viewing our Forum Archives. To view or take place in current topics click here.
C# Help with textboxes!
Posted:

C# Help with textboxes!Posted:

Visxal
  • 2 Million
Status: Offline
Joined: Nov 07, 20158Year Member
Posts: 2,261
Reputation Power: 332
Status: Offline
Joined: Nov 07, 20158Year Member
Posts: 2,261
Reputation Power: 332
I have a textbox where people have to enter a number but I don't want to them to type the number 0 in first, how do I do this?

Example: If they type, 10 it's ok but if they type 010 it's not ok and I want a window to appear and tell them to try again.

Thanks!

Current code:

int MassAns;
            int PassengersAns;
            PassengersAns = Convert.ToInt32(txt_Passengers.Text);
            PassengersAns = int.Parse(txt_Passengers.Text);
            MassAns = Convert.ToInt32(txt_Mass.Text);
            MassAns = int.Parse(txt_Mass.Text);

            if ((MassAns >= 1) && (MassAns <= 180) && (PassengersAns >= 1) && (PassengersAns <= 10))
            {
                this.Hide();
                PaymentForm sistema = new PaymentForm();
                sistema.ShowDialog();
                this.Close();
            }

            else
            {
                MessageBox.Show("Please enter a mass between 1-180 and amount of passengers between 1-10.");
            }
        }
#2. Posted:
Visxal
  • Summer 2023
Status: Offline
Joined: Nov 07, 20158Year Member
Posts: 2,261
Reputation Power: 332
Status: Offline
Joined: Nov 07, 20158Year Member
Posts: 2,261
Reputation Power: 332
It's ok I got it working!

private void textBox1_TextChanged(object sender, EventArgs e)
{
    string input = textBox1.Text.TrimStart(' ');
    if (input.Length == 1)
    {
        textBox1.Text = input == "0" ? "" : input;
    }
}
#3. Posted:
Bashful
  • TTG Senior
Status: Offline
Joined: Aug 02, 201211Year Member
Posts: 1,915
Reputation Power: 77
Status: Offline
Joined: Aug 02, 201211Year Member
Posts: 1,915
Reputation Power: 77
You can also use a key event to check the value, and adjust/erase it as they type.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.