Problems with a text box & changing alignment in the leave event.
-
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
-
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
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.
-
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.
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...)