Click here to Skip to main content
15,921,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have written code for search box .its like i have taken textbox and when i enter characters into it searches whether the entered data is present in db or not and parallelly it makes the hidden listbox to be visible.in listbox iam binding the top 5 data which is retrieved from db if we select any of the 5 words came from db into listbox .then that selected word will be binded to textbox .this is WPF .so i used binding ui control with c# coding .so when i go back for evry keyup event it checks whether am using keyback or not.if it is backkey then it erases data otherwise erase of data never works.this is to restrict the bad operation that is being erased itself.
so according me i cant do this without using event handlers .but i have a question can we make this code without writing in each and evry page.is that posstible.and iam writing global variables also.to confirm whether the conditions are true or false.

What I have tried:

<textbox text={binding the listbox selecteditem.CATEGORY element name ="li"}/>
<listbox name="li" ( binding from db ) BINDING :CATEGORY/>

 private void TEXTBOX_TextChanged(object sender, TextChangedEventArgs e)
{
THIS IS TO CHECK THE DATA WHAT WE ENTERED IS PRESNT IN DB OR NOT IF PRESENT 
IT BINDS TO LISTBOX AND THE SELECTED ITEM FROM LISTBOX WILL BE BINDED TO TEXTBOX
}
 private void txtroomno_KeyUp(object sender, KeyEventArgs e)
        {
          FOR EVRY KEY UP IAM CHECKING ITS BACK KEY OR NOT.IF ITS NOT BACK KEY THEN DATA FROM TEXBOX WONT GET ERASED AND IF IT IS BACK KEY THEN DATA GETS ERASED AND FINALLY WHEN TEXTBOX IS EMPTY THEN IT CHECKS ITS LENGTH .IF LENGTH IS ZERO THEN IT MAKES LISTBOX TO BE INVISIBLE.
        }
Posted
Updated 11-Sep-17 3:12am
Comments
Graeme_Grant 7-Sep-17 4:27am    
yes.
saimanisha 7-Sep-17 4:51am    
can you please explian me how to this irrespective of control evente.and the requirement is we have to use the same code for all the forms

1 solution

So you could create a "custom Control" (not a "user Control"!) to do that (maybe inherited from TextBox),
or make it a behavior Creating custom behaviors[^] #
If you create a "custom control" better read a tutorial first because of some specials in WPF like generic theme etc. - and I'd guess (based on the style of your question) you are a WPF beginner.

Btw:
I don't get what you mean by "without eventhandlers" and "irrespective of the Control"...


OffTopic: I'd suggest you don't do a rounddtrip to the database on every textchange - maybe you can cache the database values, and just refresh them from time to time. Or you implement a kind of "auto-suggest" TextBox because it seems typing should just "search" through the possible values, and no other values can be entered.... Maybe a Combobox would be a better fit...
 
Share this answer
 

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