It seems difficult to right justify text in a text box.
Does anybody have some clues? I think I could write the code with a few hints.
Is there some way to get the length of a string in twips or some unit other than characters?
Printable View
It seems difficult to right justify text in a text box.
Does anybody have some clues? I think I could write the code with a few hints.
Is there some way to get the length of a string in twips or some unit other than characters?
Set the textbox property 'Alignment' to "Right Justify".
But he said listbox in the title.
The Listbox does not have a right justify the texbox does .which one do you want?if its the textbox then look at the answer give by GuileQuote:
Right justify text in list box?
I paid more attention to the post, then the title. <Shrug>
your thread subject asks about a list box and the body of your quesiton asks about a text box. you need to decide which one you want to know about because they are not the same. If you really want to know about a text box, then as Guile.NET has stated, it is trivial.
if you want to know about a list box, then (1) there is probably some way to do it through subclassing and/or API calls, but I don't know how, or (2) you can fake it by using a fixed width font such as courier and then putting a variable number of spaces onto the front of each item to make them all come out the same length
Guv's abilities are well enough known to lead me to think it's a typo, cuz I'm quite sure he knows how to justify in a text box.
Without even knowing Guv, or his abilities, I'm thinking the same thing.
In fact, I didn't even realise there _was_ a mistake, I just assumed he is new to VB, and wasn't aware of the property value.
That being said... no, I don't know how to Right Justify text in a List Box. =)
Sorry about the typo. It is a list box that I want to right justify.
create a new project and add a listbox to it..
put this code in and run the project. You should be
able to work with this.
VB Code:
Option Explicit Private Sub Form_Load() List1.AddItem "TESTING" List1.AddItem "ANOTHER ITEM" List1.AddItem "YET ANOTHER ITEM" RightAlignListBox List1 End Sub Public Sub RightAlignListBox(lstBox As ListBox) Dim lTextSizePix As Long Dim lSpaceSizePix As Long Dim lSizeNeeded As Long Dim i As Integer 'MAY NEED TO CHANGE THIS VALUE TO ACCOMODATE SMALL GAPS ON THE RIGHT SIDE, 'EITHER TOO MUCH SPACE OR NOT ENOUGH SPACE. 50 WORKED WELL FOR ME Const Variable_Difference = 50 lSpaceSizePix = Printer.TextWidth(" ") For i = 0 To lstBox.ListCount - 1 lTextSizePix = Printer.TextWidth(lstBox.List(i)) lSizeNeeded = ((lstBox.Width * Screen.TwipsPerPixelX) - lTextSizePix) - Variable_Difference lSizeNeeded = lSizeNeeded / lSpaceSizePix lstBox.List(i) = Space(lSizeNeeded) & lstBox.List(i) Next End Sub Private Sub List1_DblClick() Dim strItem As String strItem = Trim(List1.List(List1.ListIndex)) MsgBox "Text: " & strItem & vbCrLf & "Length: " & Len(strItem) End Sub
Kleinma: Your code looks reasonable. I will try it and experiment a bit.
Printer.TextWidth Function looks like the key to this task.
Thanx !!
If all else fails, I will try simulating a List Box using a Text Box. which might be the easiest way to go.
I want to avoid changing to a change to a Text Box because there is some tricky logic associated with my current List Box. I am using it to simulate a LIFO Stack. The last Item is at the bottom at a fixed height on the Form, with the Top moving as items are added.
make sure you set the forms scalemode to pixel as well..Quote:
Originally posted by Guv
Kleinma: Your code looks reasonable. I will try it and experiment a bit.
Printer.TextWidth Function looks like the key to this task.
Thanx !!
If all else fails, I will try simulating a List Box using a Text Box. which might be the easiest way to go.
I want to avoid changing to a change to a Text Box because there is some tricky logic associated with my current List Box. I am using it to simulate a LIFO Stack. The last Item is at the bottom at a fixed height on the Form, with the Top moving as items are added.