Advertisement
JayBeeOH

ListView Demo

Jun 16th, 2015
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 14.59 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2015. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   ListView Demo
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   May 2015
  16. '
  17. ' Description:    Demonstrate the use of the ListView, the ErrorProvider,
  18. '                 StreamReader and StreamWriter, and the Split method.
  19. '
  20. '                 Documentation is at:
  21. '                   App's screen image is at: http://imgur.com/SzcxdlU
  22. '                   App's Visual Basic .NET code is at http://pastebin.com/hbrNyjXu
  23. '                   Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
  24. '-------------------------------------------------------------------------------------------
  25.  
  26. Imports System.IO
  27. Imports System.Text
  28.  
  29. Public Class MainForm
  30.  
  31. #Region " Declare Class level variables."
  32.  
  33.     Private lvItem As ListViewItem
  34.     Private pathFile As String = My.Application.Info.DirectoryPath & "\ListViewItems.csv"
  35.  
  36. #End Region
  37.  
  38. #Region " Form Events."
  39.  
  40.     ' Form loading event
  41.     Private Sub MainForm_Load(sender As Object, e As EventArgs) _
  42.         Handles MyBase.Load
  43.  
  44.         DefineListView1()
  45.  
  46. #If DEBUG Then
  47.         LoadDummyData()
  48. #End If
  49.         ' Set Buttons.
  50.         AddButton.Enabled = True
  51.         UpdateButton.Enabled = False
  52.         DeleteButton.Enabled = False
  53.     End Sub
  54.  
  55.     ' Form closing event.
  56.     Private Sub MainForm_FormClosing(sender As Object, e As FormClosingEventArgs) _
  57.         Handles Me.FormClosing
  58.  
  59.         'If ErrorProvier has error, allow close of program.
  60.         e.Cancel = False
  61.     End Sub
  62.  
  63. #End Region
  64.  
  65. #Region " ListView Main Events - Add, Modify, Delete."
  66.  
  67.  
  68.     ' Load textboxes with selected ListView Item.
  69.     Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) _
  70.         Handles ListView1.SelectedIndexChanged
  71.  
  72.         If ListView1.SelectedItems.Count > 0 Then
  73.             lvItem = ListView1.SelectedItems(0)
  74.             DisplayData()
  75.         Else
  76.             ClearData()
  77.         End If
  78.     End Sub
  79.  
  80.     ' Clear button.
  81.     Private Sub ClearButton_Click(sender As Object, e As EventArgs) _
  82.         Handles ClearButton.Click
  83.  
  84.         ClearData()
  85.     End Sub
  86.  
  87.     ' Add ListView Item.
  88.     Private Sub AddButton_Click(sender As Object, e As EventArgs) _
  89.         Handles AddButton.Click
  90.  
  91.         If ValidateChildren() Then
  92.  
  93.             Dim fullName As String = LastNameTextBox.Text.Trim
  94.             If FirstNameTextBox.Text.Trim <> String.Empty Then
  95.                 fullName &= ", " & FirstNameTextBox.Text
  96.             End If
  97.  
  98.             Dim findListViewItem As New ListViewItem
  99.             findListViewItem = ListView1.FindItemWithText(fullName, False, 0, False)
  100.             If IsNothing(findListViewItem) Then
  101.                 AddListViewItem()
  102.                 ClearData()
  103.             Else
  104.                 MessageBox.Show("Not added. Corresponding ListView Item already exist.",
  105.                                 Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
  106.             End If
  107.         End If
  108.     End Sub
  109.  
  110.     ' Update ListView Item.
  111.     Private Sub UpdateButton_Click(sender As Object, e As EventArgs) _
  112.         Handles UpdateButton.Click
  113.  
  114.         If ValidateChildren() Then
  115.  
  116.             Dim fullName As String = LastNameTextBox.Text.Trim
  117.             If FirstNameTextBox.Text.Trim <> String.Empty Then
  118.                 fullName &= ", " & FirstNameTextBox.Text
  119.             End If
  120.  
  121.             Dim findListViewItem As New ListViewItem
  122.             findListViewItem = ListView1.FindItemWithText(fullName, False, 0, False)
  123.             If Not IsNothing(findListViewItem) Then
  124.                 If findListViewItem.Index = lvItem.Index Then
  125.                     UpdateListViewItem(lvItem)
  126.                     ClearData()
  127.                 Else
  128.                     MessageBox.Show("Not Updated. Corresponding ListView Item already exist.",
  129.                                     Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
  130.                 End If
  131.             Else
  132.                 UpdateListViewItem(lvItem)
  133.                 ClearData()
  134.             End If
  135.         End If
  136.     End Sub
  137.  
  138.     ' Delete ListView Item.
  139.     Private Sub DeleteButton_Click(sender As Object, e As EventArgs) _
  140.         Handles DeleteButton.Click
  141.  
  142.         ListView1.Items.RemoveAt(ListView1.SelectedIndices(0))
  143.         ' Also, it could have been coded ...
  144.         'ListView1.Items.Remove(ListView1.SelectedItems(0))
  145.         'ListView1.Items.Remove(lvItem)
  146.         ClearData()
  147.     End Sub
  148.  
  149.     ' Add a ListView Item.
  150.     Private Sub AddListViewItem()
  151.  
  152.         Dim fullName As String = LastNameTextBox.Text
  153.         If FirstNameTextBox.Text.Trim <> String.Empty Then
  154.             fullName &= ", " & FirstNameTextBox.Text
  155.         End If
  156.  
  157.         lvItem = ListView1.Items.Add(fullName)
  158.         With lvItem.SubItems
  159.             .Add(Address1TextBox.Text)
  160.             .Add(Address2TextBox.Text)
  161.             .Add(CityTextBox.Text)
  162.             .Add(StateMaskTextBox.Text)
  163.             .Add(ZipCodeMaskedTextBox.Text)
  164.             .Add(PhoneMaskedTextBox.Text)
  165.         End With
  166.     End Sub
  167.  
  168.     ' Update a ListView Item.
  169.     Private Sub UpdateListViewItem(ByVal lvItem As ListViewItem)
  170.  
  171.         Dim fullName As String = LastNameTextBox.Text.Trim
  172.         If FirstNameTextBox.Text.Trim <> String.Empty Then
  173.             fullName &= ", " & FirstNameTextBox.Text
  174.         End If
  175.  
  176.         lvItem.Text = fullName
  177.         With lvItem
  178.             .SubItems(1).Text = Address1TextBox.Text
  179.             .SubItems(2).Text = Address2TextBox.Text
  180.             .SubItems(3).Text = CityTextBox.Text
  181.             .SubItems(4).Text = StateMaskTextBox.Text
  182.             .SubItems(5).Text = ZipCodeMaskedTextBox.Text
  183.             .SubItems(6).Text = PhoneMaskedTextBox.Text
  184.         End With
  185.     End Sub
  186.  
  187.     ' Clear Textboxes
  188.     Private Sub ClearData()
  189.  
  190.         ErrorProvider1.Clear()
  191.  
  192.         FirstNameTextBox.Clear()
  193.         LastNameTextBox.Clear()
  194.         Address1TextBox.Clear()
  195.         Address2TextBox.Clear()
  196.         CityTextBox.Clear()
  197.         StateMaskTextBox.Clear()
  198.         ZipCodeMaskedTextBox.Clear()
  199.         PhoneMaskedTextBox.Clear()
  200.  
  201.         FirstNameTextBox.Focus()
  202.  
  203.         ' Set Buttons.
  204.         AddButton.Enabled = True
  205.         UpdateButton.Enabled = False
  206.         DeleteButton.Enabled = False
  207.  
  208.         lvItem = Nothing
  209.         ListView1.SelectedIndices.Clear()
  210.     End Sub
  211.  
  212.     ' Display ListView Item's Text and subitems.
  213.     Private Sub DisplayData()
  214.  
  215.         With lvItem
  216.             Dim field As String() = .Text.Split(","c)
  217.             LastNameTextBox.Text = field(0)
  218.             If field.Length > 1 Then FirstNameTextBox.Text = field(1).Trim
  219.             Address1TextBox.Text = .SubItems(1).Text
  220.             Address2TextBox.Text = .SubItems(2).Text
  221.             CityTextBox.Text = .SubItems(3).Text
  222.             StateMaskTextBox.Text = .SubItems(4).Text
  223.             ZipCodeMaskedTextBox.Text = .SubItems(5).Text
  224.             PhoneMaskedTextBox.Text = .SubItems(6).Text
  225.         End With
  226.  
  227.         ' Set Buttons.
  228.         AddButton.Enabled = False
  229.         UpdateButton.Enabled = True
  230.         DeleteButton.Enabled = True
  231.     End Sub
  232.  
  233.     ' Validate LastName TextBox - required.
  234.     Private Sub LastNameTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) _
  235.         Handles LastNameTextBox.Validating
  236.  
  237.         ErrorProvider1.SetError(LastNameTextBox, String.Empty)
  238.  
  239.         If LastNameTextBox.Text.Trim = String.Empty Then
  240.             ErrorProvider1.SetError(LastNameTextBox, "Last Name is a required field.")
  241.             e.Cancel = True
  242.             Beep()
  243.         End If
  244.     End Sub
  245.  
  246. #End Region
  247.  
  248. #Region " Define ListView and Debug Dummy Load of items."
  249.  
  250.     ' Define the ListView.
  251.     Private Sub DefineListView1()
  252.  
  253.         ' Define and create ListView columns for ListView1.
  254.         With ListView1
  255.             .View = View.Details
  256.             .GridLines = True
  257.             .FullRowSelect = True
  258.             .MultiSelect = False
  259.             .Sorting = SortOrder.Ascending
  260.         End With
  261.  
  262.         ' Add Header columns to ListView
  263.         Dim header1, header2, header3, header4, header5, header6, header7 As New ColumnHeader
  264.  
  265.         With header1
  266.             .Text = "Full Name"
  267.             .TextAlign = HorizontalAlignment.Left
  268.             .Width = 175
  269.         End With
  270.  
  271.         With header2
  272.             .Text = "Address 1"
  273.             .TextAlign = HorizontalAlignment.Left
  274.             .Width = 175
  275.         End With
  276.  
  277.         With header3
  278.             .Text = "Address 2"
  279.             .TextAlign = HorizontalAlignment.Left
  280.             .Width = 175
  281.         End With
  282.  
  283.         With header4
  284.             .Text = "City"
  285.             .TextAlign = HorizontalAlignment.Left
  286.             .Width = 175
  287.         End With
  288.  
  289.         With header5
  290.             .Text = "State"
  291.             .TextAlign = HorizontalAlignment.Left
  292.             .Width = 50
  293.         End With
  294.  
  295.         With header6
  296.             .Text = "Zip Code"
  297.             .TextAlign = HorizontalAlignment.Left
  298.             .Width = 100
  299.         End With
  300.  
  301.         With header7
  302.             .Text = "Phone"
  303.             .TextAlign = HorizontalAlignment.Left
  304.             .Width = 125
  305.         End With
  306.  
  307.         With ListView1.Columns
  308.             .Add(header1)
  309.             .Add(header2)
  310.             .Add(header3)
  311.             .Add(header4)
  312.             .Add(header5)
  313.             .Add(header6)
  314.             .Add(header7)
  315.         End With
  316.     End Sub
  317.  
  318.     ' Dummy load
  319.     Private Sub LoadDummyData()
  320.  
  321.         Dim lvItem As ListViewItem
  322.  
  323.         ListView1.BeginUpdate()
  324.  
  325.         lvItem = ListView1.Items.Add("Bolen, Joe")
  326.         With lvItem.SubItems
  327.             .Add("123 Crooked Dr")
  328.             .Add("")
  329.             .Add("Dover")
  330.             .Add("OH")
  331.             .Add("44622")
  332.             .Add("(330) 555-1212")
  333.         End With
  334.  
  335.         lvItem = ListView1.Items.Add("Smith, John")
  336.         With lvItem.SubItems
  337.             .Add("123 Main St")
  338.             .Add("Apt. 6B")
  339.             .Add("Dover")
  340.             .Add("OH")
  341.             .Add("44622")
  342.             .Add("")
  343.         End With
  344.  
  345.         lvItem = ListView1.Items.Add("Peterson, Sally")
  346.         With lvItem.SubItems
  347.             .Add("555 Lovers Ln")
  348.             .Add("")
  349.             .Add("New Philadelphia")
  350.             .Add("OH")
  351.             .Add("44633")
  352.             .Add("(330) 555-2222")
  353.         End With
  354.  
  355.         lvItem = ListView1.Items.Add("Harstine, Sam")
  356.         With lvItem.SubItems
  357.             .Add("1287 Schneiders Crossing Rd")
  358.             .Add("")
  359.             .Add("Dover")
  360.             .Add("OH")
  361.             .Add("44622")
  362.             .Add("(330) 555-2345")
  363.         End With
  364.  
  365.         lvItem = ListView1.Items.Add("Williams, Abby")
  366.         With lvItem.SubItems
  367.             .Add("1038 N Broadway")
  368.             .Add("Suite C")
  369.             .Add("New Philadelphia")
  370.             .Add("OH")
  371.             .Add("44663")
  372.             .Add("(330) 555-5687")
  373.         End With
  374.  
  375.         ListView1.EndUpdate()
  376.     End Sub
  377.  
  378. #End Region
  379.  
  380. #Region " Load from and Save to a file the ListView items."
  381.  
  382.     ' Load from file ListView Items.
  383.     Private Sub LoadButton_Click(sender As Object, e As EventArgs) _
  384.         Handles LoadButton.Click
  385.  
  386.         ListView1.Items.Clear()
  387.         ListView1.BeginUpdate()
  388.  
  389.         Try
  390.             Using fs As New FileStream(pathFile, FileMode.Open)
  391.                 Using reader As StreamReader = New StreamReader(fs)
  392.                     Dim line As String
  393.                     Dim field As String()
  394.                     Dim lvItem As ListViewItem
  395.                     Dim fullName As String
  396.  
  397.                     While Not reader.EndOfStream
  398.                         line = reader.ReadLine
  399.                         field = line.Split(","c)
  400.                         fullName = field(0)
  401.                         If field(1).Trim <> String.Empty Then
  402.                             fullName &= ", " & field(1)
  403.                         End If
  404.                         lvItem = ListView1.Items.Add(fullName)
  405.                         With lvItem.SubItems
  406.                             .Add(field(2))
  407.                             .Add(field(3))
  408.                             .Add(field(4))
  409.                             .Add(field(5))
  410.                             .Add(field(6))
  411.                             .Add(field(7))
  412.                         End With
  413.                     End While
  414.                 End Using
  415.             End Using
  416.         Catch ex As Exception
  417.         End Try
  418.  
  419.         ListView1.EndUpdate()
  420.     End Sub
  421.  
  422.     ' Save to File ListView Items.
  423.     Private Sub SaveButton_Click(sender As Object, e As EventArgs) _
  424.         Handles SaveButton.Click
  425.  
  426.         Dim itemsCount As Integer = ListView1.Items.Count
  427.         If itemsCount = 0 Then Exit Sub
  428.  
  429.         Try
  430.             Using fs As New FileStream(pathFile, FileMode.Create)
  431.                 Using writer As StreamWriter = New StreamWriter(fs, Encoding.Default)
  432.                     For Each lvItem As ListViewItem In ListView1.Items
  433.                         Dim field As String() = lvItem.Text.Split(","c)
  434.                         writer.Write(field(0) & ",")
  435.                         If field.Length > 1 Then
  436.                             writer.Write(field(1).Trim & ",")
  437.                         Else
  438.                             writer.Write(",")
  439.                         End If
  440.                         With lvItem
  441.                             writer.Write(.SubItems(1).Text & ",")
  442.                             writer.Write(.SubItems(2).Text & ",")
  443.                             writer.Write(.SubItems(3).Text & ",")
  444.                             writer.Write(.SubItems(4).Text & ",")
  445.                             writer.Write(.SubItems(5).Text & ",")
  446.                             writer.Write(.SubItems(6).Text & ControlChars.CrLf)
  447.                         End With
  448.                     Next
  449.                 End Using
  450.             End Using
  451.         Catch ex As Exception
  452.         End Try
  453.     End Sub
  454.  
  455. #End Region
  456.  
  457. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement