Event to return value

  • Thread starter Thread starter John S
  • Start date Start date
J

John S

I have an event handler tied to a text box keypress event. All I am trying
to do is have it read the key and return it in upper case. Because the
event doesn't return anything, I cannot use the keyword "return". I thought
I could do it this way.
Here is the code.

public static void uCaseReturn(object sender,
System.Windows.Forms.KeyPressEventArgs e){

TextBox tb = (TextBox) sender;

string tb1 = tb.Text.ToUpper();

tb.Text=tb1;

}
 
You are correct in that you cannot return a value from this event.

I'm not sure why your event handler is not specific to the text box you instantiated. For instance if your text box is named myTextBox, then the standard naming convention for the key press event handler should look like this:

private void myTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
myTextBox.Text = myTextBox.Text.ToUpper();
}

and your class constructor should have:

myTextBox.KeyPress +=new KeyPressEventHandler(tbSearch_KeyPress);


Does this help?
Steve
 
Would setting the .CharacterCasing property of the textbox to Upper work for
you?

If not, just create an eventhandler for KeyPress and use it like so:

private void textBox1_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)

{

e.Handled = true;

((TextBox)sender).Text += e.KeyChar.ToString().ToUpper();

}


--

W.G. Ryan, eMVP

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/community/newsgroups
 
See the CharacterCasing property on the TextBox control for an easy way to
force all letters to caps.

Ken
 
See comments inline...
private void textBox1_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)

{

e.Handled = true;

((TextBox)sender).Text += e.KeyChar.ToString().ToUpper();

}

The event handler code above isn't sufficient because you don't know that
when the user pressed the key that the cursor is at the end of the textbox.
You'll need to use the SelectionStart and SelectionLength properties to
ensure that your character is placed in the correct location (and whatever
text was selected is removed), e.g.

public static void uCaseReturn(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
if (!Char.IsControl(e.KeyChar))
{
TextBox tb = (TextBox) sender;
int charPosition = tb.SelectionStart;

// Build the new text for the box by replacing the selected text
with the character (in uppercase)
tb.Text = tb.Text.Substring(0, charPosition) +
Char.ToUpper(e.KeyChar) +
tb.Text.Substring(tb.SelectionStart + tb.SelectionLength);

// Place the cursor in the correct location
tb.SelectionLength = 0;
tb.SelectionStart = charPosition;

// Mark the keystroke as being handled
e.Handled = true;
}
}

Ken
 
John said:
I have an event handler tied to a text box keypress event. All I am trying
to do is have it read the key and return it in upper case. Because the
event doesn't return anything, I cannot use the keyword "return". I thought
I could do it this way.
Here is the code.

public static void uCaseReturn(object sender,
System.Windows.Forms.KeyPressEventArgs e){

TextBox tb = (TextBox) sender;

string tb1 = tb.Text.ToUpper();

tb.Text=tb1;

}

When the KeyPressed event is fired, the character for that key has not
yet been added to the Text property.

An easier way to force a TextBox to be uppercase is to set the TextBox's
CharacterCasing property;

tb1.CharacterCasing = CharacterCasing.Upper;

If you need to do fancier manipulation of the Text property, you should
probably handle it in the TextChanged event.
 
Yes, that should certainly work as long as you are careful to reposition the
caret to where it was at the start of the event. I was just trying to show
all that you'd have to do to use the KeyPress event since there are cases
when TextChanged wouldn't be appropriate (e.g. if you decide to filter out
certain characters). As a few people have mentioned, the easiest way to
solve his immediate problem is with the CharacterCasing property.

Ken
 

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

Back
Top