HeaderPane
Main
Hide
Share Link
Print

DataInput Sample (C)

This sample illustrates dropdown combo boxes used for data entry. Two combo boxes show the same three column list (state, abbreviation, capital), one using the first column and one using the second column as edit and search column (SetEditSearchColumn), with autocompletion as text is typed (SFTBOX_SEARCH_AUTOCOMPLETE), column headers shown as buttons in the dropdown portion, a dropdown portion the user can resize with the sizing box (SetDisableNoScroll, SetShowResizer), sort indicators and sorting by the clicked column using SortItems. A third combo box keeps a list of the cities entered, most recent first, using FindStringExact, MoveItem, InsertString and DeleteString.

The source code is located at C:\Program Files (x86)\Softelvdm\SftBox DLL 6.0\Samples\C\DataInput\DataInput.c.

/****************************************************************************/
/* SftBox/DLL 6.0 - ComboBox Control for Windows                            */
/* Copyright (C) 1997, 2026  Softel vdm, Inc. All Rights Reserved.          */
/****************************************************************************/
/* SftBox/DLL 6.0 sample. Guide: https://softelvdm.com/Vault/Softelvdm.com/llms/SftBox-DLL-6.0.txt, API reference: https://softelvdm.com/Vault/Softelvdm.com/llms/SftBox-DLL-6.0-reference.txt - see AGENTS.md. */

#include <windows.h>
#include <tchar.h>

#include "SftBox.h"                     /* SftBox/DLL Header File */

#define SFTDARKMODE_IMPLEMENTATION
#include "SftDarkMode.h"                /* Dark mode support */

#include "resource.h"                   // application resources

HINSTANCE g_hInst;                      // App Instance Handle

/**********************************************************************/
/*                               Data                                 */
/**********************************************************************/

typedef struct tagState {
    LPCTSTR lpszName;
    LPCTSTR lpszAbbr;
    LPCTSTR lpszCapital;
} STATE;

static const STATE m_aStates[] = {
    { TEXT("Alabama"), TEXT("AL"), TEXT("Montgomery") },
    { TEXT("Alaska"), TEXT("AK"), TEXT("Juneau") },
    { TEXT("Arizona"), TEXT("AZ"), TEXT("Phoenix") },
    { TEXT("Arkansas"), TEXT("AR"), TEXT("Little Rock") },
    { TEXT("California"), TEXT("CA"), TEXT("Sacramento") },
    { TEXT("Colorado"), TEXT("CO"), TEXT("Denver") },
    { TEXT("Connecticut"), TEXT("CT"), TEXT("Hartford") },
    { TEXT("Delaware"), TEXT("DE"), TEXT("Dover") },
    { TEXT("Florida"), TEXT("FL"), TEXT("Tallahassee") },
    { TEXT("Georgia"), TEXT("GA"), TEXT("Atlanta") },
    { TEXT("Hawaii"), TEXT("HI"), TEXT("Honolulu") },
    { TEXT("Idaho"), TEXT("ID"), TEXT("Boise") },
    { TEXT("Illinois"), TEXT("IL"), TEXT("Springfield") },
    { TEXT("Indiana"), TEXT("IN"), TEXT("Indianapolis") },
    { TEXT("Iowa"), TEXT("IA"), TEXT("Des Moines") },
    { TEXT("Kansas"), TEXT("KS"), TEXT("Topeka") },
    { TEXT("Kentucky"), TEXT("KY"), TEXT("Frankfort") },
    { TEXT("Louisiana"), TEXT("LA"), TEXT("Baton Rouge") },
    { TEXT("Maine"), TEXT("ME"), TEXT("Augusta") },
    { TEXT("Maryland"), TEXT("MD"), TEXT("Annapolis") },
    { TEXT("Massachusetts"), TEXT("MA"), TEXT("Boston") },
    { TEXT("Michigan"), TEXT("MI"), TEXT("Lansing") },
    { TEXT("Minnesota"), TEXT("MN"), TEXT("Saint Paul") },
    { TEXT("Mississippi"), TEXT("MS"), TEXT("Jackson") },
    { TEXT("Missouri"), TEXT("MO"), TEXT("Jefferson City") },
    { TEXT("Montana"), TEXT("MT"), TEXT("Helena") },
    { TEXT("Nebraska"), TEXT("NE"), TEXT("Lincoln") },
    { TEXT("Nevada"), TEXT("NV"), TEXT("Carson City") },
    { TEXT("New Hampshire"), TEXT("NH"), TEXT("Concord") },
    { TEXT("New Jersey"), TEXT("NJ"), TEXT("Trenton") },
    { TEXT("New Mexico"), TEXT("NM"), TEXT("Santa Fe") },
    { TEXT("New York"), TEXT("NY"), TEXT("Albany") },
    { TEXT("North Carolina"), TEXT("NC"), TEXT("Raleigh") },
    { TEXT("North Dakota"), TEXT("ND"), TEXT("Bismarck") },
    { TEXT("Ohio"), TEXT("OH"), TEXT("Columbus") },
    { TEXT("Oklahoma"), TEXT("OK"), TEXT("Oklahoma City") },
    { TEXT("Oregon"), TEXT("OR"), TEXT("Salem") },
    { TEXT("Pennsylvania"), TEXT("PA"), TEXT("Harrisburg") },
    { TEXT("Rhode Island"), TEXT("RI"), TEXT("Providence") },
    { TEXT("South Carolina"), TEXT("SC"), TEXT("Columbia") },
    { TEXT("South Dakota"), TEXT("SD"), TEXT("Pierre") },
    { TEXT("Tennessee"), TEXT("TN"), TEXT("Nashville") },
    { TEXT("Texas"), TEXT("TX"), TEXT("Austin") },
    { TEXT("Utah"), TEXT("UT"), TEXT("Salt Lake City") },
    { TEXT("Vermont"), TEXT("VT"), TEXT("Montpelier") },
    { TEXT("Virginia"), TEXT("VA"), TEXT("Richmond") },
    { TEXT("Washington"), TEXT("WA"), TEXT("Olympia") },
    { TEXT("West Virginia"), TEXT("WV"), TEXT("Charleston") },
    { TEXT("Wisconsin"), TEXT("WI"), TEXT("Madison") },
    { TEXT("Wyoming"), TEXT("WY"), TEXT("Cheyenne") },
};
#define STATE_COUNT (sizeof(m_aStates) / sizeof(m_aStates[0]))

