mustang29

I am new in Visual c# and I want to make een program that, when in have for example ABC in textbox and I click on an button, that in the second textbox came the A, in the third the B and in the fourth the C.

I have tryed already some things, but with no result.

Thanks already.



Re: Visual C# Express Edition split textbox.text into each character seperatly

jrboddie

Did you try something like this:

textBox2.Text = textBox1.Text.Substring(2, 1);

This copies the third character of textBox1 into textBox2.

See this article.





Re: Visual C# Express Edition split textbox.text into each character seperatly

Paul Domag

Hi,

You can also ust the toCharArray() this will split the string into seperate characters and store it in a char array.

string sample = "Hello";
char[] letters = sample.ToCharArray();
for (int i = 0; i < letters.Length; i++) {
MessageBox.Show(lettersIdea.ToString());
}

cheers,

Paul June A. Domag






Re: Visual C# Express Edition split textbox.text into each character seperatly

mustang29

thanks both for the respons.

my program works now. But I have still a problem. I want to let it work with a timer. So when the timer tick, first, the first character comes in a textbox and after the interval, the second character comes in the same textbox. ...

I tried with a for-loop, but I always only get the last character.

thx





Re: Visual C# Express Edition split textbox.text into each character seperatly

jrboddie

One way to do it:

Code Snippet

private void button1_Click(object sender, EventArgs e)

{

timer1.Interval = 1000;

index = 0;

timer1.Start();

textBox2.Clear();

}

int index;

private void timer1_Tick(object sender, EventArgs e)

{

if (index<textBox1.Text.Length )

{

textBox2.AppendText(textBox1.Text.Substring(index++,1));

}

else

{

timer1.Stop();

}

}





Re: Visual C# Express Edition split textbox.text into each character seperatly

Paul Domag

Hi,

Try this:

char[] letters;
int counter = 0;

public Form1() {
InitializeComponent();

string sample = "Hello World";
letters = sample.ToCharArray();
}

private void timer1_Tick(object sender, EventArgs e) {
if (counter >= letters.Length) {
counter = 0;
textBox1.Text = "";
}
textBox1.Text = textBox1.Text + letters[counter];
counter++;
}

Just ask any questions if you have a problem with the code provided...

cheers,

Paul June A. Domag






Re: Visual C# Express Edition split textbox.text into each character seperatly

mustang29

I understand what you do and the code works fine, but I still have a problem. Now the program works that the second character come after the first character, and at the end you see al the characters. But I want only to see one character. So when the timer ticks, the first character is in an textbox, when the timer ticks again the first character dissapear and the second character comes in the textbox, ... And when I came at the last character that it stops.

That's what I mean that I tryed whith a for-loop.

thx





Re: Visual C# Express Edition split textbox.text into each character seperatly

jrboddie

In my code example just change:

textBox2.AppendText(textBox1.Text.Substring(index++,1));

to

textBox2.Text(textBox1.Text.Substring(index++,1));

and you will see only one character at a time.





Re: Visual C# Express Edition split textbox.text into each character seperatly

Paul Domag

Hi,

Change it to this:

char[] letters;
int counter = 0;

public Form1() {
InitializeComponent();

string sample = "Hello World";
letters = sample.ToCharArray();
}

private void timer1_Tick(object sender, EventArgs e) {
if (counter >= letters.Length) {
counter = 0;
textBox1.Text = "";
}
textBox1.Text = letters[counter];
counter++;
}

Note:

Both my source code and jrboddie's work just fine. we're just here to give you alternatives.

cheers,

Paul June A. Domag






Re: Visual C# Express Edition split textbox.text into each character seperatly

mustang29

thx both. It works fine now. the result that I want. But the code from jrboddie don't work with my program. But it is soved.

thanks a lot.