Searches a string and selects the exactly matching item.
C
LONG WINAPI SftBox_SelectStringExact(HWND hwndCtl, LONG start, int col, LPCTSTR lpszString, BOOL fWrap); LONG WINAPI SftBox_SelectStringExactCase(HWND hwndCtl, LONG start, int col, LPCTSTR lpszString, BOOL fWrap);
C++
LONG CSftBox::SelectStringExact(LONG start, int col, LPCTSTR lpszString, BOOL fWrap); LONG CSftBox::SelectStringExactCase(LONG start, int col, LPCTSTR lpszString, BOOL fWrap);
hwndCtl
The window handle of the combo box control.
start
The zero-based index of the item preceding the first item to be searched. The search starts with the item following start. Specify -1 to search all items, starting with the first item.
col
The zero-based column number to be searched. Specify -1 to search all columns.
lpszString
The string to be searched.
fWrap
Set to TRUE to continue the search with the first item once the last item has been reached. The search then ends with the item start. Set to FALSE to end the search with the last item, so no item is searched if start is the last item. This parameter is ignored if start 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 SelectStringExact and SelectStringExactCase functions search a string and select the exactly matching item.
The string lpszString is compared to the cell text (see GetText) of each item, starting with the item following start. 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, it becomes the selected item (see SetCurSel), it is made visible in the dropdown portion (see MakeRowVisible) and its zero-based index is returned. If no matching item is found, the combo box no longer has a selected item and -1 is returned.
If the cell text is identical to the string in lpszString, it is considered a match. When using SelectStringExact, the comparison of lpszString and the cell text is not case sensitive. SelectStringExactCase performs a case sensitive comparison. The strings are compared using the locale defined using SetLocale. To select an item whose text starts with lpszString use SelectString or SelectStringCase.
If the fSelectEnabledItemsOnly member of the SFTBOX_CONTROL structure is set to TRUE (see SetControlInfo), disabled items are not searched.
FindStringExact can be used to search for an item without selecting it.
The following table shows examples using SelectStringExact:
| lpszString | Cell Text | Match | Comment |
|---|---|---|---|
| ABC | abc | Yes | Same string, case is ignored (no match when using SelectStringExactCase) |
| 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. No item is found, the combo box no longer has a selected item and -1 is returned.
INT_PTR CALLBACK MainDialogProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_INITDIALOG:
SendMessage(hwndDlg, WM_SETICON, ICON_BIG, (LPARAM)LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_APP)));
InitStates(GetDlgItem(hwndDlg, IDC_STATE), COL_NAME);
InitStates(GetDlgItem(hwndDlg, IDC_ABBR), COL_ABBR);
InitCities(GetDlgItem(hwndDlg, IDC_CITY));
SftBox_SelectStringExact(GetDlgItem(hwndDlg, IDC_STATE), -1, COL_NAME, TEXT("Florida"), FALSE);
SftBox_SelectStringExact(GetDlgItem(hwndDlg, IDC_ABBR), -1, COL_ABBR, TEXT("FL"), FALSE);
SftBox_SetCurSel(GetDlgItem(hwndDlg, IDC_CITY), 0);
ShowStatus(hwndDlg, GetDlgItem(hwndDlg, IDC_STATE));
SftDarkMode_ApplyToDialog(hwndDlg);
return TRUE;
case WM_COMMAND:
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
InitStates(m_State, COL_NAME);
InitStates(m_Abbr, COL_ABBR);
InitCities();
m_State.SelectStringExact(-1, COL_NAME, _T("Florida"), FALSE);
m_Abbr.SelectStringExact(-1, COL_ABBR, _T("FL"), FALSE);
m_City.SetCurSel(0);
ShowStatus(m_State);
// Dark mode
SftDarkMode_ApplyToDialog(m_hWnd);
See Also C/C++ API | Categories | Notifications