Hey guy,I would like to do a number jumping just like the picture.In the textbox,it will show number "3".When the button is click,the textbox number "3" will change to "2" then click again it will change to "1".How to do it?! I had try this code but useless(i know that code is error)

for (int i = 2; i >= 3; i--)
            {
                if (i == 0)
                {
                    MessageBox.Show("Please try again", "Error");

                }
            }

Recommended Answers

All 9 Replies

Put this in your button click handler

private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 3; i >= 0; i--)
            {
                if (i ==0)
                {
                    MessageBox.Show("Please try again", "Error");
                }
                else 
                {
                    textBox1.Text = i.ToString();
                    MessageBox.Show("Continue?", "OK");
                }
            }
        }

if i don't want to pop out the message box

MessageBox.Show("Continue?", "OK");

how to solve it?Just the number change only without any messagebox pop up.Just at the end of it become "0" then pop out the messagebox "please try again".

Well, ever heard of deleting a line?

I had try delete this

MessageBox.Show("Continue?", "OK");

But i run the code,it work without passing "2".It jump to "1" and pop up messagebox "please try again"!!!

Yes, that's exactly what your code does.
If you press the OK button, the for loop decrements a counter and sets the textbox text. The moment the counter gets 0 the messagebox pops up.
Don't know exactly what you are trying to do here. Could you clarify?

Oh I see~Actually i want to make it as verify something...for example: If the data insert wrongly there had 3 chance to try...

You got to use the Validating event and eventually an ErrorProvider. Example.

As an example, I leave the ErrorPorvider to you.

namespace WindowsFormsApplication1
{
    enum Valid
    {
        correct, wrong
    }

    public partial class Form1 : Form
    {
        public int tries = 3;

        public Form1()
        {
            InitializeComponent();            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private Valid TestInput(string inStr)
        {
            //Your validation check here:
            if (inStr == "danny")
            {
                return Valid.correct;
            }
            return Valid.wrong;
        }

        private void textBox1_Validating(object sender, CancelEventArgs e)
        {
            if (tries > 0)
            {
                if (TestInput(textBox1.Text) == Valid.wrong)
                {      
                    tries--;
                    textBox1.Clear();
                    MessageBox.Show("Please try again", "Error");
                    e.Cancel = true; // we try again
                }
            }
            else
            {
                MessageBox.Show("Sorry, no more tries", "Error");
                Application.Exit();
            }
        }

        private void textBox1_Validated(object sender, EventArgs e)
        {
            if (tries != 0)
            { 
                MessageBox.Show("Well done, you managed to enter a valid input!", "Validation OK");
            }            
        }       
    }

thanks you for helping me so much!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.