Results 1 to 9 of 9

Thread: [RESOLVED] [2008] Help with ListView

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Resolved [RESOLVED] [2008] Help with ListView

    Hi, i'm trying to add some items into a specific ColumnHeader in ListBox1. If i have 3 columns, and i want to add the information written in Textbox1 to ColumnHeader2, how can i do it?

    I add using:

    Code:
    ListView1.Items.Add("blah blah blah")
    But that adds it into the first column... What can i do? Any ideas??
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

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

    Re: [2008] Help with ListView

    As I'm a stickler for terminology I'll point out that you're adding to ListView1, not ListBox1. Your code is right but your post is not. Also, you'll add the data to the column, not the column header. The column header is just the button-like area at the top, i.e. head, of the column that contains the column title.

    As for the question, each item you add to the ListView represents a row. The Text of the item is the text displayed in the first column. If you want to display text in any other row then you must add subitems to the item. Note that, if you want an empty "cell", you must still add a subitem but leave its text blank. To populate three columns you might do this:
    vb.net Code:
    1. Me.ListView1.Items.Add(New ListViewItem(New String() {"col 1", "col 2", "col 3"}))
    or this:
    vb.net Code:
    1. With Me.ListView1.Items.Add("col 1").SubItems
    2.     .Add("col 2")
    3.     .Add("col 3")
    4. End With
    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

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [2008] Help with ListView

    Ohhh!! Great, thanks. But I have another question regarding that... If I have a text file with 3 words:

    help me please

    Can I "read" that file and then add those words to each column? Help in column 1, me in column 2 and please in column 3? By adding a range or do i do it by adding subitems?
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: [2008] Help with ListView

    vb Code:
    1. Dim TextFileContent() As String = IO.File.ReadAllLines("path As String")

    Will return a string() of the contents of a text file at a given path. Basic text parsing from there.

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

    Re: [2008] Help with ListView

    As ForumAccount says, ReadAllLines will return a String array where each element is a line from the file. You can then treat each element of the array as you can any other String. If you want to split a string into individual words then you can call its Split method and specify a space as the delimiter.

    String.Split also returns a String array and you'll note that the first code snippet in my previous post shows you how to create a ListViewItem using a String array. That means that you can pass the result of String.Split directly to the ListViewItem constructor, e.g.
    vb.net Code:
    1. For Each line As String In IO.File.ReadAllLines("file path here")
    2.     Me.ListView1.Items.Add(New ListViewItem(line.Split(" "c)))
    3. Next
    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

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [2008] Help with ListView

    Thanks, it works perfectly. But i just noticed, that i want it to skip one column...

    Lets take the other example:

    Help me please

    I want Help to be in the first column, me in the second one and please in the fourth one, so that the third column can be free for another input. How can i do that? And also, I don't understand why there is a "c" in

    Me.ListView1.Items.Add(New ListViewItem(line.Split(" "c))
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

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

    Re: [2008] Help with ListView

    Quote Originally Posted by tassa
    Thanks, it works perfectly. But i just noticed, that i want it to skip one column...

    Lets take the other example:

    Help me please

    I want Help to be in the first column, me in the second one and please in the fourth one, so that the third column can be free for another input. How can i do that?
    This is from my first reply:
    Note that, if you want an empty "cell", you must still add a subitem but leave its text blank.
    Quote Originally Posted by tassa
    And also, I don't understand why there is a "c" in

    Me.ListView1.Items.Add(New ListViewItem(line.Split(" "c))
    The lower-case 'c' indicates a Char literal rather than a String literal that contains a single character.
    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

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [2008] Help with ListView

    Yeah, I got how to leave a "cell" empty, I forgot to edit the post.

    Ok, well now I want to "fill" that empty cell with the text in a second combo box. But instead of entering the text in that third column in that row, it adds it in the next row, but in the correct column.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

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

    Re: [2008] Help with ListView

    Like I said, each row in the ListView corresponds to a ListViewItem and each column corresponds to a ListViewSubItem of that item. If you want to set the text of column C in row R then you need to get the Cth subitem of the Rth item and set its Text property. Remember that, like all lists in VB.NET, the items and subitems of a ListView are zero-based, i.e. the first item is at index 0, which means that the Rth row is the item at index (R - 1) and the Cth subitem is the subitem at index (C - 1). So, to set the text in the Cth column of the Rth row to "Hello World" you would do this:
    vb.net Code:
    1. myListView.Items(R - 1).SubItems(C - 1).Text = "Hello World"
    Note that to set the text in the first column you can either set the Text property of the item itself, or you can set the Text property of the first subitem.
    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