Jump to content

text aligning


Recommended Posts

Hi,

This needs to have all the "OK" notes aligned
With my tries I got close to fixing this but I don't want to waste on it  any more time ..

Maybe some one has a good idea or a ready snippet on how the padding should get calculated 
here is a raw example :

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Local $hGUI = GUICreate("Example", 785, 400)
Local $idOK = GUICtrlCreateButton("OK", 310, 370, 85, 25)
Global $sdata, $idInput = GUICtrlCreateEdit("", 10, 10, 760, 310) ;, BitOR($ES_AUTOVSCROLL, $ES_WANTRETURN, $WS_VSCROLL))
GUISetState(@SW_SHOW, $hGUI)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $idOK
            _doit()
    EndSwitch
WEnd

Func _sGen()
    $pwd = ""
    Dim $aSpace[3]
    $digits = Random(1, 80)
    For $i = 1 To $digits
        $aSpace[0] = Chr(Random(65, 90, 1)) ;A-Z
        $aSpace[1] = Chr(Random(97, 122, 1)) ;a-z
        $aSpace[2] = Chr(Random(48, 57, 1)) ;0-9
        $pwd &= $aSpace[Random(0, 2, 1)]
    Next
    Return $pwd
EndFunc   

Func _doit()
    For $i = 1 To 10
        $sMsg = _sGen()
        $sMsg &= _padd(StringLen($sMsg)) & "OK" & @CRLF
        GUICtrlSetData($idInput, GUICtrlRead($idInput) & $sMsg)
    Next
EndFunc 

Func _padd($count)
    Local $padd
    For $k = 0 To 80 - $count
        $padd &= " "
    Next
    Return $padd & @TAB
EndFunc

Thanks

Deye

Edit: for comparing both outputs ( aka: + ConsoleWrite)

Func _doit()
    For $i = 1 To 10
        $sMsg = _padd(_sGen()) & "OK"
        ConsoleWrite($sMsg & @CRLF)
        GUICtrlSetData($idInput, GUICtrlRead($idInput) & $sMsg & @CRLF)
    Next
EndFunc   ;==>_doit

Func _padd($strString)
    Local $padd, $len = StringLen($strString)
    Do
        $padd &= " "
        $len += 1
    Until $len = 80
    Return $strString & $padd ;& @TAB
EndFunc   ;==>_padd

 

Edited by Deye
Link to comment
Share on other sites

I don't know if it could be more compact with StringRegExpReplace(), but try these two "workarounds" :)

First workaround:

Spoiler
Global $arrStrings[4] = ["I am a string", _
                         "I am a longer string than the previous one", _
                         "I am a shorter string than this one above", _
                         "I am the shortest string here"], _
        $intStringLenght = 0, _
        $intStringMaxLenght = 0, _
        $strSpaces = ""

Func _GetStringMaxLenght()
    For $i = 0 To UBound($arrStrings) - 1 Step 1

        $intStringLenght = StringLen($arrStrings[$i])
        If $intStringLenght > $intStringMaxLenght Then $intStringMaxLenght = $intStringLenght

        ; ConsoleWrite("Lenght of '" & $arrStrings[$i] & "' is " & StringLen($arrStrings[$i]) & " characters." & @CRLF)
    Next

    ; ConsoleWrite("Max lenght of a string is " & $intStringMaxLenght & " characters." & @CRLF)
EndFunc

Func _AddSpaces()
    For $i = 0 To UBound($arrStrings) - 1 Step 1
        $intStringLenght = StringLen($arrStrings[$i])
        If $intStringLenght < $intStringMaxLenght Then
            Do
                $arrStrings[$i] &= " "
                $intStringLenght += 1
            Until $intStringLenght = $intStringMaxLenght
        EndIf

        $arrStrings[$i] &= " -----> OK"
        ConsoleWrite($arrStrings[$i] & @CRLF)
    Next
EndFunc

_GetStringMaxLenght()
_AddSpaces()

 

Second workaround:

Spoiler
Global $arrStrings[4] = ["I am a string", _
                         "I am a longer string than the previous one", _
                         "I am a shorter string than this one above", _
                         "I am the shortest string here"], _
        $intStringLenght = 0, _
        $intStringMaxLenght = 0, _
        $strSpaces = ""

For $i = 0 To UBound($arrStrings) - 1 Step 1
    _GetStringMaxLenght($arrStrings[$i])
Next

For $i = 0 To UBound($arrStrings) - 1 Step 1
    $arrStrings[$i] = _AddSpaces($arrStrings[$i]) & " -----> OK"
    ConsoleWrite($arrStrings[$i] & @CRLF)
Next

Func _GetStringMaxLenght($strString)
        $intStringLenght = StringLen($strString)
        If $intStringLenght > $intStringMaxLenght Then $intStringMaxLenght = $intStringLenght
        ; ConsoleWrite("Lenght of '" & $strString & "' is " & $intStringLenght & " characters." & @CRLF)
EndFunc

Func _AddSpaces($strString)
    $intStringLenght = StringLen($strString)
    If $intStringLenght < $intStringMaxLenght Then
        Do
            $strString &= " "
            $intStringLenght += 1
        Until $intStringLenght = $intStringMaxLenght
    EndIf

    Return $strString

EndFunc

 

Hope that helps :)

Edited by FrancescoDiMuro
Added another workaround

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

thanks FrancescoDiMuro

see my edit above and compare the outputs

using tabs alone can fix this .. but this needs some kind of a calculation to set required spaces & Tabs

as i said i could solve it but not in a clear way\mind so maybe some one has a better way ..

Deye

Link to comment
Share on other sites

You can use a fixed width font.

Local Const $sFont = "Courier New"
Local $hGUI = GUICreate("Example", 785, 400)
Local $idOK = GUICtrlCreateButton("OK", 310, 370, 85, 25)
Global $sdata, $idInput = GUICtrlCreateEdit("", 10, 10, 760, 310) ;, BitOR($ES_AUTOVSCROLL, $ES_WANTRETURN, $WS_VSCROLL))
GUICtrlSetFont(-1, 9, $FW_DONTCARE , "", $sFont)

 

Link to comment
Share on other sites

JoHanatCent's suggestion looks great. Just add these 2 lines in your 1st script, between the GUISetState and the While 1 :

Local $sFont = "Courier New"
GUICtrlSetFont($idInput, 9, 0, 0, $sFont)

Other fixed width fonts I just tried : Consolas, Courier, Fixedsys, Lucida Console

Or you could place all your "Ok's" at the very left of each line, problem solved (just kidding) :)

 

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