All capitals by Key Press

M

Manu Singhal

Hi


I am trying to build a windows forms control which derieves from TextBox.
The added functionality that i want is that

I want every character that appears on the TextBox should be in Capitals.


For this i have created a Class iTextBox and Derieved from TextBox and have
overriden the OnKeyPress Method Here is the code :-

protected override void OnKeyPress(KeyPressEventArgs e)

{


if(Char.IsLetter(e.KeyChar))

{

e = new KeyPressEventArgs(Char.ToUpper(e.KeyChar));


}

base.OnKeyPress (e);


}



But when i test the class it the method runs but the characted is not
converted to Upper case.







Please help and advice.





Manu Singhal
 
M

Maqsood Ahmed

Hello,
You can do it with another approach.

<code lang='C#'>
protected override OnKeyPress(KeyPressEventArgs e)
{
if(Char.IsLetter(e.KeyChar) && Char.IsLower(e.KeyChar))
{
this.Text += Char.ToUpper(e.KeyChar);
this.SelectionStart = this.Text.Length;
e.Handled = true;
}
else
base.OnKeyPress(e);
}
</code>

Don't make an inherited textbox if you are doing it only for this
purpose, it can be achieved via KeyPressed event. You will only need to
ignore the 'else' part from the code in that case.

HTH. Cheers.
Maqsood Ahmed [MCP C#,SQL Server]
Kolachi Advanced Technologies
http://www.kolachi.net
 
O

Octavio Hernandez

Manu,

You can use the CharacterCasing property of the TextBox for that:

textBox1.CharacterCasing = CharacterCasing.Upper

The version you have writtten only seems to lack the assignment:

e.Handled = true;

in the event handler.

Regards - Octavio
 
V

Visually Seen #

Manu,

An even easier option is to go to the Properties window while your text
box is still selected and go down to Behaviour, and change the
CharacterCasing from Normal to Upper,

Visually Seen #
 

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