473,503 Members | 2,322 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Responding to textbox user input

jd
I have several textboxes in which the end user can enter values. When the user presses the Enter key
when in any of the textboxes, or leaves that textbox, I want a routine to run (mathematical analysis
followed by plotting a graph). Since it is relatively slow (less than a second, but makes the
updating sluggish) I only want it to be triggered when the value in one of the textboxes is actually
changed by the user - pressing Enter or leaving a textbox when its value has not been changed should
not trigger the slow routine.

I've figured out some logic for this, but wondered if there's a simpler way to do it (eg some
Textbox members that I've overlooked):

tbx_Enter
- get textbox value (as a string) in the textbox entered, and store it in a state variable.

tbx_KeyDown
- check If return was pressed
- if so, check if value for that textbox has changed compared to state variable. If so, copy the new
value to the state variable and run slow routine. If not, do nothing.

tbx_Leave
- check if value has changed compared to state variable. If so, run slow routine. If not, do
nothing.

Note that the user could modify the value and press Enter several times without leaving a particular
textbox, which is why the new value is copied to the state variable and the slow routine is run
whenever Enter is pressed.

Also, if there's a flaw in my logic, feel free to comment.
Sep 12 '08 #1
2 3708
Hi JD,

Just check the "KeyDown" event. It fires before the text is actually
changed, so you can evaluate the text and respond to it then.

Other option:
create a global variable just above a TextBox_Changed event handler, and
compare to that:

private string m_textBoxValue;
private void TextBox1_Changed(object sender, EventArgs e)
{
bool changed = (TextBox1.Text != m_textBoxValue);
if (changed == true)
{
m_textBoxValue = TextBox1.Text;
using (BackgroundWorker bg = new BackgroundWorker()) {
worker.DoWork += delegate {
YourCalculationMethod(m_textBoxValue);
};
worker.RunWorkerCompleted += delegate {
YourMethodToUpdateYourForm();
Cursor = Cursors.Default;
}
worker.RunWorkerAsync();
if (worker.IsBusy == true) {
Cursor = Cursors.AppStarting;
}
}
}
}

"jd" wrote:
I have several textboxes in which the end user can enter values. When the user presses the Enter key
when in any of the textboxes, or leaves that textbox, I want a routine to run (mathematical analysis
followed by plotting a graph). Since it is relatively slow (less than a second, but makes the
updating sluggish) I only want it to be triggered when the value in one of the textboxes is actually
changed by the user - pressing Enter or leaving a textbox when its value has not been changed should
not trigger the slow routine.

I've figured out some logic for this, but wondered if there's a simpler way to do it (eg some
Textbox members that I've overlooked):

tbx_Enter
- get textbox value (as a string) in the textbox entered, and store it in a state variable.

tbx_KeyDown
- check If return was pressed
- if so, check if value for that textbox has changed compared to state variable. If so, copy the new
value to the state variable and run slow routine. If not, do nothing.

tbx_Leave
- check if value has changed compared to state variable. If so, run slow routine. If not, do
nothing.

Note that the user could modify the value and press Enter several times without leaving a particular
textbox, which is why the new value is copied to the state variable and the slow routine is run
whenever Enter is pressed.

Also, if there's a flaw in my logic, feel free to comment.
Sep 12 '08 #2
jd
Hi,

Thanks for your advice.

I don't think that only checking the KeyDown event will be sufficient, since a user could paste a
value into the textbox with the mouse, so KeyDown would not be fired. For this reason, I think that
I do need to react to the textbox Enter event.

The Changed event would presumably fire when the user makes any change, ie before the user has
entered the complete value. It's for this reason that I intended to wait for the user to press Enter
or to leave the textbox before I intended to evaluate the value.

Thanks for the async code. I wasn't thinking of doing it asynchronously (mainly because I'm not yet
familiar with async working) but I may do in the future.

JD
"jp2msft" <jp*****@discussions.microsoft.comwrote in message
news:BB**********************************@microsof t.com...
Hi JD,

Just check the "KeyDown" event. It fires before the text is actually
changed, so you can evaluate the text and respond to it then.

Other option:
create a global variable just above a TextBox_Changed event handler, and
compare to that:

private string m_textBoxValue;
private void TextBox1_Changed(object sender, EventArgs e)
{
bool changed = (TextBox1.Text != m_textBoxValue);
if (changed == true)
{
m_textBoxValue = TextBox1.Text;
using (BackgroundWorker bg = new BackgroundWorker()) {
worker.DoWork += delegate {
YourCalculationMethod(m_textBoxValue);
};
worker.RunWorkerCompleted += delegate {
YourMethodToUpdateYourForm();
Cursor = Cursors.Default;
}
worker.RunWorkerAsync();
if (worker.IsBusy == true) {
Cursor = Cursors.AppStarting;
}
}
}
}

"jd" wrote:
I have several textboxes in which the end user can enter values. When the user presses the Enter
key
when in any of the textboxes, or leaves that textbox, I want a routine to run (mathematical
analysis
followed by plotting a graph). Since it is relatively slow (less than a second, but makes the
updating sluggish) I only want it to be triggered when the value in one of the textboxes is
actually
changed by the user - pressing Enter or leaving a textbox when its value has not been changed
should
not trigger the slow routine.

I've figured out some logic for this, but wondered if there's a simpler way to do it (eg some
Textbox members that I've overlooked):

tbx_Enter
- get textbox value (as a string) in the textbox entered, and store it in a state variable.

tbx_KeyDown
- check If return was pressed
- if so, check if value for that textbox has changed compared to state variable. If so, copy the
new
value to the state variable and run slow routine. If not, do nothing.

tbx_Leave
- check if value has changed compared to state variable. If so, run slow routine. If not, do
nothing.

Note that the user could modify the value and press Enter several times without leaving a
particular
textbox, which is why the new value is copied to the state variable and the slow routine is run
whenever Enter is pressed.

Also, if there's a flaw in my logic, feel free to comment.

Sep 15 '08 #3

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

Similar topics

2
13677
by: anonieko | last post by:
This applies to javascript dynamic textbox onkey > > > Newsgroups: comp.lang.javascript From: Lasse Reichstein Nielsen <l...@hotpop.com> - Find messages by this author Date: Fri, 15 Jul 2005...
9
10088
by: Jerry | last post by:
In limiting textbox input to 500 characters I would like to include a dynamic count of characters input while the user is typing into a textbox. This would obviously be a client side control,...
6
4104
by: sprayer | last post by:
I have a UI thread not responding also. I have an Async operation that I am processing a long running SQL script in ADO.Net. I am executing the Command asynchronous and hooking the...
14
14566
by: teddysnips | last post by:
WINDOWS FORMS I've a form that has a textbox that allows the user to enter a string. On the LostFocus event, the textbox formats the string into a preferred format. However, if the user...
5
13937
by: Stuart Shay | last post by:
Hello All I am working on ASP.NET 1.1 Custom Pager that allows a User to Enter a Number in a TextBox and go to the page selected. Since the OnClick Event does not work in ASP.NET 1.1 for a...
3
2588
by: Mike | last post by:
Is there a way to determine if a user deleted text in a asp:textbox? I have a textbox were users can enter in a product number, then they click a button to see if the product numbers(s) exists in...
10
8621
by: moondaddy | last post by:
I have a textbox on a webform and based on certain conditions I wanted to prevent a user from editing its text. I dont like the look of the texbox when its disabled and was wondering if there is a...
6
3716
by: Tom P. | last post by:
I'm trying to make one of our perennial favorites - The Syntax Color Editor. (Mostly as a learning exercise). I'm wondering if there is a way to capture the Paint event of a textbox so I can...
0
7207
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
7093
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
7291
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,...
1
7012
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
5598
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,...
1
5023
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...
0
3180
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...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.