473,473 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Event to return value

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;

}
Nov 16 '05 #1
7 9973
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

"John S" wrote:
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;

}

Nov 16 '05 #2
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/...ity/newsgroups
"John S" <jo********@cinfin.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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;

}

Nov 16 '05 #3
See the CharacterCasing property on the TextBox control for an easy way to
force all letters to caps.

Ken
"John S" <jo********@cinfin.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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;

}

Nov 16 '05 #4
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
Nov 16 '05 #5
John S wrote:
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.

--
mikeb
Nov 16 '05 #6
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
"Steve" <St***@discussions.microsoft.com> wrote in message
news:EF**********************************@microsof t.com...
Wouldn't it be easier to do this on the TextChanged event?
That way the .text includes the new key and you know nothing
is currently highlighted, although you still have to care for
the position of the caret. Just curious.... Steve
"Ken Kolda" wrote:
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

Nov 16 '05 #7
If you need to have everything capitalized then setting the CharacterCasing
is the way to go . I agree with you though about the positioning.

--

W.G. Ryan, eMVP

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/...ity/newsgroups
"Ken Kolda" <ke*******@elliemae-nospamplease.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

Nov 16 '05 #8

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

Similar topics

0
by: Andy Read | last post by:
Hello all, I have the requirement to produce source code that produces an object hierarchy. Example: Root | Folder 1
2
by: Bartosz Wegrzyn | last post by:
I use onblue event to validate fields in my form. I do this onblur="return isname()" and so so ... I have one form with 20 fields. My problem is that when the focus is for example on the...
10
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script...
0
by: Demetri | last post by:
I have created a web control that can be rendered as either a linkbutton or a button. It is a ConfirmButton control that allows a developer to force a user to confirm if they intended to click it...
1
by: Alex K. | last post by:
Hi all How do I catch an event when list item is deleted from ListBox? Is there such event? Thank you Alex
1
by: tony | last post by:
Hello!! Hello Victor! I use a product called flygrid to create grid tables. In many of my forms I create such grid tables. Some columns in these grid tables is of type drop down list where I...
5
by: kmcmanus | last post by:
I have just started to write a few business classes that are largely made up of properties (getters and setters). For each setter I want to fire a changed event - each event will have a unique...
1
by: mailpitches | last post by:
X-No-Archive: yes a function, the return value of the function is a boolean. What does this boolean value mean? Example: <body onload="document.getElementById('field').onkeydown=function(x) {...
5
by: jaysonnward | last post by:
Hello All: I've recently been recreating some 'dropdown menus' for a website I manage. I'm writing all my event handlers into my .js file. I've got the coding to work in Firefox, but the...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.