I write this code to start always with a capital letter in my textbox. its works!
But what happen? When I click on my button to clean my textbox (or combobox). I receive this message: Index was outside the bounds of the array. What happen?
Code to have the first capital letter:
private void txtdesignation_TextChanged(object sender, EventArgs e)
{
char[] v = txtdesignation.Text.ToCharArray();
string s = v[0].ToString().ToUpper();
for (int b = 1; b < v.Length; b++)
s += v[b].ToString().ToLower();
txtdesignation.Text = s;
txtdesignation.Select(txtdesignation.Text.Length, 0);
}
Code to clean my textbox:
txtdesignation.Text = "";

Michal HabalcikRank #902 · 2.3K reputationPosted Dec 5, 2014, 9:18 PM EST
if (!String.IsNullOrEmpty(txtDesignation.Text)){
VulpesRank #55 · 29.2K reputationPosted Dec 5, 2014, 10:38 AM EST
You then get an exception on this line because there isn't a first character:
string s = v[0].ToString().ToUpper();
To avoid this problem, just add the highlighted line:
Kunal VaishyaRank #990 · 2K reputationPosted Dec 5, 2014, 7:53 PM EST
char[] v = txtdesignation.Text.ToCharArray();
if (v.Length > 0)
{
s = v[0].ToString().ToUpper();
for (int b = 1; b < v.Length; b++)
s += v[b].ToString().ToLower();
txtdesignation.Text = s;
txtdesignation.Select(txtdesignation.Text.Length, 0);
}
Michal HabalcikRank #902 · 2.3K reputationPosted Dec 5, 2014, 8:39 PM EST
if (!String.IsNullOrEmpty(txtDesignation.Text){
}
IsraelRank #1,223 · 1.5K reputationPosted Dec 5, 2014, 9:14 PM EST
Your code is working but its bugging here: if (v.Length > 0). when I am trying to rewrite on the same textbox. Its gives the same error like this: Index was outside the bounds of the array
@Michael
Your code is sturking here:
if (!String.IsNullOrEmpty(txtDesignation.Text){
@Vupes
Your code is working without problem.
IsraelRank #1,223 · 1.5K reputationPosted Dec 5, 2014, 9:29 PM EST
Now its works!
Vulpes and Michael, your codes works perfectly.
Many thanx!