473,418 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

delaying TextBox.TextChanged event

VMI
I'm trying to some dynamic searching to my windows Form so I added code in
my TextBox.TextChanged so that, after every text change, a search is done to
a datatable. So if I want to type the string "Memphis", it'll do a search
(and display) all the items after the user types "M", then another search
(of "Me") when the user types "e", another search of "Mem" after "M", and so
on. The problem is that there's so much data in the table that each one of
these searches is very slow, and I can't use a primary key because it
searches in all columns (there's three).
Is there any way that I can delay the TextChange so it only executes if the
user doesn't type anything for, say, a second? Or is there any way I can do
this?

Thanks.
Nov 16 '05 #1
2 18045
One idea would be to have a timer object on the form set to the time you
want to the delay to be.

From here you could reset the timer everytime a valid key is pressed,
changing the value of your textbox. If the event of the timer fires, and you
have some text then I'd advise starting another thread to populate your
datatable.

You need to be careful of things like, what happens when you press a key
while the thread is busy populating the data table... clearly the thread
will have to be aborted before the new one can start, otherwise you'll have
two threads trying to populate the same datatable.

If you want to read up on multithreading, Jon Skeet's got a good article on
it:
http://www.yoda.arachsys.com/csharp/threads/

A complete alternative to all this is scrap the idea of searching after each
key press, even with a delay, and having a find button. This method holds
more weight, especially since you have a large dataset with no efficient way
of searching it.

Just hallow if you need any help.

"VMI" <vo******@yahoo.com> wrote in message
news:un**************@TK2MSFTNGP09.phx.gbl...
I'm trying to some dynamic searching to my windows Form so I added code in
my TextBox.TextChanged so that, after every text change, a search is done
to a datatable. So if I want to type the string "Memphis", it'll do a
search (and display) all the items after the user types "M", then another
search (of "Me") when the user types "e", another search of "Mem" after
"M", and so on. The problem is that there's so much data in the table
that each one of these searches is very slow, and I can't use a primary
key because it searches in all columns (there's three).
Is there any way that I can delay the TextChange so it only executes if
the user doesn't type anything for, say, a second? Or is there any way I
can do this?

Thanks.

Nov 16 '05 #2
One suggestion is to use the System.Timers.Timer like so:

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

timer.AutoReset = false;
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
}

System.Timers.Timer timer = new System.Timers.Timer( 500 );
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
timer.Stop();
timer.Start();
}

private void timer_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
Console.WriteLine( "Do something here." );
}

If you were getting really excited about it, you could write an
IExtenderProvider component to so you could easily add this
functionality to any textbox...

Nov 16 '05 #3

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

Similar topics

1
by: David Smith | last post by:
What I want to be able to do: A textbox is available that the user can enter information into. Specifically (for the purposes of this post), the user is asked to enter a number, and that number...
5
by: Vi | last post by:
Hello, I want to be able to reload a DropDownList when a TextBox changes its value. So I set the AutopostBack property of the TextBox to true and in the code behind I do something like: ...
4
by: Craig G | last post by:
i have the following code for validating a textbox once a page posts, but the problem i am having is that the TextChanged event won't fire next time if the user does not change the value again ...
4
by: MattB | last post by:
Hi. I'm working on an intranet application that requires a user to input information about themselves. I have a user control with a couple of textboxes that I want the user to enter their weight...
3
by: ton | last post by:
How can I detect that the user has modified the data on the page where several textboxes, checkboxes and dropdownlist exist. Ton
7
by: Itar | last post by:
I'm having a problem supressing characters in a text box. I only want alpha numeric characters (no special chars). I can handle the TextBox_KeyPress event to supress the invalid characters when...
10
by: Dennis | last post by:
I have a simple form with one button and one text box. In the Form, I create an array list to track the events by adding a descriptive string item to the arraylist in each event. I first Click on...
3
by: DotNetNewbie | last post by:
I am reading the book Teach Yourself Microsoft Visual Basic .Net 2003 in 21 Days. I am having trouble getting one of the exercises to work at the end of day 4. Exercises: 1. Create a new...
4
by: Jason Huang | last post by:
Hi, In my C# Windows form MyForm I have some TextBoxes. In these TextBoxes, we have to detect if the TextChanged event occurs, if there're changes in these TextBoxes, it will ask if we want to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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...

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.