Hello guys and Good day!

I just want a little help on how to get the total number of space from the left until the word starts and display the total space in msgbox..

Example:

combo1.AddItem = "                        Bank       "

In this example the total number of space from the left is 30.

Thank you for your reply..Thank you.!

God bless!

Dani AI

Generated

Nice summary of the working fix from and — comparing the original length with a left‑trimmed length is the clean one-line approach in classic VB. For completeness, here are a couple of practical alternatives and tips that cover edge cases (tabs, non‑breaking spaces) and show how to pad an item to a fixed column without a manual loop.

A simple character scan (what hinted at) counts leading space characters directly:

Function CountLeadingSpaces(s As String) As Integer
    Dim i As Integer
    For i = 1 To Len(s)
        If Mid$(s, i, 1) <> " " Then
            CountLeadingSpaces = i - 1
            Exit Function
        End If
    Next i
    CountLeadingSpaces = Len(s)
End Function

VB.NET options (useful if the project targets .NET): a regex can count any leading whitespace (spaces, tabs), and built-in helpers create padding or pad to a fixed width.

' count leading whitespace (space, tab, etc.)
Dim m = System.Text.RegularExpressions.Regex.Match(s, "^\s*")
Dim leading = m.Length

' create N spaces and add an item
ComboBox1.Items.Add(New String(" "c, leading) & "Hello")

' pad to column 11
Dim padded = "Hello".PadLeft(11)

Notes and pitfalls: classic VB trim functions differ from .NET (TrimStart in .NET removes all whitespace types; LTrim in VB6 removes space characters). If input may contain tabs, replace them (for example, replace vbTab with a few spaces) before counting. Relying on leading spaces for visual alignment is fragile across fonts — prefer a monospaced font, an owner‑drawn combo, or a multi‑column control (ListView) when true column alignment is required.

Recommended Answers

All 7 Replies

Look up the mid function and put it in a loop.

thank you for the reply...but what is the function to detect the space when i used the mid function?

Look up the mid function and put it in a loop.

thank you for the reply...but what is the function to detect the space when i used the mid function?

commented: Don't post twice. Edit the original post. -3

(length of the string including space ) - (lenth of the string after LTRIM) = number of spaces in the start of the string.

Len("Visual Basic") - Len(ltrim("Visual Basic"))

Look up the mid function and put it in a loop.

thank you for the reply...but what is the function to detect the space when i used the mid function?

If you look up the function, I'm sure you can figure it out.

thank you guys..

this formula works without looping...

dim total space as integer

totalspace = Len("Visual Basic") - Len(ltrim("Visual Basic"))

msgbox totalspace

One last clarification...Can i additem on a combo box that will start at position 11 or the total space value without looping?

This means that when i add new item, it will automatically add 11 space or base upon the totalspace variable value from the left..

Please add the code above if its possible..

thank you once again to all of you guys..

God bless

final solution..

dim total space as integer
 
totalspace = Len("Visual Basic") - Len(ltrim("Visual Basic"))
 
msgbox totalspace

now to add item depending on totalspace var.here it is..

combo1.additem Space$(totalspace) & "Hello"

thank you guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.