c - WinAPI Combo Box Issue - Skipped by case

Hi all I wonder if anyone could tell me what I am doing wrong I have made a dialog application and all its done apart from one problem which is my combo boxes I have added these to tab page and they are called by the handler below. My problem is where to add the combo box fields i.e

   // Sets up the combo box menu 
   HWND hwndComboTab = GetDlgItem(hwndDlg, IDC_COMBO1);
   ComboBox_AddString(hwndComboTab, "Option 1");
   ComboBox_AddString(hwndComboTab, "Option 2");
   ComboBox_AddString(hwndComboTab, "Option 3");

I assumed it would be under WM_INITDIALOG but I always get a case skipped error. I assume it would need to be inside

case 1: for example is this correct? and if so what varriables are posted to this section a bool?

Also I tryed adding it before the switch statesments and it works but the options panel is replicaed many times giving me spammed options.

INT_PTR CALLBACK MyTabDialogOne(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam) 
{ 

   // Switches the message for tab 1 control
    switch (uMsg) 
    { 
    case WM_INITDIALOG: // Tryed here case error
        break; 
    case WM_COMMAND:      
        { 
            switch (LOWORD(wParam)) // No point here as this is after a button is clicked
            { 
    case IDC_GET_CODE: // Handles the GET Code button in tab
// Displays the result to main window
HWND hwndEdit2 = GetDlgItem(GetParent(GetParent(hwndDlg)), IDC_OUTPUT);
SetWindowText(hwndEdit2,buffer);
// Displays the result to bottom bar
HWND hwndStatic1 = GetDlgItem(GetParent(GetParent(hwndDlg)), IDC_STATIC1); 
Static_SetText(hwndStatic1, "Completed");
//MessageBox(hwndDlg,TEXT("Selected"),TEXT("Button"),0); 
            break; 
            } 
            break; 
        } 
    } 
    return FALSE ; 
} 

Thanks in advance

1 Answer

  1. Evan- Reply

    2019-11-13

    I suspect that when you tried to put it in WM_INITDIALOG, you made the common mistake of not putting braces around the block:

    INT_PTR CALLBACK MyTabDialogOne(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam) 
    { 
    
       // Switches the message for tab 1 control
        switch (uMsg) 
        { 
        case WM_INITDIALOG:
            {
                HWND hwndComboTab = GetDlgItem(hwndDlg, IDC_COMBO1);
                ComboBox_AddString(hwndComboTab, "Option 1");
                ComboBox_AddString(hwndComboTab, "Option 2");
                ComboBox_AddString(hwndComboTab, "Option 3");
                break; 
            }
        case WM_COMMAND:      
            { 
                switch (LOWORD(wParam)) // No point here as this is after a button is clicked
                { 
                    case IDC_GET_CODE: // Handles the GET Code button in tab
                        {
                            // Displays the result to main window
                            HWND hwndEdit2 = GetDlgItem(GetParent(GetParent(hwndDlg)), IDC_OUTPUT);
                            HWND hwndStatic1 = GetDlgItem(GetParent(GetParent(hwndDlg)), IDC_STATIC1); 
                            SetWindowText(hwndEdit2,buffer);
                            // Displays the result to bottom bar
                            Static_SetText(hwndStatic1, "Completed");
                            //MessageBox(hwndDlg,TEXT("Selected"),TEXT("Button"),0); 
                            break; 
                        }
                    default:
                        return FALSE;
                } 
                break; 
            } 
        default:
            return FALSE; 
        }
    
        return TRUE;
    } 
    

    Not adding the braces causes the declaration of hwndComboTab to be skipped. I've also sorted out your return statements. If you handled a message, you should return TRUE, else FALSE. This is with exception to a few messages as documented here.

    One last thing I did was to move your declarations to the start of each scope. I assume your file is .cpp even though you appear to be coding C. If your extension was .c, VC++ would treat the file as a C source and the way you are doing your declarations would not even compile.

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>