Searches a string (exact match).
C
LONG WINAPI SftBox_FindStringExact(HWND hwndCtl, LONG index, int col, LPCTSTR lpszText, BOOL fWrap); LONG WINAPI SftBox_FindStringExactCase(HWND hwndCtl, LONG index, int col, LPCTSTR lpszText, BOOL fWrap);
C++
LONG CSftBox::FindStringExact(LONG index, int col, LPCTSTR lpszText, BOOL fWrap) const; LONG CSftBox::FindStringExactCase(LONG index, int col, LPCTSTR lpszText, BOOL fWrap) const;
hwndCtl
The window handle of the combo box control.
index
The zero-based index of the item after which the search begins (the item itself is not searched first). If -1 is specified, the search starts at the first item (index 0).
col
The zero-based column number to be searched. If -1 is specified, all columns are searched.
lpszText
The string to be searched.
fWrap
Set to TRUE to continue the search at the first item once the last item has been searched, ending with item index. If FALSE is specified, the search ends at the last item, so no item is searched if index is the last item. This parameter is ignored if index is -1.
The return value is the zero-based index of the item where the string was found. -1 is returned if the string was not found or an error occurred.
The FindStringExact and FindStringExactCase functions search a string.
The string lpszText is compared to the cell text (see GetText) of each item following the item described by index. The search is restricted to the column specified by col (all columns if col is -1). If an item with exactly matching text is found, its zero-based index is returned, otherwise -1 is returned.
If the cell text is identical to the string in lpszText, it is considered a match. To find cell text that starts with lpszText use FindString.
Using FindStringExact, the comparison of lpszText and the cell text is not case sensitive. Using FindStringExactCase, the comparison is case sensitive. The strings are compared using the locale defined using SetLocale.
If only enabled items can be selected (see SFTBOX_CONTROL, fSelectEnabledItemsOnly), disabled items are not searched.
The FindStringEx function can also be used and allows additional search options. SelectStringExact can be used to search and select an item.
The following examples use FindStringExact:
| lpszText | Cell Text | Match | Comment |
|---|---|---|---|
| ABC | abc | Yes | Same string, case is ignored |
| abc | abc123 | No | Not the same string |
| abc | Thisabc | No | Not the same string |
| abc | ab | No | Not the same string |
In a combo box control using a virtual data source, these functions cannot be used and -1 is returned.
HWND hwndBox = GetDlgItem(hwndDlg, IDC_CITY);
TCHAR szCity[80];
LONG index;
GetWindowText(hwndBox, szCity, sizeof(szCity) / sizeof(szCity[0]));
if (!szCity[0])
return;
index = SftBox_FindStringExact(hwndBox, -1, 0, szCity, FALSE);
if (index > 0)
index = SftBox_MoveItem(hwndBox, index, 0);
else if (index < 0) {
index = SftBox_InsertString(hwndBox, 0, szCity);
if (SftBox_GetCount(hwndBox) > MAX_HISTORY)
SftBox_DeleteString(hwndBox, SftBox_GetCount(hwndBox) - 1);
}
// Add the text typed to the top of the list, unless it is already there
CString city;
LONG index;
m_City.GetWindowText(city);
if (city.IsEmpty())
return;
index = m_City.FindStringExact(-1, 0, city, FALSE);
if (index > 0)
index = m_City.MoveItem(index, 0);
else if (index < 0) {
index = m_City.InsertString(0, city);
if (m_City.GetCount() > MAX_HISTORY)
m_City.DeleteString(m_City.GetCount() - 1);
}See Also C/C++ API | Categories | Notifications