Results 1 to 6 of 6

Thread: [RESOLVED] [2003] Adding Column Names To List Box

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2008
    Posts
    13

    Resolved [RESOLVED] [2003] Adding Column Names To List Box

    Hello,

    I want to add column names and proper columns to my list box and I couldn't find a way to do this. I have a books.dat file where all the records are stored and this is the code I have under the Display button which is meant to display the books file according to MyFormat coordinate settings.

    Code:
     Dim Index As Integer
            lstDisplayFile.Items.Clear()
            Filename = "Books.dat"
            FileOpen(1, Filename, OpenMode.Random, , , Len(OneBook))
            BooksInFile = LOF(1) / Len(OneBook)
            FileClose(1)
    
            Filename = "Books.dat"
            FileOpen(1, Filename, OpenMode.Random, , , Len(OneBook))
            For Index = 1 To BooksInFile 'looks through all the records in the file
                FileGet(1, OneBook)
                lstDisplayFile.Items.Add(String.Format(MyFormat, OneBook.Title, OneBook.AuthorName, OneBook.AuthorSurname, OneBook.deweyid, OneBook.ClassOfBook, OneBook.ISBN, OneBook.DateAdded, OneBook.Price))
            Next Index
            FileClose(1)
    Here is MyFormat declaration:
    Code:
    Dim MyFormat As String = "{0, -5}{1, -21}{2, -6}{3, -10}{4, -5}{5, -10}{6, -5}{7, -10}"
    I'm doing this for a school project so I would appreciate a quick reply and not too complicated solution . What I currently use are labels above the columns where the fields are supposed to be but often they don't display in the right places, for example:


    They're all spread out; is there any way to also rectify this?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2003] Adding Column Names To List Box

    You're using the wrong control. The ListBox is not intended for tabular data. It's intended for a list of singular items, like a shopping list. If you want to display a table then use a ListView with its View property set to Details or else use a DataGridView. Those are the controls that have been made specifically to do what you want to do. Both provide the facility for column header text within the control itself and the data is automatically displayed in columns with the need for you to pad and combine values to fake columns.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2003] Adding Column Names To List Box

    Must it be a ListBox? The DataGrid is specifically designed to display data in a grid pattern as you are looking for.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2003] Adding Column Names To List Box

    Ater nmadd's post I noticed that you're using VB.NET 2003. Forget my suggestion of the DataGridView. It's not available to you. The ListView is still an option or the DataGrid, as nmadd suggested.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2008
    Posts
    13

    Re: [2003] Adding Column Names To List Box

    I chose the listbox because I don't have the code that I would need to display this .dat file in a data grid or other way.

    I'm just learning this for my school project and to be honest I don't have much time. So I guess I will just have to play around with the coordinates in MyFormat.

    Thanks anyway!

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2003] Adding Column Names To List Box

    It will be quicker to learn how to use a ListView. It's very simple. In the designer you set its View property to Details and then add the columns. In code you add items and subitems to populate the cells. Each item is a row and each subitem is a cell in that row, e.g. this will fill four columns with three rows:
    vb.net Code:
    1. Dim item As ListViewItem
    2.  
    3. For rowIndex As Integer = 0 To 2
    4.     item = myListView.Items.Add("R" & rowIndex & "C0")
    5.  
    6.     For columnIndex As Integer = 1 To 3
    7.         item.SubItems.Add("R" & rowIndex & "C" & columnIndex)
    8.     Next columnIndex
    9. Next rowIndex
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width