Need some help with CharacterCasing.Upper

S

Scott

I'm new to see C#, and the Code below should force lowercase characters to
uppercase when the key is pressed from my understanding. However, when the
key is first pressed it shows nothing in the textbox, but if you press it a
second time it shows the key pressed in uppercase as it should be. What am
I doing wrong, and how can I fix it so it shows the key pressed in uppercase
as soon as the key is pressed?

private void txtYourFirstName_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
//Validate for non-Character values
if(!Char.IsLetter(e.KeyChar) && !Char.IsControl(e.KeyChar))
{
e.Handled = true; //Remove/Prevent values from being entered if
non-Character
}
else
{
//Force Upper-case Characters in textbox
txtYourFirstName.CharacterCasing=CharacterCasing.Upper;

}//End if-else
}//End private void txtYourFirstname_KeyPress

Thanks,

Scott
 
C

C# Learner

Scott said:
I'm new to see C#, and the Code below should force lowercase characters to
uppercase when the key is pressed from my understanding. However, when the
key is first pressed it shows nothing in the textbox, but if you press it a
second time it shows the key pressed in uppercase as it should be. What am
I doing wrong, and how can I fix it so it shows the key pressed in uppercase
as soon as the key is pressed?

<snip>

What you're doing seems pointless -- you only need to set
TextBox.CharacterCasing to 'true' *once*, and then all characters
entered by the user will be upper-case.
 
S

Scott

What I have noticed is, that the first time I press a character it does not
show up, but if I press the same character again or a different one then
they all start showing up as the character is pressed.

For example, if you press "h", nothing shows up, but if you press "h" again
or some other Letter then it shows up in uppercase, and each letter after
that shows up in uppercase as it is pressed. In other words, if I do not
press the same character again and continue to type the rest of the letters
I would get "ECTOR", instead of "HECTOR". It seems to only happens the
first time you try to enter a letter in the textbox, and this happens for
each textbox I have.


Hector Martinez said:
I think you must be putting something bad, but your code works fine...to
me
 
S

Scott

How would I do this? Again, I'm new to C#. Do you have an example I could
see. From what I can see TextBox.CharacterCasing is not Boolean.
 
C

C# Learner

Scott said:
How would I do this? Again, I'm new to C#. Do you have an example I could
see. From what I can see TextBox.CharacterCasing is not Boolean.

You can set the property in the form designer.

Select the text box in the form designer and open the property inspector
(F4). Change 'CharacterCasing' to 'Upper' (not 'true' - sorry). This
property will appear under the category 'Behavior' if you have
categories visible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top