vb中防止将重复项目添加到列
框控件中(VB prevents repeated items from being added to the ListBox control)
vb中防止将重复项目添加到列表框控件中(VB prevents repeated
items from being added to the ListBox control)
In csdn_VB, prevent repeated items from being added to list box controls. VB prevents repeated items from being added to the ListBox control
Author: Tan Weng
-- the ListBox control in Visual Basic contains a list of items. This article describes how to check items in a list
No longer exists, and how to add new items to the listbox control.
Search for duplicate items using the SendMessage function
When developing applications in Visual Basic, you can use list box controls to create a list of projects.
To add a new project to the list, you can use the AddItem method, which cannot automatically report in the list box control
Is there any duplicate information in the component, so you must first check it before adding the new project to the list.
You can use the SendMessage function of the Windows application programming interface (API) to control the list box
Searches for the specified item that allows messages to be sent to the operating system. In the example of this article, we
Let the SendMessage function execute a LB_FINDSTRING message in the listbox control.
- -- LB_FINDSTRING messages allow you to search for a project that matches the target string in a list box control. this
The first parameter of the message is the type of search that you want to have, and you want to set the value to 0, which represents the first from the list box control
Two projects start searching. The second parameter is a string that ends with NULL, which is the actual item you want to search for.
If the LB_FINDSTRING message returns a value of -1, the target string is not found in the listbox control,
At this point, you can use the AddItem method to add new items to the listbox control. If the item is already in the list
In existence, you can simply display a message box or perform some other process to notify the user of a repeating item
The object already exists in the listbox control.
Sample program
The program shows how to make sure that a list box contains one that is added to the control
Projects.
- - 1. start a new project in Visual Basic, and establish the Form1 by default.
- 2. add the following constants and declarations to the general declaration section of Form1. (note that the declaration statement needs to be taken
Write in a line:
---- Private, Declare, Function, SendMessageFind, Lib, user32, Alias
"SendMessageA" (ByVal, hWnd, As, Long, ByVal, wMsg, As, Long, ByVal, wParam, As)
Integer, ByVal, lParam, As, String) As Long
---- Const WM_USER = &H400
---- Const, LB_ERR = (-1)
---- Const LB_FINDSTRING = &H18F
3. add the following code to the Form_Load event in Form1: