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 HabalcikPosted Dec 6, 2014, 2:18 AM
if (!String.IsNullOrEmpty(txtDesignation.Text)){
IsraelPosted Dec 6, 2014, 2:29 AM
Now its works!
Vulpes and Michael, your codes works perfectly.
Many thanx!
IsraelPosted Dec 6, 2014, 2:14 AM
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.
Michal HabalcikPosted Dec 6, 2014, 1:39 AM
if (!String.IsNullOrEmpty(txtDesignation.Text){
}
Kunal VaishyaPosted Dec 6, 2014, 12:53 AM
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);
}
VulpesPosted Dec 5, 2014, 3:38 PM
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: