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. TextBox Alignment

TextBox Alignment

Scheduled Pinned Locked Moved C#
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
    Shaun Becker
    wrote on last edited by
    #1

    I have a textbox inside a user control that I set up to be right aligned to start. Then when the textbox receives focus, I want it to align left. Then when focused is lost, I want it to restore itself back to right alignment. I can get it to align itself left when focus is given to the textbox. But, when the user tabs off the textbox, it erases the text and keeps focus on the textbox. I tied into the enter and leave events and also tried it on the gotfocus and lostfocus events but I don't understand why it is erasing the text and why it is not giving focus to the next control. Any ideas would be very helpful. Thanks

    H 1 Reply Last reply
    0
    • S Shaun Becker

      I have a textbox inside a user control that I set up to be right aligned to start. Then when the textbox receives focus, I want it to align left. Then when focused is lost, I want it to restore itself back to right alignment. I can get it to align itself left when focus is given to the textbox. But, when the user tabs off the textbox, it erases the text and keeps focus on the textbox. I tied into the enter and leave events and also tried it on the gotfocus and lostfocus events but I don't understand why it is erasing the text and why it is not giving focus to the next control. Any ideas would be very helpful. Thanks

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      The problem is with the order of events. When using the keyboard, the events are fired in the following order:

      1. Enter
      2. GotFocus
      3. Leave
      4. Validating
      5. Validated
      6. LosFocus

      When you use the mouse or call the Focus method, the events are fired in the following order:

      1. Enter
      2. GotFocus
      3. LostFocus
      4. Leave
      5. Validating
      6. 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. This is why you couldn't leave. The text disappearing is again related to the handle of the text control (which is actually a wrapper for the Edit common control) being created at the wrong time so that the text isn't being restore because it wasn't there when the handle was recreated. 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;
      }

      Seems pretty whacky, but it works.

      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

      S 1 Reply Last reply
      0
      • H Heath Stewart

        The problem is with the order of events. When using the keyboard, the events are fired in the following order:

        1. Enter
        2. GotFocus
        3. Leave
        4. Validating
        5. Validated
        6. LosFocus

        When you use the mouse or call the Focus method, the events are fired in the following order:

        1. Enter
        2. GotFocus
        3. LostFocus
        4. Leave
        5. Validating
        6. 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. This is why you couldn't leave. The text disappearing is again related to the handle of the text control (which is actually a wrapper for the Edit common control) being created at the wrong time so that the text isn't being restore because it wasn't there when the handle was recreated. 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;
        }

        Seems pretty whacky, but it works.

        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

        S Offline
        S Offline
        Shaun Becker
        wrote on last edited by
        #3

        Thank you very much. That worked

        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