I am new in Visual Studio and using visual Studio 2008. In a project I want to make all text in uppercase while typed by the user without pressing shift key or caps lock on.
if you can use LinqToObjects in your Project
private YourTextBox_TextChanged ( object sender, EventArgs e)
{
return YourTextBox.Text.Where(c=> c.ToUpper());
}
An if you can't use LINQ (e.g. your project's target FW is .NET Framework 2.0) then
private YourTextBox_TextChanged ( object sender, EventArgs e)
{
YourTextBox.Text = YourTextBox.Text.ToUpper();
}
Why Text_Changed Event ?
There are few user input events in framework..
1-) OnKeyPressed fires (starts to work) when user presses to a key from keyboard after the key pressed and released
2-) OnKeyDown fires when user presses to a key from keyboard during key presses
3-) OnKeyUp fires when user presses to a key from keyboard and key start to release (user take up his finger from key)
As you see, All three are about keyboard event..So what about if the user copy and paste some data to the textbox?
if you use one of these keyboard events then your code work when and only user uses keyboard..in example if user uses a screen keyboard with mouse click or copy paste the data your code which implemented in keyboard events never fires (never start to work)
so, and Fortunately there is another option to work around : The Text Changed event..
Text Changed event don't care where the data comes from..Even can be a copy-paste, a touchscreen tap (like phones or tablets), a virtual keyboard, a screen keyboard with mouse-clicks (some bank operations use this to much more security, or may be your user would be a disabled person who can't press to a standard keyboard) or a code-injection ;) ..
No Matter !
Text Changed event just care about is there any changes with it's responsibility component area ( here, Your TextBox's Text area) or not..
If there is any change occurs, then your code which implemented under Text changed event works..