How i will prevent IndexOutOfRange Error if a user only input less than three letters in the textBox1?
Just check that using temp.Length:
if (temp.Length > 0)
{
...
}
... or use switch/case.
Also, you don't need the array at all. Just call ToString on each character, or use Substring:
string temp = textBox1.Text;
switch (temp.Length)
{
case 0:
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
break;
case 1:
// Via the indexer...
textBox2.Text = temp[0].ToString();
textBox3.Text = "";
textBox4.Text = "";
break;
case 2:
// Via Substring
textBox2.Text = temp.Substring(0, 1);
textBox3.Text = temp.Substring(1, 1);
textBox4.Text = "";
break;
default:
textBox2.Text = temp.Substring(0, 1);
textBox3.Text = temp.Substring(1, 1);
textBox4.Text = temp.Substring(2, 1);
break;
}
Another option - even neater - is to use the conditional operator:
string temp = textBox1.Text;
textBox2.Text = temp.Length < 1 ? "" : temp.Substring(0, 1);
textBox3.Text = temp.Length < 2 ? "" : temp.Substring(1, 1);
textBox4.Text = temp.Length < 3 ? "" : temp.Substring(2, 1);
temp.Length) and see it it's above three, otherwise show an error or do nothing.textBox3.Text = temp4;this "3" is a typo and not an error and should be "4"?