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. Windows Forms
  4. Two TextBoxs and only one can contain any text at a given time [modified]

Two TextBoxs and only one can contain any text at a given time [modified]

Scheduled Pinned Locked Moved Windows Forms
csharpwinformshelptutorialquestion
3 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.
  • S Offline
    S Offline
    Steve Messer
    wrote on last edited by
    #1

    I have a Winforms application where I have to Textboxes. I only want one of them to have text at any given time. So, I added a TextedChange event to each of these TextBoxes that empties the text of TextBox that is not receiving text. The problem is this. If TextBox1 has the text "UT" and I then enter text into TextBox2. The text is correctly removed but the first press in TextBox2 is ignored so the user has to enter it in twice. Once to empty the text of the other TextBox and then again to actually have it show up in the TextBox being typed to. private void VIN_CHANGED(object sender, EventArgs e) { tbPlate.Text = string.Empty; } private void PLATE_CHANGED(object sender, EventArgs e) { tbVin.Text = string.Empty; } Anyone know how to get around the first stroke getting ignored?

    modified on Thursday, May 1, 2008 3:41 PM

    S 1 Reply Last reply
    0
    • S Steve Messer

      I have a Winforms application where I have to Textboxes. I only want one of them to have text at any given time. So, I added a TextedChange event to each of these TextBoxes that empties the text of TextBox that is not receiving text. The problem is this. If TextBox1 has the text "UT" and I then enter text into TextBox2. The text is correctly removed but the first press in TextBox2 is ignored so the user has to enter it in twice. Once to empty the text of the other TextBox and then again to actually have it show up in the TextBox being typed to. private void VIN_CHANGED(object sender, EventArgs e) { tbPlate.Text = string.Empty; } private void PLATE_CHANGED(object sender, EventArgs e) { tbVin.Text = string.Empty; } Anyone know how to get around the first stroke getting ignored?

      modified on Thursday, May 1, 2008 3:41 PM

      S Offline
      S Offline
      SomeGuyThatIsMe
      wrote on last edited by
      #2

      They both go away, becaues when you empty the other textbox it fires that box's textChanged event thus clearing the second box...it works the second time because the box is already clear. You'll probably have to use a flag to show if you should change it..or put some logic in the event to check if that box has focus i.e. private void textBox1_TextChanged (object sender, EventArgs e) { if (textBox1.Focused) { textBox2.Text = ""; } } private void textBox2_TextChanged (object sender, EventArgs e) { if (textBox2.Focused) { textBox1.Text = ""; } } this prevents the event from doing anything if its control isnt the one bieng typed in..oh i'm using .NET 3.5 i dont know when the Focused property was added...i've never had to use it before, but there should be something similar in each version. Hope this helps..if you have any more questions feel free to ask.

      S 1 Reply Last reply
      0
      • S SomeGuyThatIsMe

        They both go away, becaues when you empty the other textbox it fires that box's textChanged event thus clearing the second box...it works the second time because the box is already clear. You'll probably have to use a flag to show if you should change it..or put some logic in the event to check if that box has focus i.e. private void textBox1_TextChanged (object sender, EventArgs e) { if (textBox1.Focused) { textBox2.Text = ""; } } private void textBox2_TextChanged (object sender, EventArgs e) { if (textBox2.Focused) { textBox1.Text = ""; } } this prevents the event from doing anything if its control isnt the one bieng typed in..oh i'm using .NET 3.5 i dont know when the Focused property was added...i've never had to use it before, but there should be something similar in each version. Hope this helps..if you have any more questions feel free to ask.

        S Offline
        S Offline
        Steve Messer
        wrote on last edited by
        #3

        Thanks, you hit the nail on the head. I didn't realize that there was a domino effect going on.

        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