By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,343 Members | 925 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,343 IT Pros & Developers. It's quick & easy.

Items in listview control

P: n/a
Hi folks,

I am unfamiliar with the listview control, and I need a bit of help. My
listview control has 2 columns. I'm trying to populate the first column
with text in a textbox when a user clicks the "add" button, and also
populate the second column with text depending on what went in the first
column. I have a few questions, though. I can add the first column text
simply with listview.items.add, but how do I add text to the second column?
Also, how would I cycle through the items in the listview control (first
column) to make sure I don't add duplicate entries? Thanks for any help.

-J
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
* "OpticTygre" <op********@adelphia.net> scripsit:
I am unfamiliar with the listview control, and I need a bit of help. My
listview control has 2 columns. I'm trying to populate the first column
with text in a textbox when a user clicks the "add" button, and also
populate the second column with text depending on what went in the first
column. I have a few questions, though. I can add the first column text
simply with listview.items.add, but how do I add text to the second column?
\\\
Me.ListView1.Items(10).SubItems.Add("Foo")
///
Also, how would I cycle through the items in the listview control (first
column) to make sure I don't add duplicate entries? Thanks for any help.


\\\
Dim lvi As ListViewItem
For Each lvi In Me.ListView1.Items
...
Next lvi
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #2

P: n/a
Something like this?

Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
Dim sIP As IPAddress

If txtIPAddress.Text = "" Then Exit Sub

Try
sIP = IPAddress.Parse(txtIPAddress.Text)
Try
For Each lstviewitem As ListViewItem In lstIPAddresses.Items
If lstviewitem.Text = txtIPAddress.Text Then
MessageBox.Show("IP Address is already in list.",
"Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
Next
Dim item1 As New ListViewItem(txtIPAddress.Text)
item1.SubItems.Add("Not yet tested")
lstIPAddresses.Items.Add(item1)
txtIPAddress.Clear()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Catch ex As Exception
MessageBox.Show("Not a valid IP Address.", "Invalid Entry",
MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtIPAddress.Clear()
End Try
End Sub
It works, but I don't know if it's the best way of doing it....

-J

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2m************@uni-berlin.de...
* "OpticTygre" <op********@adelphia.net> scripsit:
I am unfamiliar with the listview control, and I need a bit of help. My
listview control has 2 columns. I'm trying to populate the first column
with text in a textbox when a user clicks the "add" button, and also
populate the second column with text depending on what went in the first
column. I have a few questions, though. I can add the first column text simply with listview.items.add, but how do I add text to the second
column?
\\\
Me.ListView1.Items(10).SubItems.Add("Foo")
///
Also, how would I cycle through the items in the listview control (first
column) to make sure I don't add duplicate entries? Thanks for any
help.
\\\
Dim lvi As ListViewItem
For Each lvi In Me.ListView1.Items
...
Next lvi
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.