Jump to content

Center text in ComboBox


Belini
 Share

Recommended Posts

I wanted to show the centralized events in the combobox, is there a way to centralize the text?

Link to comment
Share on other sites

I also searched a lot before posting but I did not find the solution here the forum!

 

Link to comment
Share on other sites

Here is a method to center text in a combo control.

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
; Ref: https://www.autoitscript.com/forum/topic/119009-combo-and-format-issue-aligning-text/?do=findComment&comment=827534

Local $data[20]
Local $iComboWidth = 249

Local $gui = GUICreate('', 260, 410)
Local $test = GUICtrlCreateCombo('', 0, 0, $iComboWidth, 400, $WS_VSCROLL)
GUICtrlSetFont(-1, 10, Default, Default, "Consolas") ; <<<<<<<<<<<<<<<< From above https reference thread.
GUISetState()

; ---- Generate data ---------
For $i = 0 To UBound($data) - 1
    For $j = 0 To Random(2, 10)
        $data[$i] &= Random(1, 10, 1)
    Next
Next
; ---- End of Generate data ---------

_CentreData($iComboWidth, $data)

; Set data to the control.
For $i = 0 To UBound($data) - 1
    GUICtrlSetData($test, $data[$i])
Next

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then
        MsgBox(0, "", "Selected: " & StringStripWS(GUICtrlRead($test), 3), 4) ; $STR_STRIPLEADING (1) = strip leading white space plus $STR_STRIPTRAILING (2) = strip trailing white space
        ExitLoop
    EndIf
WEnd


; Centre each item in the $aData array, relative to the $iComboWidth.
Func _CentreData($iComboWidth, ByRef $aData)
    Local $strintest = Int(($iComboWidth / 8.3) / 2) ; Find centre of combo window width 8.3 pixels per character

    ; Add spaces to each item to Centre text.
    For $i = 0 To UBound($aData) - 1
        While Int(StringLen($aData[$i]) / 2) < $strintest
            $aData[$i] = ' ' & $aData[$i] & ' '
        WEnd
    Next
EndFunc   ;==>_CentreData

 

Edited by Malkey
The If expression and the Until expression were replaced by the one While expression in the _CentreData() function..
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...