Handle KeyPress Event
-
I do have a text box where I am typing some text. I want to handlee the keypress event. I don't have any problem with that. In my program, I do have two array like array x contains something like ex btw will and array y contains example by the way williams Basically what I want, when I type "ex" in the textbox it must be replaced automatically by "example", "will" by "williams" etc. I want to know how can I handle that. I am new to c#, I can watch for a single key using the KeyPressEventArgs, so how can I hadle multiple keys. I don't know much about dictionary object in c#. I will appreciate your help.
-
I do have a text box where I am typing some text. I want to handlee the keypress event. I don't have any problem with that. In my program, I do have two array like array x contains something like ex btw will and array y contains example by the way williams Basically what I want, when I type "ex" in the textbox it must be replaced automatically by "example", "will" by "williams" etc. I want to know how can I handle that. I am new to c#, I can watch for a single key using the KeyPressEventArgs, so how can I hadle multiple keys. I don't know much about dictionary object in c#. I will appreciate your help.
Well this is weird but you can do this... Instead of using two arrays use a Hashtable: Hashtable h = new Hashtable(); h.Add("ex", "example"); Handle the TextChanged event and then do a check: if (h.ContainsKey(textbox.Text)) textbox.Text = h[textbox.Text];
-
Well this is weird but you can do this... Instead of using two arrays use a Hashtable: Hashtable h = new Hashtable(); h.Add("ex", "example"); Handle the TextChanged event and then do a check: if (h.ContainsKey(textbox.Text)) textbox.Text = h[textbox.Text];
-
I received an error from the following line of code textbox.Text = h[textbox.Text]; I received an error CS0029, Cannot implicitly convert type 'type' to 'type' So it seems like you made a mistake in the line above
Sorry I was doing this of the top of my head. Just cast to string to fix this: textbox.Text = (string)h[textbox.Text];
-
Sorry I was doing this of the top of my head. Just cast to string to fix this: textbox.Text = (string)h[textbox.Text];
Your solution works for a single line of text. For instance if my textbox is a single line, when I type "exp" it is replaced by example. However for a multiple line, I need to handle more events. I am looking at something like that, the user is typing in a multiline text box, whenever he/she types "exp" in a sentence, it will be replaced by example. In that case, I think I have to work on the kepress event to see if the spacebar and the enter key have been press. For example if the user hit spacebar after "exp" then it will be replaced by "example", the same as if the user press enter after typing "exp". So how do I handle the keypress even in order to do that. I can use a condition to check if for space and enter key which are char(13) and char(20).