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. .NET (Core and Framework)
  4. Problems with a text box & changing alignment in the leave event.

Problems with a text box & changing alignment in the leave event.

Scheduled Pinned Locked Moved .NET (Core and Framework)
helpcsharp
3 Posts 3 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.
  • A Offline
    A Offline
    adarobwpg
    wrote on last edited by
    #1

    I have two textboxes on a form and have the following code in the Leave event of one of them. The problem is that once the textbox has focus, you can't leave it if the textalign property has changed. I need a way around this without setting focus to the next control manually. I still have to see if is framework or language related with the same code in C#. HELP!!:confused: Private Sub TextBox1_Leave(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles TextBox1.Leave If TextBox1.TextAlign = HorizontalAlignment.Left Then TextBox1.TextAlign = HorizontalAlignment.Right Else TextBox1.TextAlign = HorizontalAlignment.Left End If End Sub

    G 1 Reply Last reply
    0
    • A adarobwpg

      I have two textboxes on a form and have the following code in the Leave event of one of them. The problem is that once the textbox has focus, you can't leave it if the textalign property has changed. I need a way around this without setting focus to the next control manually. I still have to see if is framework or language related with the same code in C#. HELP!!:confused: Private Sub TextBox1_Leave(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles TextBox1.Leave If TextBox1.TextAlign = HorizontalAlignment.Left Then TextBox1.TextAlign = HorizontalAlignment.Right Else TextBox1.TextAlign = HorizontalAlignment.Left End If End Sub

      G Offline
      G Offline
      Gerald Leslie Jones
      wrote on last edited by
      #2

      when you change the textalign property, it will again set the focus to the current control(textbox in our example). So you should not change the alignment in the leave event... If you explain your requirement in detail, I hope I would be able to help you clearly.

      S 1 Reply Last reply
      0
      • G Gerald Leslie Jones

        when you change the textalign property, it will again set the focus to the current control(textbox in our example). So you should not change the alignment in the leave event... If you explain your requirement in detail, I hope I would be able to help you clearly.

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

        At a guess, the alignment is set to right by default, but on editing is set to left, then back to right in the leave event (somewhat erroneously). The problem is most likely with the order of events. When using the keyboard, the events are fired in the following order: Enter GotFocus (user enters data) Leave (user has tabbed away) Validating Validated LostFocus When you use the mouse or call the Focus method, the events are fired in the following order: Enter GotFocus (user enters data) LostFocus (user clicks on other control) Leave Validating Validated So, if you handle the Enter and LostFocus events, what you want works when using the keyboard. If you use the likely pairs together (like Enter and Leave), when you change the TextAlign property the handle for the text control is recreated. If it had the focus (which for keyboard events, it still does) the focus is again set to the TextBox. The trick is to change the alignment at the correct time based on whether or not a mouse button was clicked: // Hook-up event handlers. myTextBox.Enter += new EventHandler(myTextBox_Enter); myTextBox.Leave += new EventHandler(myTextBox_Leave); myTextBox.LostFocus += new EventHandler(myTextBox_LostFocus); // Handle events. private void myTextBox_Enter(object sender, EventArgs e) { myTextBox.TextAlign = HorizontalAlignment.Left; } private void myTextBox_Leave(object sender, EventArgs e) { if (Control.MouseButtons != MouseButtons.None) myTextBox.TextAlign = HorizontalAlignment.Right; } private void myTextBox_LostFocus(object sender, EventArgs e) { if (Control.MouseButtons == MouseButtons.None) myTextBox.TextAlign = HorizontalAlignment.Right; } Hope this helps Steve S (One of these days I really must start using this stuff properly...)

        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