By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,136 Members | 1,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,136 IT Pros & Developers. It's quick & easy.

Responding to textbox user input

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.