简体   繁体   中英

C#.Net printing one character of a string at a time in a TextBox

Am new to C# and i need your help on this, I want to display one character at a time in a textbox this is my code

private void timer1_Tick(object sender, EventArgs e)
{
    int i = 0;  //why does this don't increment when it ticks again?
    string str = "Herman Lukindo";
    textBox1.Text += str[i];
    i++; 
}

private void button1_Click(object sender, EventArgs e)
{
    if(timer1.Enabled == false )
    {
        timer1.Enabled = true;
        button1.Text = "Stop";
    }
    else if(timer1 .Enabled == true )
    {
        timer1.Enabled = false;
        button1.Text = "Start";
    }
}

why does this don't increment when it ticks again?

Because your variable i is local to your event. You need to define it at class level.

int i = 0;  //at class level
private void timer1_Tick(object sender, EventArgs e)
{
    string str = "Herman Lukindo";
    textBox1.Text += str[i];
    i++; 
}

On exit of your event, variable i becomes out of scope and looses its value. On the next event it is considered a new local variable with the initialized value of 0 .

Next, you should also look for cross threaded exception. Since your TextBox is not getting updated on the UI thread.

The issue with you code is that you are assigning i = 0 with every tick, so it will always be 0 everytime it is used. I would suggest using a class level variable for this.

However, using a variable at class level means you are going to need to reset to 0 at some point, probably each time you start the timer.

A further point is that you are going to want to validate the tick event to ensure you don't try to access an index that doesn't exist ( IndexOutOfRangeException ). For this I would recommend automatically stopping the timer once the last letter has been printed.

With all that in mind, here is my suggested code:

int i = 0;// Create i at class level to ensure the value is maintain between tick events.
private void timer1_Tick(object sender, EventArgs e)
{
    string str = "Herman Lukindo";
    // Check to see if we have reached the end of the string. If so, then stop the timer.
    if(i >= str.Length)
    {
        StopTimer();
    }
    else
    {
        textBox1.Text += str[i];
        i++; 
    }
}

private void button1_Click(object sender, EventArgs e)
{
    // If timer is running then stop it.
    if(timer1.Enabled)
    {
        StopTimer();
    }
    // Otherwise (timer not running) start it.
    else
    {
        StartTimer();
    }
}

void StartTimer()
{
    i = 0;// Reset counter to 0 ready for next time.
    textBox1.Text = "";// Reset the text box ready for next time.
    timer1.Enabled = true;
    button1.Text = "Stop";
}

void StopTimer()
{
    timer1.Enabled = false;
    button1.Text = "Start";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM