473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

KeyPress Event

I have the snippet of code at the end of this message that has the following
line of code in it:
txtYourFirstname.SelectedText=Convert.ToString(Cha r.ToUpper(e.KeyChar));
The problem I am having is when it converts lowercase to upper case it also
leaves the lowercase character in the textbox. In other words, if you type
the letter 'q' in the textbox it will convert it to uppercase but show 'Qq'.
What am I doing wrong, and how can I correct it? Also, I'm just starting to
learn C#.
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
{
txtYourFirstname.SelectedText=Convert.ToString(Cha r.ToUpper(e.KeyChar));
}
}
Nov 15 '05 #1
3 7728
You are setting TextBox.SelectedText rather than TextBox.Text.

If you just want to force all input to be upper-case, you can use the
CharacterCasing property of the TextBox.

P.

--
www.CL4.org
Nov 15 '05 #2
Thanks Paul. The CharacterCasting property worked.

I did try textbox.text, but it did the samething as the textbox.Selectedtext
did. I choose the Selectedtext property over the text property because it
put the cursor at the end of the line. The text property for some reason
put it at the begining.
"Paul E Collins" <fi******************@CL4.org> wrote in message
news:c1**********@titan.btinternet.com...
You are setting TextBox.SelectedText rather than TextBox.Text.

If you just want to force all input to be upper-case, you can use the
CharacterCasing property of the TextBox.

P.

--
www.CL4.org

Nov 15 '05 #3
- Setting Text replaces the entire contents of the TextBox.
- Setting SelectedText replaces only the user's highlighted selection.

Since there was no highlighted selection in the box, setting SelectedText
will *insert* the text at the cursor position.

By the way, you can use that "KeyPressEventArgs e". If you set e.Handled =
true, the keystroke will be considered to have been processed, and the
result of pressing that key won't automatically appear in the textbox.

P.

--
www.CL4.org
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Darryn Ross | last post by:
Hi, I am trying to catch the KeyPress event on my datagrid but it isn't working... i have also tried registering the handler with the event like this... dgGLBatch.KeyPress += new...
1
by: Rene | last post by:
Hi, I am running is some problems with the KeyPreview and KeyPress events. The KeyPress event is only triggered when there this an focusable control on the form. When all controls are disabled...
4
by: Tom | last post by:
I have a VB.NET user control that I wrote - this control has three or four other controls on it (textbox, combobox, datetime picker, etc). Now, whenever the control is on a form and the user enters...
15
by: Adam J. Schaff | last post by:
I have noticed that if a user closes a form via pressing return (either while the OK button has focus or if AcceptButton is set to OK for the form) then the "ENTER" keypress event fires ON THE...
3
by: Terry Olsen | last post by:
Is there anyway to cause a KeyPress event in a datagrid cell? The only way I get a keypress event is if none of the cells are selected. I was hoping to have the program respond to a certain...
3
by: Fia | last post by:
Hi In Visual Basic 6 I could call keypress with an integer of a choosen key. For example I could call a textbox's keypress with the number 13 (for the enter key). But now in Visual Basic .Net I...
3
by: Nikolay Evseev | last post by:
Hi, I am trying to trace down the Enter key in my Form.KeyPress event handler. The KeyPreview property is set to false, so I'd assume that all key presses should go through my form's KeyPress...
5
by: Henry Jones | last post by:
I am new to C# and wanted to capture the KeyPress for a textbox. I created some code as follows: private void textBox3_KeyPress(object sender, System.EventArgs e) { this.textBox2.Text =...
2
by: Tony Johansson | last post by:
Hello! I have created a Control that consist of a label and a textbox.I have called this class ctlLabelTextbox. public partial class ctlLabelTextbox : UserControl { .... } The class that I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.