Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2015. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Program Name: ListView Demo
- '
- ' Author: Joseph L. Bolen
- ' Date Created: May 2015
- '
- ' Description: Demonstrate the use of the ListView, the ErrorProvider,
- ' StreamReader and StreamWriter, and the Split method.
- '
- ' Documentation is at:
- ' App's screen image is at: http://imgur.com/SzcxdlU
- ' App's Visual Basic .NET code is at http://pastebin.com/hbrNyjXu
- ' Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
- '-------------------------------------------------------------------------------------------
- Imports System.IO
- Imports System.Text
- Public Class MainForm
- #Region " Declare Class level variables."
- Private lvItem As ListViewItem
- Private pathFile As String = My.Application.Info.DirectoryPath & "\ListViewItems.csv"
- #End Region
- #Region " Form Events."
- ' Form loading event
- Private Sub MainForm_Load(sender As Object, e As EventArgs) _
- Handles MyBase.Load
- DefineListView1()
- #If DEBUG Then
- LoadDummyData()
- #End If
- ' Set Buttons.
- AddButton.Enabled = True
- UpdateButton.Enabled = False
- DeleteButton.Enabled = False
- End Sub
- ' Form closing event.
- Private Sub MainForm_FormClosing(sender As Object, e As FormClosingEventArgs) _
- Handles Me.FormClosing
- 'If ErrorProvier has error, allow close of program.
- e.Cancel = False
- End Sub
- #End Region
- #Region " ListView Main Events - Add, Modify, Delete."
- ' Load textboxes with selected ListView Item.
- Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) _
- Handles ListView1.SelectedIndexChanged
- If ListView1.SelectedItems.Count > 0 Then
- lvItem = ListView1.SelectedItems(0)
- DisplayData()
- Else
- ClearData()
- End If
- End Sub
- ' Clear button.
- Private Sub ClearButton_Click(sender As Object, e As EventArgs) _
- Handles ClearButton.Click
- ClearData()
- End Sub
- ' Add ListView Item.
- Private Sub AddButton_Click(sender As Object, e As EventArgs) _
- Handles AddButton.Click
- If ValidateChildren() Then
- Dim fullName As String = LastNameTextBox.Text.Trim
- If FirstNameTextBox.Text.Trim <> String.Empty Then
- fullName &= ", " & FirstNameTextBox.Text
- End If
- Dim findListViewItem As New ListViewItem
- findListViewItem = ListView1.FindItemWithText(fullName, False, 0, False)
- If IsNothing(findListViewItem) Then
- AddListViewItem()
- ClearData()
- Else
- MessageBox.Show("Not added. Corresponding ListView Item already exist.",
- Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
- End If
- End If
- End Sub
- ' Update ListView Item.
- Private Sub UpdateButton_Click(sender As Object, e As EventArgs) _
- Handles UpdateButton.Click
- If ValidateChildren() Then
- Dim fullName As String = LastNameTextBox.Text.Trim
- If FirstNameTextBox.Text.Trim <> String.Empty Then
- fullName &= ", " & FirstNameTextBox.Text
- End If
- Dim findListViewItem As New ListViewItem
- findListViewItem = ListView1.FindItemWithText(fullName, False, 0, False)
- If Not IsNothing(findListViewItem) Then
- If findListViewItem.Index = lvItem.Index Then
- UpdateListViewItem(lvItem)
- ClearData()
- Else
- MessageBox.Show("Not Updated. Corresponding ListView Item already exist.",
- Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
- End If
- Else
- UpdateListViewItem(lvItem)
- ClearData()
- End If
- End If
- End Sub
- ' Delete ListView Item.
- Private Sub DeleteButton_Click(sender As Object, e As EventArgs) _
- Handles DeleteButton.Click
- ListView1.Items.RemoveAt(ListView1.SelectedIndices(0))
- ' Also, it could have been coded ...
- 'ListView1.Items.Remove(ListView1.SelectedItems(0))
- 'ListView1.Items.Remove(lvItem)
- ClearData()
- End Sub
- ' Add a ListView Item.
- Private Sub AddListViewItem()
- Dim fullName As String = LastNameTextBox.Text
- If FirstNameTextBox.Text.Trim <> String.Empty Then
- fullName &= ", " & FirstNameTextBox.Text
- End If
- lvItem = ListView1.Items.Add(fullName)
- With lvItem.SubItems
- .Add(Address1TextBox.Text)
- .Add(Address2TextBox.Text)
- .Add(CityTextBox.Text)
- .Add(StateMaskTextBox.Text)
- .Add(ZipCodeMaskedTextBox.Text)
- .Add(PhoneMaskedTextBox.Text)
- End With
- End Sub
- ' Update a ListView Item.
- Private Sub UpdateListViewItem(ByVal lvItem As ListViewItem)
- Dim fullName As String = LastNameTextBox.Text.Trim
- If FirstNameTextBox.Text.Trim <> String.Empty Then
- fullName &= ", " & FirstNameTextBox.Text
- End If
- lvItem.Text = fullName
- With lvItem
- .SubItems(1).Text = Address1TextBox.Text
- .SubItems(2).Text = Address2TextBox.Text
- .SubItems(3).Text = CityTextBox.Text
- .SubItems(4).Text = StateMaskTextBox.Text
- .SubItems(5).Text = ZipCodeMaskedTextBox.Text
- .SubItems(6).Text = PhoneMaskedTextBox.Text
- End With
- End Sub
- ' Clear Textboxes
- Private Sub ClearData()
- ErrorProvider1.Clear()
- FirstNameTextBox.Clear()
- LastNameTextBox.Clear()
- Address1TextBox.Clear()
- Address2TextBox.Clear()
- CityTextBox.Clear()
- StateMaskTextBox.Clear()
- ZipCodeMaskedTextBox.Clear()
- PhoneMaskedTextBox.Clear()
- FirstNameTextBox.Focus()
- ' Set Buttons.
- AddButton.Enabled = True
- UpdateButton.Enabled = False
- DeleteButton.Enabled = False
- lvItem = Nothing
- ListView1.SelectedIndices.Clear()
- End Sub
- ' Display ListView Item's Text and subitems.
- Private Sub DisplayData()
- With lvItem
- Dim field As String() = .Text.Split(","c)
- LastNameTextBox.Text = field(0)
- If field.Length > 1 Then FirstNameTextBox.Text = field(1).Trim
- Address1TextBox.Text = .SubItems(1).Text
- Address2TextBox.Text = .SubItems(2).Text
- CityTextBox.Text = .SubItems(3).Text
- StateMaskTextBox.Text = .SubItems(4).Text
- ZipCodeMaskedTextBox.Text = .SubItems(5).Text
- PhoneMaskedTextBox.Text = .SubItems(6).Text
- End With
- ' Set Buttons.
- AddButton.Enabled = False
- UpdateButton.Enabled = True
- DeleteButton.Enabled = True
- End Sub
- ' Validate LastName TextBox - required.
- Private Sub LastNameTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) _
- Handles LastNameTextBox.Validating
- ErrorProvider1.SetError(LastNameTextBox, String.Empty)
- If LastNameTextBox.Text.Trim = String.Empty Then
- ErrorProvider1.SetError(LastNameTextBox, "Last Name is a required field.")
- e.Cancel = True
- Beep()
- End If
- End Sub
- #End Region
- #Region " Define ListView and Debug Dummy Load of items."
- ' Define the ListView.
- Private Sub DefineListView1()
- ' Define and create ListView columns for ListView1.
- With ListView1
- .View = View.Details
- .GridLines = True
- .FullRowSelect = True
- .MultiSelect = False
- .Sorting = SortOrder.Ascending
- End With
- ' Add Header columns to ListView
- Dim header1, header2, header3, header4, header5, header6, header7 As New ColumnHeader
- With header1
- .Text = "Full Name"
- .TextAlign = HorizontalAlignment.Left
- .Width = 175
- End With
- With header2
- .Text = "Address 1"
- .TextAlign = HorizontalAlignment.Left
- .Width = 175
- End With
- With header3
- .Text = "Address 2"
- .TextAlign = HorizontalAlignment.Left
- .Width = 175
- End With
- With header4
- .Text = "City"
- .TextAlign = HorizontalAlignment.Left
- .Width = 175
- End With
- With header5
- .Text = "State"
- .TextAlign = HorizontalAlignment.Left
- .Width = 50
- End With
- With header6
- .Text = "Zip Code"
- .TextAlign = HorizontalAlignment.Left
- .Width = 100
- End With
- With header7
- .Text = "Phone"
- .TextAlign = HorizontalAlignment.Left
- .Width = 125
- End With
- With ListView1.Columns
- .Add(header1)
- .Add(header2)
- .Add(header3)
- .Add(header4)
- .Add(header5)
- .Add(header6)
- .Add(header7)
- End With
- End Sub
- ' Dummy load
- Private Sub LoadDummyData()
- Dim lvItem As ListViewItem
- ListView1.BeginUpdate()
- lvItem = ListView1.Items.Add("Bolen, Joe")
- With lvItem.SubItems
- .Add("123 Crooked Dr")
- .Add("")
- .Add("Dover")
- .Add("OH")
- .Add("44622")
- .Add("(330) 555-1212")
- End With
- lvItem = ListView1.Items.Add("Smith, John")
- With lvItem.SubItems
- .Add("123 Main St")
- .Add("Apt. 6B")
- .Add("Dover")
- .Add("OH")
- .Add("44622")
- .Add("")
- End With
- lvItem = ListView1.Items.Add("Peterson, Sally")
- With lvItem.SubItems
- .Add("555 Lovers Ln")
- .Add("")
- .Add("New Philadelphia")
- .Add("OH")
- .Add("44633")
- .Add("(330) 555-2222")
- End With
- lvItem = ListView1.Items.Add("Harstine, Sam")
- With lvItem.SubItems
- .Add("1287 Schneiders Crossing Rd")
- .Add("")
- .Add("Dover")
- .Add("OH")
- .Add("44622")
- .Add("(330) 555-2345")
- End With
- lvItem = ListView1.Items.Add("Williams, Abby")
- With lvItem.SubItems
- .Add("1038 N Broadway")
- .Add("Suite C")
- .Add("New Philadelphia")
- .Add("OH")
- .Add("44663")
- .Add("(330) 555-5687")
- End With
- ListView1.EndUpdate()
- End Sub
- #End Region
- #Region " Load from and Save to a file the ListView items."
- ' Load from file ListView Items.
- Private Sub LoadButton_Click(sender As Object, e As EventArgs) _
- Handles LoadButton.Click
- ListView1.Items.Clear()
- ListView1.BeginUpdate()
- Try
- Using fs As New FileStream(pathFile, FileMode.Open)
- Using reader As StreamReader = New StreamReader(fs)
- Dim line As String
- Dim field As String()
- Dim lvItem As ListViewItem
- Dim fullName As String
- While Not reader.EndOfStream
- line = reader.ReadLine
- field = line.Split(","c)
- fullName = field(0)
- If field(1).Trim <> String.Empty Then
- fullName &= ", " & field(1)
- End If
- lvItem = ListView1.Items.Add(fullName)
- With lvItem.SubItems
- .Add(field(2))
- .Add(field(3))
- .Add(field(4))
- .Add(field(5))
- .Add(field(6))
- .Add(field(7))
- End With
- End While
- End Using
- End Using
- Catch ex As Exception
- End Try
- ListView1.EndUpdate()
- End Sub
- ' Save to File ListView Items.
- Private Sub SaveButton_Click(sender As Object, e As EventArgs) _
- Handles SaveButton.Click
- Dim itemsCount As Integer = ListView1.Items.Count
- If itemsCount = 0 Then Exit Sub
- Try
- Using fs As New FileStream(pathFile, FileMode.Create)
- Using writer As StreamWriter = New StreamWriter(fs, Encoding.Default)
- For Each lvItem As ListViewItem In ListView1.Items
- Dim field As String() = lvItem.Text.Split(","c)
- writer.Write(field(0) & ",")
- If field.Length > 1 Then
- writer.Write(field(1).Trim & ",")
- Else
- writer.Write(",")
- End If
- With lvItem
- writer.Write(.SubItems(1).Text & ",")
- writer.Write(.SubItems(2).Text & ",")
- writer.Write(.SubItems(3).Text & ",")
- writer.Write(.SubItems(4).Text & ",")
- writer.Write(.SubItems(5).Text & ",")
- writer.Write(.SubItems(6).Text & ControlChars.CrLf)
- End With
- Next
- End Using
- End Using
- Catch ex As Exception
- End Try
- End Sub
- #End Region
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement