Add an item to one column of ListView.
This code is to add items to columns of ListView.
vb Code:
Dim adi As New ListViewItem(TextBox1.Text) ' will add to column(0)
adi.SubItems.Add(TextBox2.Text) 'will add to column(1)
adi.SubItems.Add(TextBox3.Text) 'will add to column(2)
ListView2.Items.Add(adi)
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:
Dim adi As New ListViewItem
' Code to only add to column(2) ....Example
ListView2.Items.Add(adi)
ListView2.View = View.Details
Re: Add an item to one column of ListView.
you can just add the first column.
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
1 Attachment(s)
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:
Private Sub 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)
ListView2.Items.Add(adi)
ListView2.View = View.Details
End Sub
vb Code:
Private Sub Button2_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)
ListView2.Items.Add(adi)
ListView2.View = View.Details
End Sub
Re: Add an item to one column of ListView.
vb.net Code:
Private Sub Button2_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles Button2.Click
Dim adi As ListViewItem
adi = New ListViewItem("")
adi.SubItems.Add("")
adi.SubItems.Add("third")
ListView1.Items.Add(adi)
adi.SubItems(1).Text = "second"
End Sub
While adding text to next column, you need to use the previous used ListViewItem object like I did in above example.
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:
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)
adi.SubItems.Add("") 'will add to column(1)
adi.SubItems.Add("") 'will add to column(2)
ListView2.Items.Add(adi)
adi.SubItems(2).Text = "Third"
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
Re: Add an item to one column of ListView.
try this:
vb Code:
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!
Re: Add an item to one column of ListView.
Re: Add an item to one column of ListView.
Quote:
Originally Posted by .paul.
try this:
vb Code:
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.
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
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:
Private Sub Button1_Click
'add only the item second
End Sub
vb Code:
Private Sub Button2_Click
'add only the item three
End Sub
The result must be like pic2 not like pic1
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
Re: Add an item to one column of ListView.
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
Re: Add an item to one column of ListView.
vb Code:
Private Sub Button1_Click
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"
End Sub
vb Code:
Private Sub Button1_Click
Dim adi As New ListViewItem("") ' will add to column(0)
ListView2.Items.Add(adi)
ListView2.FocusedItem.SubItems(2).Text = "three"
End Sub
The error : "Object reference not set to an instance of an object"
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.
1 Attachment(s)
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.
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
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.
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