Articles in this section
Category / Section

How to specify the text alignment of the ComboBoxBarItem in WinForms XPToolBar?

3 mins read

Specify the text alignment

The cursor position of the ComboBoxBarItem is the same on its lost focus, by default. Like the ComboBox, the cursor position of the ComboBoxBarItem can be aligned to the left by handling its events, TextBoxBound, MouseUp, and LostFocus.

TextBoxBound:

This event is raised when TextBox bounds in the ComboBarItem is set. By handling this event, you can access its inner TextBox.

MouseUp:

This event is raised once Mouse selection occurs in the ComboBarItem. By handling this event, you can update the cursor position upon selection.

LostFocus:

This event is raised when the ComboBarItem focus is lost. By handling this event, you can update its cursor position once the focus in lost.

C#

//Initializes the TextBoxBase.
private TextBoxBase textBox = null;
//Checks whether the ComboBox is focused.
private bool textBoxFocused;
private void comboBoxBarItem1_TextBoxBound(object sender, Syncfusion.Windows.Forms.Tools.XPMenus.TextBoxBoundEventArgs args)
{
    textBox = args.TextBox;
    textBox.MouseUp += new MouseEventHandler(textBox_MouseUp);
    //The event fires when the focus leaves the text box.
    textBox.LostFocus += new System.EventHandler(textBox_LostFocus);
}
//Selects all the Text in the TextBox of the ComboBoxBarItem.
void textBox_MouseUp(object sender, MouseEventArgs e)
{
    if ((!textBoxFocused) && this.textBox1.SelectionLength == 0)
    {
        textBoxFocused = true;
        this.textBox.SelectAll();
    }
}
private void textBox_LostFocus(object sender, EventArgs e)
{
    textBoxFocused = false;
    textBox.SelectionStart = textBox.Text.Length;
    textBox.SelectionStart = 0;
}

VB

'Initializes the TextBoxBase.
Private textBox As TextBoxBase = Nothing
'Checks whether the ComboBox is focused.
Private textBoxFocused As Boolean
Private Sub comboBoxBarItem1_TextBoxBound(ByVal sender As Object, ByVal args As Syncfusion.Windows.Forms.Tools.XPMenus.TextBoxBoundEventArgs)
    textBox = args.TextBox
    AddHandler textBox.MouseUp, AddressOf textBox_MouseUp
    'The event fires when the focus leaves the text box.
    AddHandler textBox.LostFocus, AddressOf textBox_LostFocus
End Sub
'Selects all the Text in the TextBox of the ComboBoxBarItem.
Private Sub textBox_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
    If ((Not textBoxFocused)) AndAlso Me.textBox1.SelectionLength = 0 Then
       textBoxFocused = True
       Me.textBox.SelectAll()
    End If
End Sub
Private Sub textBox_LostFocus(ByVal sender As Object, ByVal e As EventArgs)
    textBoxFocused = False
    textBox.SelectionStart = textBox.Text.Length
    textBox.SelectionStart = 0
End Sub

 

ComboBoxBarItem focused on its item selection

Figure 1: ComboBoxBarItem focused on its item selection

ComboBoxBarItem text is left aligned on its focus last

Figure 2: ComboBoxBarItem text is left aligned on its focus lost

Samples:

C#: https://www.syncfusion.com/downloads/support/directtrac/141232/ze/ComboBoxJustify-897526030

VB: https://www.syncfusion.com/downloads/support/directtrac/141232/ze/ComboBoxJustify_VB-574148021

Reference link: https://help.syncfusion.com/windowsforms/xptoolbar/baritem-types#comboboxbaritem

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied