Click here to Skip to main content
16,020,669 members
Please Sign up or sign in to vote.
1.12/5 (3 votes)
See more:
hi everybody i have atextbox which have two events first txtsearch_TextChanged which fire to autocomplete the other is txtsearch_KeyPress which pass data in click enter txtsearch_TextChanged stop txtsearch_KeyPress firing

how can i make both of them work one after the other textchanged first then keypress event without problem ?

EDIT: Copied from comments:

C#
private void txtsearch_KeyPress(object sender, KeyPressEventArgs e)
{


if (e.KeyChar == 13)
{

string Str = txtsearch.Text.Trim();
double Num;
bool isNum = double.TryParse(Str, out Num);
if (isNum)
{

// search by barcode

prd = new BLL();
DataTable dta = new DataTable();

dta = prd.findbybarcode(txtsearch.Text);

txtid.DataBindings.Clear();
txtid.DataBindings.Add("text", dta, "ID");

txtproductname.DataBindings.Clear();
txtproductname.DataBindings.Add("text", dta, "proname");

txtprice.DataBindings.Clear();
txtprice.DataBindings.Add("text", dta, "price");

txtunit.DataBindings.Clear();
txtunit.DataBindings.Add("text", dta, "Unit");

txtcount.Focus();

}

}
}


private void txtsearch_TextChanged(object sender, EventArgs e)
{
txtsearch.AutoCompleteMode = AutoCompleteMode.Suggest;
txtsearch.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection DataCollection = new AutoCompleteStringCollection();
getData(DataCollection);
txtsearch.AutoCompleteCustomSource = DataCollection;


}
Posted
Updated 28-Apr-15 11:44am
v2
Comments
CHill60 28-Apr-15 12:38pm    
There must be something in your code preventing this from working, KeyPress will fire before TextChanged. Use the Improve question link to share the code in those two events
ost3z 28-Apr-15 13:05pm    
textchanged fired before keypress and stopped key press from working
Karthik_Mahalingam 28-Apr-15 13:23pm    
post your code..
ost3z 28-Apr-15 16:51pm    
private void txtsearch_KeyPress(object sender, KeyPressEventArgs e)
{


if (e.KeyChar == 13)
{

string Str = txtsearch.Text.Trim();
double Num;
bool isNum = double.TryParse(Str, out Num);
if (isNum)
{

// search by barcode

prd = new BLL();
DataTable dta = new DataTable();

dta = prd.findbybarcode(txtsearch.Text);

txtid.DataBindings.Clear();
txtid.DataBindings.Add("text", dta, "ID");

txtproductname.DataBindings.Clear();
txtproductname.DataBindings.Add("text", dta, "proname");

txtprice.DataBindings.Clear();
txtprice.DataBindings.Add("text", dta, "price");

txtunit.DataBindings.Clear();
txtunit.DataBindings.Add("text", dta, "Unit");

txtcount.Focus();

}

}
}


private void txtsearch_TextChanged(object sender, EventArgs e)
{
txtsearch.AutoCompleteMode = AutoCompleteMode.Suggest;
txtsearch.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection DataCollection = new AutoCompleteStringCollection();
getData(DataCollection);
txtsearch.AutoCompleteCustomSource = DataCollection;


}
Sergey Alexandrovich Kryukov 28-Apr-15 17:18pm    
First of all, it's not the evidence that txtsearch_KeyPress was added to any invocation list of any control really included in the UI being executed. You probably assume that if the method takes this exact name, the designer took care of that. It could be so when everything goes write, but if something wrong, you need to make sure this method is really used as a right operand in appropriate "+=" operator. Did you take care of that?

There is no single += in your code sample, so it does not tell the story. Also, use the debugger.

Now, comment is not a right place for code samples; put in in the question instead, using "Improve question".

—SA

1 solution

I've experimented with your code.

As far as I can tell the Enter key is no longer handled once txtsearch.AutoCompleteMode = AutoCompleteMode.Suggest; has been run. Which sort of makes sense - the Enter key would be used to pick from the list of possible auto-complete entries.

As you are not filtering the autocomplete collection, why not set that up in one of the Form_Load or txtsearch_Enter events? The Enter key will still not work, but the behaviour of the text box is better.

I have no idea what the key_leave event is. If you meant the txtSearch_Leave event it won't help with your autocomplete.

If you really want to capture the Enter key then try using the txtsearch_KeyUp event instead

I also experimented with the order of the events that were fired by playing about with the order that I wired them up. It doesn't appear to be relevant - for example this order of wiring up the events
C#
this.txtsearch.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txtsearch_KeyUp);
this.txtsearch.TextChanged += new System.EventHandler(this.txtSearch_TextChanged);
this.txtsearch.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtSearch_KeyPress);
still resulted in the order of events
VB
key pressed
text changed
key up
(As did the other combinations of order that I tried)
 
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