show string in 2 columns

chadmanvb
chadmanvb used Ask the Experts™
on
I use a form as a popup box, but the text I display looks messing.  How can I seperate the 2 values so it looks like its in 2 columns before I send the string to may form?  Here is what the text looks like:
DISKDEFRAG 0100_COR_00
DOTNETFRAMEWKRE 0200_COR_00
DOTNETFRAMEWORK 0011_COR_00
DOTNETFW30 0300_COR_00
EFEMERALPORTCFG 0100_COR_00
ESDMENUPICK 0001_COR_02
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Column1.Text = Left$(StringToSplit.Text, InStr(1, StringToSplit.Text, " ") - 1)
Column2.Text = Mid$(StringToSplit.Text, InStr(1, StringToSplit.Text, " ") + 1)
HainKurtSr. System Analyst

Commented:
say you have line  and use this

dim a = line.split(" ")
dim nl = a(0).padRight(20) & a(1)

send nl
 
Most Valuable Expert 2012
Top Expert 2014

Commented:
Still around?
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Author

Commented:
Yep, I will check it out mon/tues at work

Author

Commented:

say you have line  and use this

dim a = line.split(" ")
dim nl = a(0).padRight(20) & a(1)

send nl
This splits the string, but does not make the columns even



Column1.Text = Left$(StringToSplit.Text, InStr(1, StringToSplit.Text, " ") - 1)
Column2.Text = Mid$(StringToSplit.Text, InStr(1, StringToSplit.Text, " ") + 1)

I could not get this to work.  I tried to use:

  arrServiceList.Sort(arrServiceList)

        Dim strService As String = ""
        Dim strServiceTotal As String = ""

        For Each line As String In arrServiceList   'loop array for the string

            strService = line.ToString
            Dim strCol1 As String = ""
            Dim strCol2 As String = ""


            strCol1 = Left$(strService, InStr(1, strService, " ") - 1)
            strCol2 = Mid$(strService, InStr(1, strService, " ") + 1)
            strService = line.Trim.ToString







            strServiceTotal = strServiceTotal & strService & vbCrLf



Author

Commented:
 strCol1 = Left$(strService, InStr(1, strService, " ") - 1)

Opps hit send to fast, but this is the line that errors.  It does not like the left.
Most Valuable Expert 2012
Top Expert 2014

Commented:
Are you printing these lines? Or showing in some control?

Author

Commented:
I will pass the strServiceTotal  string to a new form to print as a popup box
Most Valuable Expert 2012
Top Expert 2014

Commented:
>print as a popup box
You mean show as a popup form? What controls are you using on this form?

Author

Commented:
Yep, here is what I'm doing
  Dim arrServiceList() As String = (GetPage(WebString))

        arrServiceList.Sort(arrServiceList)

        Dim strService As String = ""
        Dim strServiceTotal As String = ""

        For Each line As String In arrServiceList   'loop array for the string

            strService = line.ToString
            Dim strCol1 As String = ""
            Dim strCol2 As String = ""


            strCol1 = Left(strService, InStr(1, strService, " ") - 1)
            strCol2 = Mid(strService, InStr(1, strService, " ") + 1)
            strService = line.Trim.ToString





            strServiceTotal = strServiceTotal & strService & vbCrLf


        Next line


        'display list on popup form
        Dim frmPopupSeviceList As New frmPopSearch(strServiceTotal)
        If frmPopupSeviceList.ShowDialog() = Windows.Forms.DialogResult.Cancel Then

            ' the cancel button was clicked
            Exit Sub

        End If


I guess I could use a listbox.  But I was hopping there would be an easy way to the textbox look like 2 columns.  so
DISKDEFRAG 0100_COR_00
DOTNETFRAMEWKRE 0200_COR_00
DOTNETFRAMEWORK 0011_COR_00
DOTNETFW30 0300_COR_00
EFEMERALPORTCFG 0100_COR_00
ESDMENUPICK 0001_COR_02

would look like
DISKDEFRAG                   0100_COR_00
DOTNETFRAMEWKRE             0200_COR_00
DOTNETFRAMEWORK             0011_COR_00
DOTNETFW30                   0300_COR_00
EFEMERALPORTCFG                             0100_COR_00
ESDMENUPICK                   0001_COR_02

Author

Commented:
opps the spacing did not turn on right on this form, but I guess  you get the idea.  I just want both columns lined up so it looks nice and is easier to read.
Most Valuable Expert 2012
Top Expert 2014

Commented:
You have multiple options. If the number of items is known and limited then you could simply use the labels. Otherwise you could either use a multicolumn ListView control or a DataGridView. You can populate a DataTable on main form and then pass it to popup form which would simply set the DataTable as the DataSource of the DataGridView. Let me know which way you want to go and i can provide relevant code examples.

Author

Commented:
I have never used a DataGridView, but have used ListView.  Could you show me how pass this from my main form to the popup form?
Most Valuable Expert 2012
Top Expert 2014
Commented:
There are multiple options for PASSING this to the popup form.
1) Use a string; separate rows by : (for example) and separate columns by ;.
2) Use a datatable.

This example uses the option 2.


Dim dTable As New DataTable
dTable.Columns.Add("Column1", GetType(String))
dTable.Columns.Add("Column2", GetType(String))

Dim drow as DataRow = dTable.NewRow
drow(0) = value1
drow(1) = value2

dTable.Rows.Add(drow)

Popupform.dTable = dTable 'In popup form class, declare a public variable called dTable of type DataTable at class level

Popup Form Load:

For i as Integer = 0 to dTable.Rows.Count - 1
    Dim lItem As New ListViewItem(dTable.Rows(i).Item(0)) 'Set the listview to details mode and add the columns as you want
    lItem.SubItems.Add(dTable.Rows(i).Item(1))
    LV.Items.Add(lItem)
Next

Open in new window

Most Valuable Expert 2012
Top Expert 2014

Commented:
If you were to use a DataGridView then

Popupform:

DataGridView1.DataSource = dTable 'just a single line of code!

Author

Commented:
Ok, I think we are close.  I still see a couple problems that I guess I am doing wrong.

   arrServiceList.Sort(arrServiceList)

        Dim strService As String = ""
        Dim strServiceTotal As String = ""
        Dim dTable As New DataTable


        dTable.Columns.Add("Column1", GetType(String))
        dTable.Columns.Add("Column2", GetType(String))
        Dim drow As DataRow = dTable.NewRow


        For Each line As String In arrServiceList   'loop array for the string
            line.Split(","c)
         

            'strService = line.Trim.ToString.Split(","c)



            drow(0) = line(0)
            drow(1) = line(1)

            dTable.Rows.Add(drow)


            'strServiceTotal = strServiceTotal & strService & vbCrLf


        Next line






       frmPopSearch.dTable = dTable 'this is not reconized.  does not like the dTable after it.  frmPopSearch is the name of my new form

        'Dim frmPopupSeviceList As New frmPopSearch(strServiceTotal)

And for on my popup form I have the following error:
Public Class frmPopSearch

    Public Sub New(ByVal DisplayText As String)
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

        'TextBox1.Text = DisplayText
        'TextBox1.Select(0, 0)   'this will get set focus on textbox backgroud so the text will not be highlighted


'*******************code does not like the dTable, I guess it does not see it from my original form?
        For i As Integer = 0 To dTable.Rows.Count - 1
            Dim lItem As New ListViewItem(dTable.Rows(i).Item(0)) 'Set the listview to details mode and add the columns as you want
            lItem.SubItems.Add(dTable.Rows(i).Item(1))
            ListView1.Items.Add(lItem)
        Next
     

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub

   
    Private Sub frmPopSearch_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'********************I have the same error below if I put the code here
        'For i As Integer = 0 To dTable.Rows.Count - 1
        '    Dim lItem As New ListViewItem(dTable.Rows(i).Item(0)) 'Set the listview to details mode and add the columns as you want
        '    lItem.SubItems.Add(dTable.Rows(i).Item(1))
        '    ListView1.Items.Add(lItem)
        'Next
    End Sub
