Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

position of variables in a list

Posted on 2003-03-16
4
Medium Priority
?
161 Views
Last Modified: 2010-05-01
Hi,

I've a small problem.
I'm making a very simple cashregister program.  It this program, I use a list to add items.
Each item in that list have to contain 3 variables.
For example:

3   Cola                                  4,5
5   Fanta                                 6,5

'and so on

NOW HERE IS THE PROBLEM:
As in the example the '3' is stored in a variable1, the 'cola' in variable2 and the '4,5' in variable3.

Now I do the following:

List1.AddItem variable1 & vbTab & variable2 & vbTab & vbTab & vbTab & vbTab & vbTab & variable3

The problem is, when the characters or numbers stored in some of these variables ar longer I get this:


3   Cola                                  4,5
5   Fanta                                 6,5
3   The best beer                                9,5
5   Fristi                                8,0

As you can see in the 3th line, the 9,5 sticks out.  Does anyone have the solution to this problem.
(keep in mind, it's a very easy program so I dont want to use (or connect to) a database)
For example: Is it possible to declare each position of each variable, starting from the left edge of the list??)

Thanks in advance,

Vincent
0
Comment
Question by:vincentregent
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
Sweat earned 240 total points
ID: 8147300
vincentregent,

Easy "problem" to fix.

To properly align the three variables, you need to insure that all of the rows added to the listbox are of the same length.  So when you are dealing with the "3" or Variable1, then if you have more than ten rows and a value of two characters, then the "3" must be "3 ".

The same is true for the other two variables.  You'll need to pick a length, say 20 characters and insure that each variable2 is the same length.

Consider the following method:

    Variable1 = "Cola"
    Variable1 = Variable1 & Space$(20 - Len(Variable1))
    would become: "Cola                "

    or

    Variable1 = "The best beer"
    Variable1 = Variable1 & Space$(20 - Len(Variable1))
    would become: "The best beer       "

Then when you build the Additem line, each line will be of the same length and therefore lined up.

Let me know if this isn't clear.

Sweat


0
 

Author Comment

by:vincentregent
ID: 8147440
Thanks a lot Sweat !!!!!!



Greetings,
Vincent
0
 
LVL 3

Expert Comment

by:DocM
ID: 8147480

If you need to stay with the Tab as a separator, you could use a function like this:

Private Sub Command1_Click()
List1.Clear

a = 3: b = "The best beer": c = "9,5"
List1.AddItem CheckWidth(a, "1234") & CheckWidth(b, "1234567890") & c

a = 3: b = "Cola": c = "4.5"
List1.AddItem CheckWidth(a, "1234") & CheckWidth(b, "1234567890") & c

a = 5: b = "Fanta": c = "6, 5"
List1.AddItem CheckWidth(a, "1234") & CheckWidth(b, "1234567890") & c

 
End Sub
Function CheckWidth(strT, strMaxWidth) As String
 'We put at least one vbTab
 strMaxWidth = strMaxWidth & vbTab
 
 Me.Font = List1.Font
 Me.FontSize = List1.FontSize
 
 If TextWidth(strT & vbTab) > TextWidth(strMaxWidth) Then
  MsgBox "Text too long :" & strT
 
 Else
 
separator = ""
Do While TextWidth(strT & separator) < TextWidth(strMaxWidth)
 separator = separator & vbTab
Loop
 
 Do While TextWidth(strT & separator) < TextWidth(strMaxWidth)
 If separator = "" Then Exit Do
 separator = Mid(separator, 2)
Loop
 
End If

CheckWidth = strT & separator
 
End Function
0
 

Author Comment

by:vincentregent
ID: 8152448
Thanx for your help DocM.

Vincent
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question