TextBox Alignment
-
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
-
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
The problem is with the order of events. When using the keyboard, the events are fired in the following order:
- Enter
- GotFocus
- Leave
- Validating
- Validated
- LosFocus
When you use the mouse or call the
Focus
method, the events are fired in the following order:- Enter
- GotFocus
- LostFocus
- Leave
- Validating
- Validated
So, if you handle the
Enter
andLostFocus
events, what you want works when using the keyboard. If you use the likely pairs together (likeEnter
andLeave
), when you change theTextAlign
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 theTextBox
. 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-----
-
The problem is with the order of events. When using the keyboard, the events are fired in the following order:
- Enter
- GotFocus
- Leave
- Validating
- Validated
- LosFocus
When you use the mouse or call the
Focus
method, the events are fired in the following order:- Enter
- GotFocus
- LostFocus
- Leave
- Validating
- Validated
So, if you handle the
Enter
andLostFocus
events, what you want works when using the keyboard. If you use the likely pairs together (likeEnter
andLeave
), when you change theTextAlign
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 theTextBox
. 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-----
Thank you very much. That worked