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 © 2014. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Program Name: ListBox Demo
- ' Author: Joseph L. Bolen
- ' Date Created: Aug 2014
- '
- ' Description: The program shows various ways to populate a ListBox's item collection.
- '
- ' Documentation is at:
- ' App's screen capture image is at http://pastebin.com/DDRUaggb
- ' App's Visual Basic .NET code is at http://imgur.com/r94Mi3O
- ' App's video tutorial is at http://www.youtube.com/user/bolenpresents
- '
- ' See Also, MSDN's article "Implementing Drag and Drop in Visual Basic .NET"
- ' at http://msdn.microsoft.com/en-us/library/aa289508(v=vs.71).aspx .
- '
- ' Control properties set at design time:
- ' MyListBox.AllowDrop = True
- ' MyListBox.HorizontalScrollbar = True
- ' MyListBox.SelectionMode = SelectionMode.One
- ' ItemTextBox.AllowDrop = True
- ' ResetButton.Enabled = False
- '-------------------------------------------------------------------------------------------
- Option Strict On
- Imports System
- Imports System.IO
- Public Class MainForm
- #Region " Declare Class level variables."
- Const namesFile As String = "Names.txt"
- Private MouseIsDown As Boolean = False
- #End Region
- ' Sort the ListBox.
- Private Sub SortedCheckBox_CheckedChanged(sender As Object, e As EventArgs) _
- Handles SortedCheckBox.CheckedChanged
- ' Toggle ListBox's Sorted property.
- MyListBox.Sorted = SortedCheckBox.Checked
- End Sub
- ' Clear the ListBox and TextBox
- Private Sub ClearButton_Click(sender As Object, e As EventArgs) _
- Handles ClearButton.Click
- 'Clear the ListBox and unselect all items.
- MyListBox.Items.Clear()
- MyListBox.ClearSelected()
- 'Clear the TextBox
- ItemTextBox.Clear()
- End Sub
- ' Loading the ListBox with names using the Items.Add method.
- Private Sub LoadByAddButton_Click(sender As Object, e As EventArgs) _
- Handles LoadByAddButton.Click
- 'Clear the ListBox.
- MyListBox.Items.Clear()
- 'Prevent ListBox from painting until all items are added.
- MyListBox.BeginUpdate()
- ' Loading the ListBox with names using the Items.Add method.
- MyListBox.Items.Add("Joe")
- MyListBox.Items.Add("Sally")
- MyListBox.Items.Add("Jim")
- MyListBox.Items.Add("George")
- MyListBox.Items.Add("Susan")
- MyListBox.Items.Add("Linda")
- MyListBox.Items.Add("Peter")
- ' Resume painting the ListBox after items are loaded.
- MyListBox.EndUpdate()
- 'unselect all items.
- MyListBox.ClearSelected()
- End Sub
- ' Loading the ListBox with names using the Items.AddRange method.
- Private Sub LoadByAddRangeButton_Click(sender As Object, e As EventArgs) _
- Handles LoadByAddRangeButton.Click
- 'Clear the ListBox.
- MyListBox.Items.Clear()
- ' Loading the ListBox with valuse using Items.AddRange
- MyListBox.Items.AddRange(New Object() {"Joe", "Sally", "Jim", "George", "Susan", "Linda", "Peter"})
- 'MyListBox.Items.AddRange(New Object() {34, 0.128, 578.25, 19.53, 6342, -26, 17})
- 'OR ...
- ''Loading the ListBox with string values from an array using Items.AddRange
- 'Dim myStringArray As String() = {"Joe", "Sally", "Jim", "George", "Susan", "Linda", "Peter"}
- 'MyListBox.Items.AddRange(myStringArray)
- 'Unselect all items.
- MyListBox.ClearSelected()
- End Sub
- ' Loading the ListBox with names using the DataSource property.
- Private Sub LoadByDataSourceButton_Click(sender As Object, e As EventArgs) _
- Handles LoadByDataSourceButton.Click
- 'Clear the ListBox.
- MyListBox.Items.Clear()
- 'Turn off selection mode until after the load to prevent SelectedIndexChanged event.
- MyListBox.SelectionMode = SelectionMode.None
- Dim myStringArray As String() = {"Joe", "Sally", "Jim", "George", "Susan", "Linda", "Peter"}
- ' Loading the ListBox with names using the DataSource property.
- MyListBox.DataSource = myStringArray
- ' Set when using a DataSource
- SortedCheckBox.Visible = False
- MyListBox.AllowDrop = False
- ItemTextBox.AllowDrop = False
- ItemTextBox.ReadOnly = True
- AddFromTextBoxButton.Enabled = False
- ResetButton.Enabled = True
- LoadByDataSourceButton.Enabled = False
- 'Turn on selection mode to allow SelectionIndexChanged event.
- MyListBox.SelectionMode = SelectionMode.One
- 'Unselect all items.
- MyListBox.ClearSelected()
- End Sub
- ' Load the ListBox with names using code from a file.
- Private Sub Button1_Click(sender As Object, e As EventArgs) _
- Handles Button1.Click
- ' Add above Public Class ...
- ' Imports System
- ' Imports System.IO
- 'Clear the ListBox
- MyListBox.Items.Clear()
- Try
- Using sr As New StreamReader(namesFile)
- Dim line As String
- MyListBox.BeginUpdate() 'Prevent ListBox from painting until all items are added.
- Do While Not sr.EndOfStream
- line = sr.ReadLine
- MyListBox.Items.Add(line)
- Loop
- MyListBox.EndUpdate() ' Resume painting the ListBox after items are loaded.
- End Using
- Catch ex As Exception
- MessageBox.Show("The file '" & namesFile & "' could not be read.",
- "File Input Problem",
- MessageBoxButtons.OK,
- MessageBoxIcon.Error)
- End Try
- 'Unselect all items.
- MyListBox.ClearSelected()
- End Sub
- 'Load a ListBox item from the TextBox.
- Private Sub AddButton_Click(sender As Object, e As EventArgs) _
- Handles AddFromTextBoxButton.Click
- If Not String.IsNullOrWhiteSpace(ItemTextBox.Text) Then
- ' Prevent duplicates.
- If Not MyListBox.Items.Contains(ItemTextBox.Text) Then
- MyListBox.Items.Add(ItemTextBox.Text)
- ' Clear the ItemTextBox.
- ItemTextBox.Clear()
- End If
- End If
- End Sub
- ' User has selected an item from the ListBox.
- Private Sub MyListBox_SelectedIndexChanged(sender As Object, e As EventArgs) _
- Handles MyListBox.SelectedIndexChanged
- ' Display a MessageBox of the item selected.
- If MyListBox.SelectedItem IsNot Nothing Then
- MessageBox.Show("You slected: " & MyListBox.SelectedItem.ToString,
- "Your Selection",
- MessageBoxButtons.OK,
- MessageBoxIcon.Information)
- End If
- End Sub
- ' Reset for this demo after using DataSource to load ListBox.
- Private Sub ResetButton_Click(sender As Object, e As EventArgs) _
- Handles ResetButton.Click
- SortedCheckBox.Visible = True
- SortedCheckBox.Checked = False
- MyListBox.DataSource = Nothing
- MyListBox.Sorted = False
- MyListBox.AllowDrop = True
- ItemTextBox.AllowDrop = True
- ItemTextBox.ReadOnly = False
- AddFromTextBoxButton.Enabled = True
- ResetButton.Enabled = False
- LoadByDataSourceButton.Enabled = True
- 'Unselect all items.
- MyListBox.ClearSelected()
- End Sub
- #Region " Drag and Drop"
- ' Mouse button has been pressed for TextBox.
- Private Sub ItemTextBox_MouseDown(sender As Object, e As MouseEventArgs) _
- Handles ItemTextBox.MouseDown
- ' Set a flag to show that the mouse is down.
- MouseIsDown = True
- End Sub
- ' Mouse is dragging from TextBox.
- Private Sub ItemTextBox_MouseMove(sender As Object, e As MouseEventArgs) _
- Handles ItemTextBox.MouseMove
- If MouseIsDown AndAlso (Not String.IsNullOrWhiteSpace(ItemTextBox.Text)) Then
- ' Initiate dragging.
- ItemTextBox.DoDragDrop(ItemTextBox.Text, DragDropEffects.Move)
- End If
- MouseIsDown = False
- End Sub
- ' Mouse is dragging contents into the ListBox.
- Private Sub MyListBox_DragEnter(sender As Object, e As DragEventArgs) _
- Handles MyListBox.DragEnter
- ' Check the format of the data being dropped.
- If (e.Data.GetDataPresent(DataFormats.Text)) Then
- ' Display the copy cursor.
- e.Effect = DragDropEffects.Move
- Else
- ' Display the no-drop cursor.
- e.Effect = DragDropEffects.None
- End If
- End Sub
- ' Mouse button has been release in the ListBox.
- Private Sub MyListBox_DragDrop(sender As Object, e As DragEventArgs) _
- Handles MyListBox.DragDrop
- ' Add the text the the ListBox
- Dim aString As String = CStr(e.Data.GetData(DataFormats.Text))
- If Not MyListBox.Items.Contains(aString) Then
- MyListBox.Items.Add(aString)
- ' Clear the TextBox's text property.
- ItemTextBox.Clear()
- End If
- End Sub
- ' Mouse button has been pressed for ListBox.
- Private Sub MyListBox_MouseDown(sender As Object, e As MouseEventArgs) _
- Handles MyListBox.MouseDown
- ' Set a flag to show that the mouse is down.
- MouseIsDown = True
- End Sub
- ' Mouse is dragging from ListBox.
- Private Sub MyListBox_MouseMove(sender As Object, e As MouseEventArgs) _
- Handles MyListBox.MouseMove
- If MouseIsDown AndAlso (Not IsNothing(MyListBox.SelectedItem)) Then
- ' Initiate dragging.
- MyListBox.DoDragDrop(MyListBox.Text, DragDropEffects.Move)
- End If
- MouseIsDown = False
- End Sub
- ' Mouse is dragging contents into the TextBox.
- Private Sub ItemTextBox_DragEnter(sender As Object, e As DragEventArgs) _
- Handles ItemTextBox.DragEnter
- ' Check the format of the data being dropped.
- If (e.Data.GetDataPresent(DataFormats.Text)) Then
- ' Display the copy cursor.
- e.Effect = DragDropEffects.Move
- Else
- ' Display the no-drop cursor.
- e.Effect = DragDropEffects.None
- End If
- End Sub
- ' Mouse button has been release in the TextBox.
- Private Sub ItemTextBox_DragDrop(sender As Object, e As DragEventArgs) _
- Handles ItemTextBox.DragDrop
- ' Add the text the the ListBox
- Dim aString As String = CStr(e.Data.GetData(DataFormats.Text))
- ItemTextBox.Text = aString
- ' Remove item from the ListBox.
- MyListBox.Items.Remove(aString)
- End Sub
- #End Region
- End Class
- '=====================================================================
- ' Names.txt file (Build Action = Content, Copy to ... = Copy if newer
- '=====================================================================
- Joe
- Sally
- Jim
- George
- Susan
- Linda
- Peter
Advertisement
Add Comment
Please, Sign In to add comment