Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use ListBox Control in VB.NET

Shulou Source: shulou.com Published: 2022-06-02 08:57:27 09月16日 Update

This article is about how to use ListBox controls in VB.NET. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Drag and drop in windows is usually a way to copy or move files, which is fully supported by windows and is the preferred way for many users to manipulate files. In addition, users are used to dragging files to a program to open them, like dragging a doc file to word to open them.

In this example, the VB.NET ListBox control is manipulated with files dragged from windows Explorer. Add a VB.NET ListBox control to the form, set its AllowDrop property to True, and add the following code:

Private Sub ListBox1_DragEnter (ByVal sender As Object, ByVal e As _ System.Windows.Forms.DragEventArgs) Handles ListBox1.DragEnter If e.Data.GetDataPresent (DataFormats.FileDrop) Then e.Effect = DragDropEffects.All End If End Sub Private Sub ListBox1_DragDrop (ByVal sender As Object, ByVal e As _ System.Windows.Forms.DragEventArgs) Handles ListBox1.DragDrop If e.Data.GetDataPresent (DataFormats.FileDrop) Then Dim MyFiles () As String Dim i As Integer 'Assign the files to an array. MyFiles = e.Data.GetData (DataFormats.FileDrop) 'Loop through the array and add the files to the list. For I = 0 To MyFiles.Length-1 ListBox1.Items.Add (MyFiles (I)) Next End If End Sub

You may have noticed that the Effect property in the DragEnter event is set to DragDropEffects.All. Because the file itself is not actually copied or moved, it doesn't matter which AllowedEffects the source control is set to, so specifying All is fine for any FileDrop.

In the above example, the FileDrop format contains the full path of each dragged file.

Thank you for reading! This is the end of this article on "how to use ListBox controls in VB.NET". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Files controls examples content properties methods more users articles mobile good practical useful events code functions articles formats look knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Linux macOS Shulou Tech Info vpn