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.
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:
ListView1.Items.Clear()
Dim adi As New ListViewItem("") ' 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)
adi.SubItems(1).Text = "second"
vb Code:
ListView1.Items.Clear()
Dim adi As New ListViewItem("") ' will add to column(0)
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
Dim adi As New ListViewItem("") ' will add to column(0)
adi.SubItems.Add("second") 'will add to column(1)
adi.SubItems.Add("third") 'will add to column(2)
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.
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.
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.
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.
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.
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.