Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

key handling 1

Status
Not open for further replies.

ernatlas

Programmer
Jan 17, 2007
8
0
0
MX
C# Hello, i have a question. For example, i have a txtBox focused, and im writting text on it, but when i see the key '-' pressed, i want to not show that character in my textbox, and at the same time delete de last character from it. Is there any way to do?
 
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
char f = char.Parse("-");
string t = this.textBox1.Text;

if(e.KeyChar == f)
{
// Do not print the char
e.Handled = true;
// Remove the last char
this.textBox1.Text = t.Substring(0, t.Length - 1);
// Set the I-beam at the end of the text box
this.textBox1.Select(this.textBox1.Text.Length, 1);
}
}
 
FYI:
Code:
char f = char.Parse("-");
Can also be represented as:
Code:
char f = '-';
which is a little shorter to code & view, but ultimately I don't think it makes any difference, as the IL should be the same for both styles.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top