Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Lining up tabs...

Posted on 2000-03-19
4
183 Views
Last Modified: 2010-05-18
ok...
if I have a string like this (without the ---'s)

---
TCP      mypc1:nbsession      pc2:1028      ESTABLISHED
TCP      mypc1:3968      208.184.29.170.doubleclick.net:80      TIME_WAIT
TCP      mypc1:3969      208.184.29.170.doubleclick.net:80      TIME_WAIT
TCP      mypc1:3459      efnet.demon.co.uk:6666      ESTABLISHED
TCP      mypc1:3975      208.184.29.170.doubleclick.net:80      TIME_WAIT
TCP      mypc1:3976      208.184.29.170.doubleclick.net:80      TIME_WAIT
TCP      mypc1:3979      208.184.29.170.doubleclick.net:80      TIME_WAIT
TCP      mypc1:3980      208.184.29.170.doubleclick.net:80      TIME_WAIT
---

This is obviously the output of netstat.

Anyway... my problem is that due to the fact that some strings are longer than others, the next column of text doesn't always line up properly.

I would like a function that I can pass a string such as this to, and it will line it all up properly.

For example after formatting there should be:
three extra tabs after pc:1028
one extra tab after efnet.demon.co.uk:6666

also please note this code should work for a string with any amount of columns
0
Comment
Question by:plasmatek
4 Comments
 

Expert Comment

by:raquel061097
ID: 2634095
You could write an algorithm to line up all the lines if all you are doing is getting the columns matched up with each other. However I would suggest using the spaces as delimiting markers or changing them to tabs.

VB6
newstring = replace(input, " ", vbTab)

Then you could output the information to a multicolumn list box.

Just a suggestion.
0
 
LVL 14

Accepted Solution

by:
wsh2 earned 75 total points
ID: 2634890
If you are using VB6.. AND.. fixed width fonts.. you could also use spaces.

<----- Code Begin ----->

Dim strInput As String
strInput = "TCP mypc1:3968 208.184.29.170.doubleclick.net:80 TIME_WAIT"

Dim varWidth As Variant
varWidth = Array(6, 16, 36, 16) ' Specify your column widths here

Dim strWork() As String
strWork() = Split(strInput, " ")

Dim lngIndex As Long
For lngIndex = LBound(strWork) To UBound(strWork)
    If Len(strWork(lngIndex)) + 1 < varWidth(lngIndex) _
    Then
        strWork(lngIndex) = strWork(lngIndex) _
            & Space(varWidth(lngIndex) _
            - Len(strWork(lngIndex)) _
            - 1)
    End If
Next lngIndex

strInput = Join(strWork, " ")

MsgBox (strInput)

<----- Code End ----->

0
 
LVL 28

Expert Comment

by:Ark
ID: 2635015
Hi
Which control do you use for output your strings? If TextBox:
'Works in VB 4/32,VB5,VB6

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const EM_SETTABSTOPS = &HCB
Dim sFrom(7) As String
Private Sub Command1_Click()
  Text1 = ""
  For i = 0 To 7
    Text1 = Text1 & TabStrings(sFrom(i)) & vbCrLf
  Next i
End Sub

Private Sub Form_Load()
  Dim tabvals(2) As Long
'You can set TabStops as you want. I set them for your strings
  tabvals(0) = 16 '4 position - 16 in dialog units
  tabvals(1) = 72 '18 position
  tabvals(2) = 184 '46 position
  di& = SendMessage(Text1.hwnd, EM_SETTABSTOPS, 3, tabvals(0))
  sFrom(0) = "TCP mypc1:nbsession pc2:1028 ESTABLISHED"
  sFrom(1) = "TCP mypc1:3968 208.184.29.170.doubleclick.net:80 TIME_WAIT"
  sFrom(2) = "TCP mypc1:3969 208.184.29.170.doubleclick.net:80 TIME_WAIT"
  sFrom(3) = "TCP mypc1:3459 efnet.demon.co.uk:6666 ESTABLISHED"
  sFrom(4) = "TCP mypc1:3975 208.184.29.170.doubleclick.net:80 TIME_WAIT"
  sFrom(5) = "TCP mypc1:3976 208.184.29.170.doubleclick.net:80 TIME_WAIT"
  sFrom(6) = "TCP mypc1:3979 208.184.29.170.doubleclick.net:80 TIME_WAIT"
  sFrom(7) = "TCP mypc1:3980 208.184.29.170.doubleclick.net:80 TIME_WAIT"

End Sub

Private Function TabStrings(sIn As String) As String
  For i = 1 To Len(sIn)
      If Mid$(sIn, i, 1) = " " Then Mid$(sIn, i, 1) = vbTab
  Next i
  TabStrings = sIn
End Function


You need Form, Text1 textbox with multiline property set to True and Comman1 command button for this sample.

Cheers
0
 
LVL 1

Author Comment

by:plasmatek
ID: 2637196
thanks... it works.
Would've been nicer to use tabs, and would have been nice in a function but I can do that myself.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA open file from excel cell 4 44
vb6 connector to mongodb 2 117
Convert VB6 MSXML2.ServerXMLHTTP process to C# 2 58
Recommendation vb6 to vb.net or others 14 174
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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

808 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