Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Handle KeyPress Event

Handle KeyPress Event

Scheduled Pinned Locked Moved C#
helpcsharpdata-structurestutorialquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mfcuser
    wrote on last edited by
    #1

    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.

    A 1 Reply Last reply
    0
    • M mfcuser

      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.

      A Offline
      A Offline
      Alex Korchemniy
      wrote on last edited by
      #2

      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];

      M 1 Reply Last reply
      0
      • A Alex Korchemniy

        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];

        M Offline
        M Offline
        mfcuser
        wrote on last edited by
        #3

        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

        A 1 Reply Last reply
        0
        • M mfcuser

          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

          A Offline
          A Offline
          Alex Korchemniy
          wrote on last edited by
          #4

          Sorry I was doing this of the top of my head. Just cast to string to fix this: textbox.Text = (string)h[textbox.Text];

          M 1 Reply Last reply
          0
          • A Alex Korchemniy

            Sorry I was doing this of the top of my head. Just cast to string to fix this: textbox.Text = (string)h[textbox.Text];

            M Offline
            M Offline
            mfcuser
            wrote on last edited by
            #5

            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).

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups