hello guys,

I need help regarding with my listview how will you make in listview if the name is already exist in the listview it will no longer accept or there is a msgbox("name already in listview") and the name is in the first column of the listview. Or what i want is that if the name is in the listview is equal to the textbox.text which is where u input the name that shown in the listview there is a msgbox that will appear will not accept that coz the name is already in the listview....

You need to iterate through all the rows in the listview, and compare the content of the cells in the first column with the content in the textbox.
Like so:

For Each item As ListViewItem In listview1.Items
   If item.SubItems(0).Text = textbox1.Text Then
      MessageBox.Show("That name is already in the list.", "Existing name", MessageBoxButtons.OK, MessageBoxIcon.Stop)
      Exit Sub
   End If
Next

thank u so much oxeigen...and it works one last question because in my I.O.File i use this to save the data in the listview in a c:\test.txt but the problem is that it only save one data and every time i click the save the previous data that i save is deleted and it change the new one that i save...and also when i click the remove in the listview it will also delete the data that save in the c:\test.txt and the format should like this

First name | lastname | address |
rae alanah new york
john smith los angeles

just like that...

here the code that i did...

Dim myFile As String = "C:\test.txt" '//file location
        System.IO.File.WriteAllText(myFile, TextBox1.Text & " " & ComboBox1.Text & " " & TextBox4.Text & " " & ComboBox2.Text & " " & TextBox3.Text & " " & TextBox2.Text & " " & TextBox5.Text)

New Thread - http://www.daniweb.com/forums/thread314639.html

I'm assuming that this text file is to be used as permanent record storage for when you are not running your program.
If that is the case, then I would strongly suggest that you reformat the text file into a CSV format (which also can be imported into Excel).

First name,lastname,adress
rae,alanah,new york
john,smith,los angeles

Here's my solution on how to do this:

Dim myFile As String = "C:\test.txt"
Dim stream As New IO.FileStream(myFile, IO.FileMode.Create)
Dim writer As New IO.StreamWriter(stream)

writer.WriteLine("First name,lastname,address")

For Each item As ListViewItem In ListView1.Items
   writer.WriteLine(item.SubItems(0).Text & "," & item.SubItems(1).Text & ","
 & item.SubItems(2).Text)
Next
writer.Flush()
writer.Close()
stream.Close()

This will effectivaly write to the file only the records you have in the listview. And if the file already exists, it will be overwritten.

ok thanks...but it is possible when u delete in the listview it will also delete in test.txt??

If you put that code in the same method that removes the selected item from the listview, then yes, it will be deleted from the file as well.

i try this but the problem it delete all the data in the list view it should only delete the data in listview that i selected

Dim myFile As String = "C:\report.txt"
        Dim stream As New IO.FileStream(myFile, IO.FileMode.Create)
        Dim writer As New IO.StreamWriter(stream)
 
         For Each item As ListViewItem In ListView1.SelectedItems
                    item.Remove()
                    MsgBox("File Deleted Permanently.", MsgBoxStyle.Information)
                Next
        Next
        writer.Flush()
        writer.Close()
        stream.Close()

but the problem of that even if i select one data in listview still all data in the report.txt are deleted..i try to use the listview1.selecteditems.item(0) but does not allowed...help please

Yeah, I can see the problem.
Try this:

Private Sub buttonDelete(ByVal sender As Object, ByVal e As EventArgs) Handles buttonDelete.Click
   'Exit sub if no items are selected.
   If ListView1.SelectedItems.Count <= 0 Then Exit Sub
   Try
      'Remove the selected item from the listview
      ListView1.SelectedItems(0).Remove()

      'Delete the file, and recreate it based on remaining items in the listview
      Dim myFile As String = "C:\test.txt"
      Dim stream As New IO.FileStream(myFile, IO.FileMode.Create)
      Dim writer As New IO.StreamWriter(stream)

      writer.WriteLine("First name,lastname,address")

      For Each item As ListViewItem In ListView1.Items
         writer.WriteLine(item.SubItems(0).Text & "," & item.SubItems(1).Text & "," & item.SubItems(2).Text)
      Next
      writer.Flush()
      writer.Close()
      stream.Close()

      MessageBox.Show("Record has been permanently removed","Record Purge", MessageBoxButtons.OK, MessageBoxIcon.Information)
   Catch ex As Exception
      MessageBox.Show(ex.ToString(), "An error occured", MessageBoxButtons.OK, MessageBoxIcon.Error)
   End Try
End Sub

yeah i got it thank u so much:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.