I'm trying to create a console application that will write an entire paragraph one letter at a time (on a timer for instance) without creating a new line everytime. My problem is that I can't figure out how to do it without if and else if statements. I thought about using an array to do it but can't remember how to read each individual slot of an array and don't exactly feel like assigning each member of the paragraph to an array index. So what I would like to learn is how to process a string one letter at a time starting with the first letter reading from left to right, writing every letter in the string individually one after the next until the paragraph is spelled out. We've all seen this in the movies I'm sure.

I'm thinking I'll need a for loop in order to make it continue throughout the process but at the same time a for loop would also write it too fast on a timer. The timer's interval wouldn't matter inside of a for loop. Either way here are the simple codes I've come up with one using the if and else if method; the other using a for loop that I really don't know where to begin at. Also I'm practicing this on a Windows Forms Application so that I can learn faster (I've replaced the console window with a label) so maybe you guys can help me out.

// if and else if

if (label1.Text == "t")
label1.Text = label1.Text + "h";

else if (label1.Text == "th")
label1.Text = label1.Text + "i";

else if (label1.Text == "thi")
label1.Text = label1.Text + "s";

// for loop

string x = "this";
int l = x.Length;

for (int i = 0; i < l; i++)
{
label1.Text = label1.Text + x.StartsWith();
x.Remove(x.StartsWith());
}

I do realize that the StartsWith() method is a boolean and will not work the way I have it coded above. I already realize and acknowledge this. The only reason it's there is as a pseudocode. Much thanks to whoever helps me figure this out.

--Taco

String x = "this";
for (int i = 1; i <= x.Length; i++) {
    label1.Text = x.Substring(0, i);
}

Of course this will go so fast you can't see it. So I created a form, put a label and timer and a button on it and added these methods:

private void button1_Click_1(object sender, EventArgs e) {
    myString = "This is a test";
    timer1.Enabled = true;

}

private int counter = 0;
private String myString = String.Empty;

private void timer1_Tick(object sender, EventArgs e) {
    counter++;
    if (counter > myString.Length) {
        timer1.Enabled = false;
        counter = 0;
    } else {
        label1.Text = myString.Substring(0, counter);
    }
}

Thank you; that actually worked very well. Now I can move further into my program. Much, much, thanks.

This article has been dead for over six months. Start a new discussion instead.