473,810 Members | 3,142 Online
Bytes | Software Development & Data Engineering Community
+ 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(obj ect sender,
System.Windows. Forms.KeyPressE ventArgs e){

TextBox tb = (TextBox) sender;

string tb1 = tb.Text.ToUpper ();

tb.Text=tb1;

}
Nov 16 '05 #1
7 9994
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_KeyPr ess(object sender, KeyPressEventAr gs e)
{
myTextBox.Text = myTextBox.Text. ToUpper();
}

and your class constructor should have:

myTextBox.KeyPr ess +=new KeyPressEventHa ndler(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(obj ect sender,
System.Windows. Forms.KeyPressE ventArgs e){

TextBox tb = (TextBox) sender;

string tb1 = tb.Text.ToUpper ();

tb.Text=tb1;

}

Nov 16 '05 #2
Would setting the .CharacterCasin g 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_KeyPre ss(object sender,
System.Windows. Forms.KeyPressE ventArgs e)

{

e.Handled = true;

((TextBox)sende r).Text += e.KeyChar.ToStr ing().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********@cin fin.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.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(obj ect sender,
System.Windows. Forms.KeyPressE ventArgs 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********@cin fin.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.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(obj ect sender,
System.Windows. Forms.KeyPressE ventArgs e){

TextBox tb = (TextBox) sender;

string tb1 = tb.Text.ToUpper ();

tb.Text=tb1;

}

Nov 16 '05 #4
See comments inline...
private void textBox1_KeyPre ss(object sender,
System.Windows. Forms.KeyPressE ventArgs e)

{

e.Handled = true;

((TextBox)sende r).Text += e.KeyChar.ToStr ing().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(obj ect sender,
System.Windows. Forms.KeyPressE ventArgs e)
{
if (!Char.IsContro l(e.KeyChar))
{
TextBox tb = (TextBox) sender;
int charPosition = tb.SelectionSta rt;

// Build the new text for the box by replacing the selected text
with the character (in uppercase)
tb.Text = tb.Text.Substri ng(0, charPosition) +
Char.ToUpper(e. KeyChar) +
tb.Text.Substri ng(tb.Selection Start + tb.SelectionLen gth);

// Place the cursor in the correct location
tb.SelectionLen gth = 0;
tb.SelectionSta rt = 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(obj ect sender,
System.Windows. Forms.KeyPressE ventArgs 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.CharacterCa sing = 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***@discussi ons.microsoft.c om> wrote in message
news:EF******** *************** ***********@mic rosoft.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_KeyPre ss(object sender,
System.Windows. Forms.KeyPressE ventArgs e)

{

e.Handled = true;

((TextBox)sende r).Text += e.KeyChar.ToStr ing().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(obj ect sender,
System.Windows. Forms.KeyPressE ventArgs e)
{
if (!Char.IsContro l(e.KeyChar))
{
TextBox tb = (TextBox) sender;
int charPosition = tb.SelectionSta rt;

// Build the new text for the box by replacing the selected text
with the character (in uppercase)
tb.Text = tb.Text.Substri ng(0, charPosition) +
Char.ToUpper(e. KeyChar) +
tb.Text.Substri ng(tb.Selection Start + tb.SelectionLen gth);

// Place the cursor in the correct location
tb.SelectionLen gth = 0;
tb.SelectionSta rt = 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*******@elli emae-nospamplease.co m> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
See comments inline...
private void textBox1_KeyPre ss(object sender,
System.Windows. Forms.KeyPressE ventArgs e)

{

e.Handled = true;

((TextBox)sende r).Text += e.KeyChar.ToStr ing().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(obj ect sender,
System.Windows. Forms.KeyPressE ventArgs e)
{
if (!Char.IsContro l(e.KeyChar))
{
TextBox tb = (TextBox) sender;
int charPosition = tb.SelectionSta rt;

// Build the new text for the box by replacing the selected text
with the character (in uppercase)
tb.Text = tb.Text.Substri ng(0, charPosition) +
Char.ToUpper(e. KeyChar) +
tb.Text.Substri ng(tb.Selection Start + tb.SelectionLen gth);

// Place the cursor in the correct location
tb.SelectionLen gth = 0;
tb.SelectionSta rt = 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
7051
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
13079
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 first field of my form the "name" field and I click somewher or press tab than I loose focus and my isname() function is executed. Everything is fine but the focus was change for next field
10
3610
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 language="JavaScript" type="text/javascript"> function show(that) { if (box.style.visibility=='hidden') { that.style.visibility = 'visible'}; }
0
2962
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 such as when they do a delete. Everything is great. By and large it will be used in my repeater controls using the command event when the user clicks on it and so that event is working great. My issue is the Click event. When the control is...
1
5824
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
2347
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 can select a value from the list. Below I have some code which exist in a file named StringClass.cs
5
4417
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 name e.g. public class CBusinessObject { private CProperty<longm_propId; private CProperty<stringm_propName;
1
2208
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) { /* do something */; return false; };"> Bonus question: What search keywords could I use to find this
5
4547
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 onmouseleave / onmouseout event I've attached to my hidden drop down (in this case an <ul>), is not firing correctly. It seems that when the mouse enters the ul it fires the mouseleave event. The problem is in the hideMenu2(e) function. I'm copying...
0
9722
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10644
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10379
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10393
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7664
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6882
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4334
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.