Results 1 to 21 of 21

Thread: Add an item to one column of ListView.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Question Add an item to one column of ListView.

    This code is to add items to columns of ListView.
    vb Code:
    1. Dim adi As New ListViewItem(TextBox1.Text) ' will add to column(0)
    2. adi.SubItems.Add(TextBox2.Text) 'will add to column(1)
    3. adi.SubItems.Add(TextBox3.Text) 'will add to column(2)
    4. ListView2.Items.Add(adi)
    5. ListView2.View = View.Details
    I'm looking to create a code that can add item only for one column individually as it show in this :

    vb Code:
    1. Dim adi As New ListViewItem
    2. ' Code to only add  to column(2) ....Example
    3. ListView2.Items.Add(adi)
    4. ListView2.View = View.Details

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Add an item to one column of ListView.

    you can just add the first column.

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Add an item to one column of ListView.

    Hey,

    If I am not mistaken, I think what he is asking for is this:

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim adi As New ListViewItem("") ' will add to column(0)
            adi.SubItems.Add("") 'will add to column(1)
            adi.SubItems.Add("three") 'will add to column(2)
            ListView1.Items.Add(adi)
            ListView1.View = View.Details
        End Sub
    Probably a more correct way to do it, but this gets the job done.

    Enters no text into the first two columns, and only puts something into the second one.

    If this isn't what you want, post back, and explain exactly what you want.

    Hope this helps!!

    Gary

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Re: Add an item to one column of ListView.

    Your code it works as it shows in the pic1.
    I want it to wrok as it show in the pic2.
    vb Code:
    1. Private Sub Button1_Click
    2. Dim adi As New ListViewItem("") ' will add to column(0)
    3.         adi.SubItems.Add("") 'will add to column(1)
    4.         adi.SubItems.Add("three") 'will add to column(2)
    5.         ListView2.Items.Add(adi)
    6.         ListView2.View = View.Details
    7. End Sub

    vb Code:
    1. Private Sub Button2_Click
    2. Dim adi As New ListViewItem("") ' will add to column(0)
    3.         adi.SubItems.Add("second") 'will add to column(1)
    4.         adi.SubItems.Add("") 'will add to column(2)
    5.         ListView2.Items.Add(adi)
    6.         ListView2.View = View.Details
    7.     End Sub
    Attached Images Attached Images  

  5. #5
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: Add an item to one column of ListView.

    vb.net Code:
    1. Private Sub Button2_Click( _
    2.     ByVal sender As System.Object, _
    3.     ByVal e As System.EventArgs _
    4. ) Handles Button2.Click
    5.  
    6.     Dim adi As ListViewItem
    7.  
    8.     adi = New ListViewItem("")
    9.     adi.SubItems.Add("")
    10.     adi.SubItems.Add("third")
    11.     ListView1.Items.Add(adi)
    12.  
    13.     adi.SubItems(1).Text = "second"
    14. End Sub

    While adding text to next column, you need to use the previous used ListViewItem object like I did in above example.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Re: Add an item to one column of ListView.

    It didn't work as I said. It must work indivdually not as you wrote. your code will insert tow items. I treid these code but they work as it show above in pic1. Not as I said in Pic2
    vb Code:
    1. ListView1.Items.Clear()
    2.         Dim adi As New ListViewItem("") ' will add to column(0)
    3.         adi.SubItems.Add("") 'will add to column(1)
    4.         adi.SubItems.Add("") 'will add to column(2)
    5.         ListView2.Items.Add(adi)
    6.         adi.SubItems(1).Text = "second"

    vb Code:
    1. ListView1.Items.Clear()
    2.         Dim adi As New ListViewItem("") ' will add to column(0)
    3.         adi.SubItems.Add("") 'will add to column(1)
    4.         adi.SubItems.Add("") 'will add to column(2)
    5.         ListView2.Items.Add(adi)
    6.         adi.SubItems(2).Text = "Third"
    Last edited by nader; Oct 6th, 2008 at 08:55 AM.

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Add an item to one column of ListView.

    Hey,

    To get what you want in pic2, doesn't this work:

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim adi As New ListViewItem("") ' will add to column(0)
            adi.SubItems.Add("second") 'will add to column(1)
            adi.SubItems.Add("three") 'will add to column(2)
            ListView1.Items.Add(adi)
            ListView1.View = View.Details
        End Sub
    Gary

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Add an item to one column of ListView.

    try this:

    vb Code:
    1. Dim adi As New ListViewItem("") ' will add to column(0)
    2. adi.SubItems.Add("second") 'will add to column(1)
    3. adi.SubItems.Add("third") 'will add to column(2)
    4. ListView2.Items.Add(adi)

    edit: beat me again!

  9. #9

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Re: Add an item to one column of ListView.

    Quote Originally Posted by .paul.
    try this:

    vb Code:
    1. Dim adi As New ListViewItem("") ' will add to column(0)
    2. adi.SubItems.Add("second") 'will add to column(1)
    3. adi.SubItems.Add("third") 'will add to column(2)
    4. ListView2.Items.Add(adi)

    edit: beat me again!
    This code will add the items togather. I want to add items each one alone in single Sub as I told in post #4 and insert the items as it show in the pic2.

  11. #11
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Add an item to one column of ListView.

    Hey,

    I think myself and paul must be missing something then.

    We both posted the exact same code, that will result in what you have displayed in Pic2. Why does it not do what you want?

    Gary

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Re: Add an item to one column of ListView.

    Quote Originally Posted by gep13
    Hey,

    I think myself and paul must be missing something then.

    We both posted the exact same code, that will result in what you have displayed in Pic2. Why does it not do what you want?

    Gary
    Because the code will add the items as I said togather. IT's mean when I click on the button will add the second and three togather. but I want add each item using one (sub) button alone.

    vb Code:
    1. Private Sub Button1_Click
    2. 'add only the item second
    3. End Sub

    vb Code:
    1. Private Sub Button2_Click
    2. 'add only the item three
    3. End Sub
    The result must be like pic2 not like pic1

  13. #13
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Add an item to one column of ListView.

    Ah, ok, I think I get you now.

    What you want to do is to click the button once, and add the "item second", then when you click the same button again?!?, you want to add the "item three".

    If that is what you are trying to do, then I think you might want to re-think the execution of the code, as this does not seem very intuitive.

    However, in order to get what you are asking for, the following code should work:

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim adi As New ListViewItem("") ' will add to column(0)
            adi.SubItems.Add("second") 'will add to column(1)
            adi.SubItems.Add("") 'will add to column(2)
            ListView1.Items.Add(adi)
            ListView1.View = View.Details
        End Sub
    
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            ListView1.FocusedItem.SubItems(2).Text = "three"
        End Sub
    In order for the code in the second button to work, you need to first select the row that you want to update.

    That make sense?

    Gary

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Re: Add an item to one column of ListView.

    Not succeed with me.

  15. #15
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Add an item to one column of ListView.

    What exactly did not succeed?

    I tested the above code and the result was what you displayed in Pic2.

    Did you select the listviewitem before pressing the second button? Can you post the code that you are using?

    Gary

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Re: Add an item to one column of ListView.

    vb Code:
    1. Private Sub Button1_Click
    2. Dim adi As New ListViewItem("") ' will add to column(0)
    3.         adi.SubItems.Add("") 'will add to column(1)
    4.         adi.SubItems.Add("") 'will add to column(2)
    5.         ListView2.Items.Add(adi)
    6.         adi.SubItems(1).Text = "second"
    7.   End Sub

    vb Code:
    1. Private Sub Button1_Click
    2. Dim adi As New ListViewItem("") ' will add to column(0)
    3.         ListView2.Items.Add(adi)
    4.         ListView2.FocusedItem.SubItems(2).Text = "three"
    5.   End Sub
    The error : "Object reference not set to an instance of an object"

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

    Re: Add an item to one column of ListView.

    If you expect to add a subitem to an existing item then you can't create a new item and add a subitem to that. If you buy a new car and put a fancy wheel on it, then buy another new car and put a fancy wheel on it, would you be surprised that you have two cars with one fancy wheel each? I would think not.

    If you already have a ListViewItem in the ListView and you want to add a subitem to it, you have to get a reference to that existing ListViewItem, not create a second new ListViewItem.

    So, how are you going to identify what item to add the subitem to? Is it the item currently selected by the user? Is there only ever going to be one item? Something else? If you explain your situation then we can provide a solution.
    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

  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Re: Add an item to one column of ListView.

    Here is a pic I hope it will clarify what I want.
    When I execute a sub1 ( like click Button ). it will add an item to column 1 of listview.When I execute sub2, it will add an item to column 2 of listview beside the last item... and so.. as it show in the pic.
    Attached Images Attached Images  

  19. #19
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Add an item to one column of ListView.

    Hey,

    If you take the code that I gave you, then you will be able to do just that.

    The difference between the code that I posted, and the code that you are using, is that I am using a reference to the listviewitem that was created when the first sub was executed. On the other hand, you are creating a new listviewitem in each sub, and this is not right, as jm as explained!!

    So, in the first sub, i.e. the first button click do this:

    Code:
    'This procedure will create a listviewitem and put the word "First" into the first column
    Private Sub Button1_Click
        Dim adi As New ListViewItem("First") ' will add to column(0)
        adi.SubItems.Add("") 'will add to column(1)
        adi.SubItems.Add("") 'will add to column(2)
        ListView2.Items.Add(adi)
    End Sub
    Then, in another button click event to this:

    Code:
    'Using a reference to the currently selected listviewitem, this will add the 
    'word "Second" into the second column
    Private Sub Button2_Click
        ListView2.FocusedItem.SubItems(1).Text = "Second"
    End Sub
    Then, in another button click event put the following:

    Code:
    'Using a reference to the currently selected listviewitem, this will add the 
    'word "Third" into the Third column
    Private Sub Button3_Click
        ListView2.FocusedItem.SubItems(2).Text = "Third"
    End Sub
    For demonstration purposes, here I am using three separate buttons. However, it order to incorporate these three methods into one, you could inspect the current text property of each of the columns in the selected listviewitem to determine which column should be populated next.

    Does this make sense?

    Gary

  20. #20

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Re: Add an item to one column of ListView.

    Thsi code not work only if there is a focus on the column.
    I don't want it like this.

  21. #21
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Add an item to one column of ListView.

    You are going to have to explain exactly what you want then?

    You have to have some reference to the listviewitem that you are trying to update!!

    In the example that I have given, this is done by looking at the currently selected row. If this is not what you want, then what do you want?

    Is there only going to be one row in the listview? Can the user specify the row that they want to update?

    Tell us what you want to happen, and then I am sure someone will be able to help.

    Gary

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