JayBeeOH

ListBox Demo

Aug 9th, 2014
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 11.77 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 © 2014. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   ListBox Demo
  13. ' Author:         Joseph L. Bolen
  14. ' Date Created:   Aug 2014
  15. '
  16. ' Description:    The program shows various ways to populate a ListBox's item collection.
  17. '
  18. '                 Documentation is at:
  19. '                   App's screen capture image is at http://pastebin.com/DDRUaggb
  20. '                   App's Visual Basic .NET code is at http://imgur.com/r94Mi3O
  21. '                   App's video tutorial is at http://www.youtube.com/user/bolenpresents
  22. '
  23. '                 See Also, MSDN's article "Implementing Drag and Drop in Visual Basic .NET"
  24. '                   at http://msdn.microsoft.com/en-us/library/aa289508(v=vs.71).aspx .
  25. '
  26. '                 Control properties set at design time:
  27. '                   MyListBox.AllowDrop = True
  28. '                   MyListBox.HorizontalScrollbar = True
  29. '                   MyListBox.SelectionMode = SelectionMode.One
  30. '                   ItemTextBox.AllowDrop = True
  31. '                   ResetButton.Enabled = False
  32. '-------------------------------------------------------------------------------------------
  33. Option Strict On
  34.  
  35. Imports System
  36. Imports System.IO
  37.  
  38. Public Class MainForm
  39.  
  40. #Region "  Declare Class level variables."
  41.  
  42.     Const namesFile As String = "Names.txt"
  43.  
  44.     Private MouseIsDown As Boolean = False
  45.  
  46. #End Region
  47.  
  48.     ' Sort the ListBox.
  49.     Private Sub SortedCheckBox_CheckedChanged(sender As Object, e As EventArgs) _
  50.         Handles SortedCheckBox.CheckedChanged
  51.  
  52.         ' Toggle ListBox's Sorted property.
  53.         MyListBox.Sorted = SortedCheckBox.Checked
  54.     End Sub
  55.  
  56.     ' Clear the ListBox and TextBox
  57.     Private Sub ClearButton_Click(sender As Object, e As EventArgs) _
  58.         Handles ClearButton.Click
  59.  
  60.         'Clear the ListBox and unselect all items.
  61.         MyListBox.Items.Clear()
  62.         MyListBox.ClearSelected()
  63.  
  64.         'Clear the TextBox
  65.         ItemTextBox.Clear()
  66.     End Sub
  67.  
  68.     ' Loading the ListBox with names using the Items.Add method.
  69.     Private Sub LoadByAddButton_Click(sender As Object, e As EventArgs) _
  70.         Handles LoadByAddButton.Click
  71.  
  72.         'Clear the ListBox.
  73.         MyListBox.Items.Clear()
  74.  
  75.         'Prevent ListBox from painting until all items are added.
  76.         MyListBox.BeginUpdate()
  77.  
  78.         ' Loading the ListBox with names using the Items.Add method.
  79.         MyListBox.Items.Add("Joe")
  80.         MyListBox.Items.Add("Sally")
  81.         MyListBox.Items.Add("Jim")
  82.         MyListBox.Items.Add("George")
  83.         MyListBox.Items.Add("Susan")
  84.         MyListBox.Items.Add("Linda")
  85.         MyListBox.Items.Add("Peter")
  86.  
  87.         ' Resume painting the ListBox after items are loaded.
  88.         MyListBox.EndUpdate()
  89.  
  90.         'unselect all items.
  91.         MyListBox.ClearSelected()
  92.     End Sub
  93.  
  94.     ' Loading the ListBox with names using the Items.AddRange method.
  95.     Private Sub LoadByAddRangeButton_Click(sender As Object, e As EventArgs) _
  96.         Handles LoadByAddRangeButton.Click
  97.  
  98.         'Clear the ListBox.
  99.         MyListBox.Items.Clear()
  100.  
  101.         ' Loading the ListBox with valuse using Items.AddRange
  102.         MyListBox.Items.AddRange(New Object() {"Joe", "Sally", "Jim", "George", "Susan", "Linda", "Peter"})
  103.  
  104.         'MyListBox.Items.AddRange(New Object() {34, 0.128, 578.25, 19.53, 6342, -26, 17})
  105.  
  106.         'OR ...
  107.  
  108.         ''Loading the ListBox with string values from an array using Items.AddRange
  109.         'Dim myStringArray As String() = {"Joe", "Sally", "Jim", "George", "Susan", "Linda", "Peter"}
  110.         'MyListBox.Items.AddRange(myStringArray)
  111.  
  112.         'Unselect all items.
  113.         MyListBox.ClearSelected()
  114.     End Sub
  115.  
  116.     ' Loading the ListBox with names using the DataSource property.
  117.     Private Sub LoadByDataSourceButton_Click(sender As Object, e As EventArgs) _
  118.         Handles LoadByDataSourceButton.Click
  119.  
  120.         'Clear the ListBox.
  121.         MyListBox.Items.Clear()
  122.  
  123.         'Turn off selection mode until after the load to prevent SelectedIndexChanged event.
  124.         MyListBox.SelectionMode = SelectionMode.None
  125.  
  126.         Dim myStringArray As String() = {"Joe", "Sally", "Jim", "George", "Susan", "Linda", "Peter"}
  127.  
  128.         ' Loading the ListBox with names using the DataSource property.
  129.  
  130.         MyListBox.DataSource = myStringArray
  131.  
  132.         ' Set when using a DataSource
  133.         SortedCheckBox.Visible = False
  134.         MyListBox.AllowDrop = False
  135.         ItemTextBox.AllowDrop = False
  136.         ItemTextBox.ReadOnly = True
  137.         AddFromTextBoxButton.Enabled = False
  138.         ResetButton.Enabled = True
  139.         LoadByDataSourceButton.Enabled = False
  140.  
  141.  
  142.         'Turn on selection mode to allow SelectionIndexChanged event.
  143.         MyListBox.SelectionMode = SelectionMode.One
  144.  
  145.         'Unselect all items.
  146.         MyListBox.ClearSelected()
  147.     End Sub
  148.  
  149.     ' Load the ListBox with names using code from a file.
  150.     Private Sub Button1_Click(sender As Object, e As EventArgs) _
  151.         Handles Button1.Click
  152.  
  153.         ' Add above Public Class ...
  154.         '   Imports System
  155.         '   Imports System.IO
  156.  
  157.         'Clear the ListBox
  158.         MyListBox.Items.Clear()
  159.  
  160.         Try
  161.             Using sr As New StreamReader(namesFile)
  162.                 Dim line As String
  163.                 MyListBox.BeginUpdate() 'Prevent ListBox from painting until all items are added.
  164.                 Do While Not sr.EndOfStream
  165.                     line = sr.ReadLine
  166.                     MyListBox.Items.Add(line)
  167.                 Loop
  168.                 MyListBox.EndUpdate()   ' Resume painting the ListBox after items are loaded.
  169.             End Using
  170.         Catch ex As Exception
  171.             MessageBox.Show("The file '" & namesFile & "' could not be read.",
  172.                             "File Input Problem",
  173.                             MessageBoxButtons.OK,
  174.                             MessageBoxIcon.Error)
  175.         End Try
  176.  
  177.         'Unselect all items.
  178.         MyListBox.ClearSelected()
  179.     End Sub
  180.  
  181.     'Load a ListBox item from the TextBox.
  182.     Private Sub AddButton_Click(sender As Object, e As EventArgs) _
  183.         Handles AddFromTextBoxButton.Click
  184.  
  185.         If Not String.IsNullOrWhiteSpace(ItemTextBox.Text) Then
  186.             ' Prevent duplicates.
  187.             If Not MyListBox.Items.Contains(ItemTextBox.Text) Then
  188.                 MyListBox.Items.Add(ItemTextBox.Text)
  189.                 ' Clear the ItemTextBox.
  190.                 ItemTextBox.Clear()
  191.             End If
  192.         End If
  193.     End Sub
  194.  
  195.     ' User has selected an item from the ListBox.
  196.     Private Sub MyListBox_SelectedIndexChanged(sender As Object, e As EventArgs) _
  197.         Handles MyListBox.SelectedIndexChanged
  198.  
  199.         ' Display a MessageBox of the item selected.
  200.         If MyListBox.SelectedItem IsNot Nothing Then
  201.             MessageBox.Show("You slected: " & MyListBox.SelectedItem.ToString,
  202.                             "Your Selection",
  203.                             MessageBoxButtons.OK,
  204.                             MessageBoxIcon.Information)
  205.         End If
  206.     End Sub
  207.  
  208.     ' Reset for this demo after using DataSource to load ListBox.
  209.     Private Sub ResetButton_Click(sender As Object, e As EventArgs) _
  210.         Handles ResetButton.Click
  211.  
  212.         SortedCheckBox.Visible = True
  213.         SortedCheckBox.Checked = False
  214.         MyListBox.DataSource = Nothing
  215.         MyListBox.Sorted = False
  216.         MyListBox.AllowDrop = True
  217.         ItemTextBox.AllowDrop = True
  218.         ItemTextBox.ReadOnly = False
  219.         AddFromTextBoxButton.Enabled = True
  220.         ResetButton.Enabled = False
  221.         LoadByDataSourceButton.Enabled = True
  222.  
  223.         'Unselect all items.
  224.         MyListBox.ClearSelected()
  225.     End Sub
  226.  
  227. #Region "  Drag and Drop"
  228.  
  229.     ' Mouse button has been pressed for TextBox.
  230.     Private Sub ItemTextBox_MouseDown(sender As Object, e As MouseEventArgs) _
  231.         Handles ItemTextBox.MouseDown
  232.  
  233.         ' Set a flag to show that the mouse is down.
  234.         MouseIsDown = True
  235.     End Sub
  236.  
  237.     ' Mouse is dragging from TextBox.
  238.     Private Sub ItemTextBox_MouseMove(sender As Object, e As MouseEventArgs) _
  239.         Handles ItemTextBox.MouseMove
  240.  
  241.         If MouseIsDown AndAlso (Not String.IsNullOrWhiteSpace(ItemTextBox.Text)) Then
  242.             ' Initiate dragging.
  243.             ItemTextBox.DoDragDrop(ItemTextBox.Text, DragDropEffects.Move)
  244.         End If
  245.         MouseIsDown = False
  246.     End Sub
  247.  
  248.     ' Mouse is dragging contents into the ListBox.
  249.     Private Sub MyListBox_DragEnter(sender As Object, e As DragEventArgs) _
  250.         Handles MyListBox.DragEnter
  251.  
  252.         ' Check the format of the data being dropped.
  253.         If (e.Data.GetDataPresent(DataFormats.Text)) Then
  254.             ' Display the copy cursor.
  255.             e.Effect = DragDropEffects.Move
  256.         Else
  257.             ' Display the no-drop cursor.
  258.             e.Effect = DragDropEffects.None
  259.         End If
  260.     End Sub
  261.  
  262.     ' Mouse button has been release in the ListBox.
  263.     Private Sub MyListBox_DragDrop(sender As Object, e As DragEventArgs) _
  264.         Handles MyListBox.DragDrop
  265.  
  266.         ' Add the text the the ListBox
  267.         Dim aString As String = CStr(e.Data.GetData(DataFormats.Text))
  268.         If Not MyListBox.Items.Contains(aString) Then
  269.             MyListBox.Items.Add(aString)
  270.  
  271.             ' Clear the TextBox's text property.
  272.             ItemTextBox.Clear()
  273.         End If
  274.     End Sub
  275.  
  276.     ' Mouse button has been pressed for ListBox.
  277.     Private Sub MyListBox_MouseDown(sender As Object, e As MouseEventArgs) _
  278.         Handles MyListBox.MouseDown
  279.  
  280.         ' Set a flag to show that the mouse is down.
  281.         MouseIsDown = True
  282.     End Sub
  283.  
  284.     ' Mouse is dragging from ListBox.
  285.     Private Sub MyListBox_MouseMove(sender As Object, e As MouseEventArgs) _
  286.         Handles MyListBox.MouseMove
  287.  
  288.         If MouseIsDown AndAlso (Not IsNothing(MyListBox.SelectedItem)) Then
  289.             ' Initiate dragging.
  290.             MyListBox.DoDragDrop(MyListBox.Text, DragDropEffects.Move)
  291.         End If
  292.         MouseIsDown = False
  293.     End Sub
  294.  
  295.     ' Mouse is dragging contents into the TextBox.
  296.     Private Sub ItemTextBox_DragEnter(sender As Object, e As DragEventArgs) _
  297.         Handles ItemTextBox.DragEnter
  298.  
  299.         ' Check the format of the data being dropped.
  300.         If (e.Data.GetDataPresent(DataFormats.Text)) Then
  301.             ' Display the copy cursor.
  302.             e.Effect = DragDropEffects.Move
  303.         Else
  304.             ' Display the no-drop cursor.
  305.             e.Effect = DragDropEffects.None
  306.         End If
  307.     End Sub
  308.  
  309.     ' Mouse button has been release in the TextBox.
  310.     Private Sub ItemTextBox_DragDrop(sender As Object, e As DragEventArgs) _
  311.         Handles ItemTextBox.DragDrop
  312.  
  313.         ' Add the text the the ListBox
  314.         Dim aString As String = CStr(e.Data.GetData(DataFormats.Text))
  315.  
  316.         ItemTextBox.Text = aString
  317.  
  318.         ' Remove item from the ListBox.
  319.         MyListBox.Items.Remove(aString)
  320.     End Sub
  321.  
  322. #End Region
  323.  
  324. End Class
  325.  
  326. '=====================================================================
  327. ' Names.txt file (Build Action = Content, Copy to ... = Copy if newer
  328. '=====================================================================
  329. Joe
  330. Sally
  331. Jim
  332. George
  333. Susan
  334. Linda
  335. Peter
Advertisement
Add Comment
Please, Sign In to add comment