End Class
Most Valuable Expert 2012
Top Expert 2014

Commented:
Change this
Public Class frmPopSearch

to this
Public Class frmPopSearch
    Public dTable as DataTable

Notice that i created this variable in the popup form. You need this at the class level and it needs to be public/friend so that you can set it from the main form.

Author

Commented:
still have 2 errors.

first one is how I am calling the pop form.  I am using
frmPopSearch.dTable = dTable 'In popup form class, declare a public variable called dTable of type DataTable at class level


when I just passed a string before I used the following and it worked.
Dim frmPopupSeviceList As New frmPopSearch(strServiceTotal)


The second error I see it the line on the new popup form:
Dim lItem As New ListViewItem(dTable.Rows(i).Item(0))

This gives me the following error:
Error      7      Overload resolution failed because no accessible 'New' can be called without a narrowing conversion:
    'Public Sub New(group As System.Windows.Forms.ListViewGroup)': Argument matching parameter 'group' narrows from 'Object' to 'System.Windows.Forms.ListViewGroup'.
    'Public Sub New(items() As String)': Argument matching parameter 'items' narrows from 'Object' to '1-dimensional array of String'.
    'Public Sub New(text As String)': Argument matching parameter 'text' narrows from 'Object' to 'String'.      C:\Temp\pkg_add\frmPopSearch.vb



I do have a listview created with 2 columns labled in detail.

Any ideas?
Most Valuable Expert 2012
Top Expert 2014

Commented:
1) Remove the constructor from the class and change the code to look like this

Dim frm As New FrmPopSearch
frm.DTable = dTable
frm.ShowDialog

2) Change
Dim lItem As New ListViewItem(dTable.Rows(i).Item(0))
to
Dim lItem As New ListViewItem(CType(dTable.Rows(i).Item(0), String))

Author

Commented:
On step one I still get an error calling the form:
Error      5      Argument not specified for parameter 'DisplayText' of 'Public Sub New(DisplayText As String)'.

am I missing somthing still?      

Looks like the error on step 2 worked.
Most Valuable Expert 2012
Top Expert 2014

Commented:
As i said, remove the constructor from the popup form class

Remove this method
Public Sub New(DisplayText As String)
Most Valuable Expert 2012
Top Expert 2014

Commented:
Or Replace it with
Public Sub New(dTable as DataTable)

Author

Commented:
I made the change, but that just gave me another error.  Here is what I have my main form
  For Each line As String In arrServiceList   'loop array for the string
            line.Split(","c)


            'strService = line.Trim.ToString.Split(","c)



            drow(0) = line(0)
            drow(1) = line(1)

            dTable.Rows.Add(drow)


            'strServiceTotal = strServiceTotal & strService & vbCrLf


        Next line

        'display list on popup form
        Dim frm As New frmPopSearch          '*********************i get an error here Argument not specified for parameter 'dTable' of 'Public Sub New(dTable As System.Data.DataTable)

        frm.dTable = dTable
        frm.ShowDialog()

        If frm.ShowDialog() = Windows.Forms.DialogResult.Cancel Then

            ' the cancel button was clicked
            Exit Sub

        End If


On my pop form I have:
Public Class frmPopSearch
    Public dTable As DataTable

    Public Sub New(ByVal dTable As DataTable)
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call


        For i As Integer = 0 To dtable.Rows.Count - 1
            Dim lItem As New ListViewItem(CType(dtable.Rows(i).Item(0), String))
            lItem.SubItems.Add(dtable.Rows(i).Item(1))
            ListView1.Items.Add(lItem)
        Next



    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub

End Class

Also thanks for all the help!  I know i have to be close:-)
Most Valuable Expert 2012
Top Expert 2014

Commented:
Ok. I thought you would know that if you changed the constructor to accept a datatable then you would use it like this

Dim frm As New frmPopSearch(dTable)

But i think you should remove the constructor to avoid confusion.

Author

Commented:
I got it working!  Thanks again for all of your patience and time.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial