Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to change the style and date format of the DateTimePicker control on combobox' CBS_SELCHANGE notification.

When user selects one option, datetime picker should have spin control and show only year. If user selects other options, style should be reset to short date style and date format should show date as dd.mm.yyyy.

Here is the code I have so far:
C++
case WM_COMMAND:
    switch(LOWORD(wParam))
    {
    case IDC_COMBO5:
        {
            if (HIWORD(wParam) == CBN_SELENDOK)  
            {
                int iSelected = ComboBox_GetCurSel((HWND)lParam);

                switch (i)
                {
                case 1:
                    {
                        DWORD_PTR dwStyle = 
                            GetWindowLongPtr(GetDlgItem(hDlg,
                               IDC_DATETIMEPICKER1), GWL_STYLE);
                        // remove short date style
                        dwStyle &= ~DTS_SHORTDATEFORMAT;
                        // add spin control
                        dwStyle |= DTS_UPDOWN;
                        SetWindowLongPtr(GetDlgItem(hDlg,
                            IDC_DATETIMEPICKER1), GWL_STYLE, dwStyle);

                        // set date format
                        DateTime_SetFormat(GetDlgItem(hDlg,
                            IDC_DATETIMEPICKER1), L"yyyy");
                    }
                    break;
                case -1:
                case 0:
                case 2:
                default:
                    {
                        DWORD_PTR dwStyle = 
                            GetWindowLongPtr(GetDlgItem(hDlg,
                                IDC_DATETIMEPICKER1), GWL_STYLE);
                        // remove spin control
                        dwStyle &= ~DTS_UPDOWN;
                        // restore short date format
                        dwStyle |= DTS_SHORTDATEFORMAT;
                        SetWindowLongPtr(GetDlgItem(hDlg,
                            IDC_DATETIMEPICKER1), GWL_STYLE, dwStyle);

                        // set date format
                        DateTime_SetFormat(GetDlgItem(hDlg,
                            IDC_DATETIMEPICKER1), L"dd'|'MM'|'yyyy");
                    }
                    break;
                }
            }
        }
        break; 
        // other cases...

Format gets changed properly, but spin control doesn't get added at all.

Running through debugger, I can see that it goes through my code and throws no exceptions -> I went to Debug->Exceptions and checked everything under Thrown.

I work on Windows 7 x86, using Visual Studio 2013.

I am targeting XP onward.

Can you help me to fix my code so I can achieve desired behavior?
Posted
Comments
Richard MacCutchan 25-Jan-15 13:26pm    
I have tried this also, but it appears that this style needs to be set in the initial resource file, and cannot be changed dynamically.
AlwaysLearningNewStuff 25-Jan-15 15:15pm    
Thank you for your help. I kind of feared of this and hoped that I made a mistake somewhere... It seems that it is Microsoft's implementation...


1 solution

Richard is right, that some style in controls cant be changed in runtime, but must initialized in creation time.

You can work around it with

a) dynamically recreating such control behavior, or
b) create 2 controls in the same place and only one is visible and the other is hidden. It needs some horrific code (switch which is active) but it works.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900