#define COL_NAME        0
#define COL_ABBR        1
#define COL_CAPITAL     2
#define COL_COUNT       3

#define MAX_HISTORY     8               // entries kept in the city combo box

/**********************************************************************/
/*                         Combo Box Setup                            */
/**********************************************************************/

static void InitColumn(LPSFTBOX_COLUMN lpCol, int position, int width, LPCTSTR lpszTitle)
{
    memset(lpCol, 0, sizeof(SFTBOX_COLUMN));
    lpCol->width = width;
    lpCol->style = ES_LEFT;
    lpCol->styleTitle = ES_LEFT;
    lpCol->lpszTitle = (LPTSTR) lpszTitle;
    lpCol->colorBg = lpCol->colorFg = lpCol->colorBgSel = lpCol->colorFgSel = SFTBOX_NOCOLOR;
    lpCol->colorBgEnd = lpCol->colorBgSelEnd = SFTBOX_NOCOLOR;
    lpCol->colorProgress = lpCol->colorProgressEnd = SFTBOX_NOCOLOR;
    lpCol->realPos = lpCol->dispPos = position;
}

static void InitStates(HWND hwndBox, int editColumn)
{
    SFTBOX_COLUMN aCols[COL_COUNT];
    int i;

    InitColumn(&aCols[COL_NAME], COL_NAME, 100, TEXT("State"));
    InitColumn(&aCols[COL_ABBR], COL_ABBR, 40, TEXT("Abbr."));
    InitColumn(&aCols[COL_CAPITAL], COL_CAPITAL, 100, TEXT("Capital"));
    SftBox_SetColumns(hwndBox, COL_COUNT, aCols);

    SftBox_SetShowHeaderDropDown(hwndBox, TRUE);// Column titles in the dropdown, as buttons
    SftBox_SetHeaderStyle(hwndBox, SFTBOX_COLSTYLE_BUTTONTEXT);
    SftBox_EnableSortIndicators(hwndBox, TRUE, TRUE);// A clicked column title shows the sort direction
    SftBox_SetShowGrid(hwndBox, TRUE);
    SftBox_SetGridStyle(hwndBox, SFTBOX_GRID_VERT);
    SftBox_SetDroppedHeight(hwndBox, 10);       // Show up to 10 items
    SftBox_SetDropDownOptimalWidth(hwndBox, TRUE);// The dropdown is as wide as its columns
    SftBox_SetDisableNoScroll(hwndBox, TRUE);   // Scroll bars always shown, needed for the sizing box
    SftBox_SetShowResizer(hwndBox, TRUE);       // The user can resize the dropdown
    SftBox_SetDarkMode(hwndBox, SFTBOX_DARKMODE_AUTO);

    // The edit portion shows and searches this column.
    SftBox_SetEditSearchColumn(hwndBox, editColumn);
    // Typing completes the text with the first matching item.
    SftBox_SetEditSearchMode(hwndBox, SFTBOX_SEARCH_AUTOCOMPLETE);
    if (editColumn == COL_ABBR)
        SftBox_LimitText(hwndBox, 2);

    for (i = 0 ; i < STATE_COUNT ; ++i) {
        LONG index = SftBox_AddString(hwndBox, m_aStates[i].lpszName);
        SftBox_SetText(hwndBox, index, COL_ABBR, m_aStates[i].lpszAbbr);
        SftBox_SetText(hwndBox, index, COL_CAPITAL, m_aStates[i].lpszCapital);
    }

    SftBox_MakeColumnOptimal(hwndBox, -1, 0);   // Make all columns optimal
    SftBox_RecalcHorizontalExtent(hwndBox, 0);
}

static void InitCities(HWND hwndBox)
{
    SftBox_SetDroppedHeight(hwndBox, MAX_HISTORY);
    SftBox_SetEditSearchMode(hwndBox, SFTBOX_SEARCH_AUTOCOMPLETE);
    SftBox_LimitText(hwndBox, 40);
    SftBox_SetDarkMode(hwndBox, SFTBOX_DARKMODE_AUTO);

    SftBox_AddString(hwndBox, TEXT("Orlando"));
    SftBox_AddString(hwndBox, TEXT("Miami"));
    SftBox_AddString(hwndBox, TEXT("Tampa"));
}

/**********************************************************************/
/*                              Sorting                               */
/**********************************************************************/

int CALLBACK SortAscending(HWND hwnd, LPCTSTR lpszString1, LPCTSTR lpszString2, SFTBOX_DWORD_PTR itemData1, SFTBOX_DWORD_PTR itemData2)
{
    return lstrcmpi(lpszString1, lpszString2);
}

int CALLBACK SortDescending(HWND hwnd, LPCTSTR lpszString1, LPCTSTR lpszString2, SFTBOX_DWORD_PTR itemData1, SFTBOX_DWORD_PTR itemData2)
{
    return lstrcmpi(lpszString2, lpszString1);
}

static void SortByColumn(HWND hwndBox)
{
    // The sort column and direction of each control are kept in its user data,
    // matching the sort indicator the control shows in the column title.
    int column = SftBox_GetHeaderButton(hwndBox);
    SFTBOX_DWORD_PTR state = SftBox_GetUserData(hwndBox);
    int lastColumn = (int) LOWORD(state) - 1;
    BOOL fDescending = HIWORD(state) != 0;

    if (column < 0)
        return;
    fDescending = (column == lastColumn) ? !fDescending : FALSE;
    SftBox_SortItems(hwndBox, -1, column, fDescending ? SortDescending : SortAscending);
    SftBox_SetUserData(hwndBox, MAKELONG(column + 1, fDescending));
}

/**********************************************************************/
/*                         Current Selections                         */
/**********************************************************************/

static void ShowStatus(HWND hwndDlg, HWND hwndBox)
{
    TCHAR szName[80], szAbbr[20], szCapital[80], szStatus[256];
    LONG index = SftBox_GetCurSel(hwndBox);

    if (index < 0) {
        // The text in the edit portion doesn't match an item
        GetWindowText(hwndBox, szName, sizeof(szName) / sizeof(szName[0]));
        wsprintf(szStatus, TEXT("\"%s\" is not in the list."), szName);
    } else {
        SftBox_GetText(hwndBox, index, COL_NAME, szName);
        SftBox_GetText(hwndBox, index, COL_ABBR, szAbbr);
        SftBox_GetText(hwndBox, index, COL_CAPITAL, szCapital);
        wsprintf(szStatus, TEXT("%s (%s), capital %s"), szName, szAbbr, szCapital);
    }
    SetDlgItemText(hwndDlg, IDC_STATUS, szStatus);
}

static void AddCity(HWND hwndDlg)
{
    // Add the text typed to the top of the list, unless it is already there
    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);
    }
    SftBox_SetCurSel(hwndBox, index);
}

/**********************************************************************/
/*                           Dialog Proc                              */
/**********************************************************************/

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:
        switch (LOWORD(wParam)) {
        case IDC_STATE:
        case IDC_ABBR:
            switch (HIWORD(wParam)) {
            case SFTBOXN_SELCHANGE:     // an item was selected
            case SFTBOXN_EDITCHANGE:    // the text in the edit portion changed
                ShowStatus(hwndDlg, (HWND) lParam);
                break;
            case SFTBOXN_LBUTTONDOWN_COLUMN: // a column title in the dropdown was clicked
                SortByColumn((HWND) lParam);
                break;
            }
            break;
        case IDC_ADD:
            AddCity(hwndDlg);
            break;
        case IDCANCEL:
            EndDialog(hwndDlg, 0);
            return TRUE;
        }
        break;
    }

    {
        INT_PTR dmResult;
        if (SftDarkMode_HandleDialogMessage(hwndDlg, msg, wParam, lParam, &dmResult))
            return dmResult;
    }

    return FALSE;
}

/**********************************************************************/
/*                              WinMain                               */
/**********************************************************************/

int PASCAL WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpszCmdLine, int cmdShow)
{
    g_hInst = hinst;

    SftDarkMode_Init();                 /* Dark mode support */
    SftBox_RegisterApp(hinst);          /* Register with SftBox/DLL */

    DialogBox(g_hInst, MAKEINTRESOURCE(IDD_MAIN), NULL, MainDialogProc);

    SftBox_UnregisterApp(hinst);        /* Unregister from SftBox/DLL */

    return 0;
}

Last Updated 11/21/2026 - (email)
© 2026 Softel vdm, Inc.