A little function problem

genu

Well-known member
Joined
Jun 9, 2005
Messages
94
Programming Experience
1-3
[RESOLVED]A little function problem

I made this function to populate a listview with data, but based on the columns its supposed decide wich part of te array to insert, but for some reason it doesnt do anything...it doesnt load anything into the listview:

VB.NET:
 Function populate_data()
        read = file.OpenText(System.AppDomain.CurrentDomain.BaseDirectory & "\data\soldiers.dat")
        While read.Peek <> -1
            On Error Resume Next
            Dim new_item As ListViewItem
            line_in = read.ReadLine
            in_array = Split(line_in, "|")
            Select Case main_list.Columns.Item(0).Text
                Case "First name"
                    new_item = main_list.Items.Add(in_array(0))
            End Select
            For i = 1 To main_list.Columns.Count
                Select Case main_list.Columns.Item(i).Text
                    Case "First Name"
                        new_item.SubItems.Add(in_array(0))
                    Case "Last Name"
                        new_item.SubItems.Add(in_array(1))
                End Select
            Next
        End While
    End Function
 
Last edited:
Sorry for the name it was another project i just removed another code and adpated to you ... take a look ... Cheers ;)


P.S. maybe it is not right like you want but i think you'll find some sense there ... otherwise feel free to ask for more ... i'd be glad to help you out
 

Attachments

  • Calculator.zip
    22.9 KB · Views: 21
thanks for the example, but here is the situtation in detail:

I have a listview:
*in that listvie there can be

FIRST NAME | LAST NAME | AGE

Now when the form loads that listview can have any of the above like so:
LAST NAME | FIRST NAME | AGE
OR
FIRST NAME | AGE
ETC

when the form loads, I now for sure that array(0) = first name, array(1)=lastname and array(2) = age

PROBLEM:
How do make I make the function see what columns there are and then fill them with the appropiate data
 
I can just use array.count to get the number cant I?..and the thing that Im not sure how to do is put specific text to specfic column..
 
hey you know what.........I just thought about it....to make things easier..is it a way to just load all the columns from the text file(which is what its doing) then fill them with the all of the data, and then make the columns that I dont want invisible. Is that possible somehow?


I will basically get the same results...
 
that thing is that the program has options to configure which columns are visible in the listview, so I thought about loading the data based on which columns already exsists. Since appearntly that doenst work I just thought I can just load every available column, load the data, and then just remove the columns that the user didnt want from the options menu.

I hope that made sense

sorry for the confusion
 
Ok, I think it could be resolved (i have no time for that right now ... sorry).
Another thing, maybe it's because i don't know the purpose of your app but personally think that this approach is absolutelly wrong and that also you should use either XML or relational DB (i.e.MSAccess) instead unformated db like the text file is.

Cheers ;)
 
the reason im using text file is because...I really never used db and im dont know how...plus I dont think the program requires something as powerfull as a access db
 
OK I HAVE FIGURED IT OUT with the help of your examples here is the new working function. It might not make much sense, but enlisted_columns array is the same length as in_array


VB.NET:
 Function check_file()
        main_list.Items.Clear()
        main_list.Columns.Clear()
        Dim read_array
        Dim line_in
        read = file.OpenText(System.AppDomain.CurrentDomain.BaseDirectory & "\data\config.dat")
        While read.Peek <> -1
            line_in = read.ReadLine
            in_array = Split(line_in, "|")
            If in_array(0) = "[Enlisted Columns]" Then
                For i = 1 To in_array.Length - 1
                    Dim new_column As New ColumnHeader
                    new_column.Text = in_array(i)
                    new_column.Width = 100
                    main_list.Columns.Add(new_column)
                Next
            End If
        End While
        read.Close()
        populate_data()

    End Function
    Function populate_data()
        Dim enlisted As Array
        read = file.OpenText(System.AppDomain.CurrentDomain.BaseDirectory & "\data\soldiers.dat")
        While read.Peek <> -1
            On Error Resume Next
            Dim new_item As ListViewItem
            line_in = read.ReadLine
            in_array = Split(line_in, "|")
            enlisted = Split(enlisted_columns, "|")
            For i = 0 To enlisted_columns.length - 1
                If main_list.Columns.Item(i).Text = enlisted(enlisted.IndexOf(enlisted, main_list.Columns.Item(i).Text)) Then
                    new_item = main_list.Items.Add(in_array(enlisted.IndexOf(enlisted, main_list.Columns.Item(i).Text)))
                    GoTo add_rest
                End If
            Next
add_rest:
            enlisted = enlisted_columns
            For i = 1 To main_list.Columns.Count
                If main_list.Columns.Item(i).Text = enlisted(enlisted.IndexOf(enlisted, main_list.Columns.Item(i).Text)) Then
                    new_item.SubItems.Add(in_array(enlisted.IndexOf(enlisted, main_list.Columns.Item(i).Text)))
                End If
            Next
        End While
        read.Close()
    End Function
 
Back
Top