Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
2.60/5 (5 votes)
Hi,
I am developing an application for which i have included seach files in given folder one like in Windows 7 start menu. in which i want to search it on Textbox_TextChanged Event but i dont know whether i need to fire any query on anything. Please tell me what should i do?

I have to show result in DataGridView and any file type with extension. result should change on Textbox_TextChanged
Posted

 
Share this answer
 
In the Visual Studio designer, highlight your TextBox.
Look at the Properties pane.
Click the button that looks like a lightning bolt - that will show you the Events instead of the Properties.
Scroll down to TextChanged. Double click it.
That will hook up an event handler for you, and take you to the method that is called when the user changes the text in the text box.

You can now write your code in the method - when the user types in the TextBox, that method will be called for each character.
 
Share this answer
 
Comments
javedsmart 6-Apr-12 6:14am    
I think you have not read the question correctly.
I know how to get TextChange event i want to know how i can search files in folder and show result in DataFridView.
OriginalGriff 6-Apr-12 6:27am    
I think you've not written it properly! :laugh:
What bit is giving trouble? What is the users input supposed to do when it changes?
(Remember I can't see your screen, so anything you don't tell me I have to make up...)
javedsmart 9-Apr-12 2:49am    
:-) Sorry OriginalGriff for late reply.
Actually all i want is show all files in DataGridView as i enter text in TextBox and show all related files in DataGridView from all drives. Same as in Windows 7 Start Menu search which shows all related files from complete drive.
OriginalGriff 9-Apr-12 4:36am    
Oh boy! Not a simple request at all!
I've not tried to do this, but if you are after similar functionality to the Start menu textbox, then you will have to do some reading:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa965362(v=vs.85).aspx
Good luck! I don't think this is a ten minute job... :)
The first thing to do is add this to the using section if not there already:
C#
using System.IO;
using System.Data;


To get the files from a folder, you can use:
C#
string[] foundFiles = Directory.GetFiles(thePathVariable, theSearchBoxText + "*.*");


You have to change "thePathVariable" and "theSearchBoxText" to suite your needs.
By appending the wildcard string ("*.*") you're searching for every file with any extension that starts with the text written in your search box. You can play with the wildcards to achieve any other needs.

To display the results into a datagridview, we have to transfer the data from the string array to a datatable. This can be also achieved easily with LINQ, but seems you're not quite there, so:

C#
DataTable s = new DataTable();
s.Columns.Add("FileName");

foreach (string aFile in foundFiles)
{
   s.Rows.Add(aFile);
}
dataGridView1.DataSource = s;


And that's it, hope it helps.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900