Solved

changing styles of dialog controls

Posted on 1998-09-21
1
324 Views
Last Modified: 2013-11-19
I want to change the style of a dialog control on
run-time,e.g. I want to switch the style of a
list box from single column to multicolumn by
button-click. I'm using old-fashionend Visual c++ 1.5 for
win 3.11. Is there any MFC-function doing this job? I could
not find any:(
0
Comment
Question by:elazabi
1 Comment
 
LVL 8

Accepted Solution

by:
Answers2000 earned 100 total points
ID: 1322370
1. In general to change style - you must use SDK style functions (these mix into your MFC program okay)

LONG lStyle = ::GetWindowLong( ::GetDlgItem( m_hWnd, ID_MYCONTROL ), GWL_STYLE ) ;

lStyle &= ~WSBORDER ; // remove WS_BORDER style
lStyle &= ~SS_LEFT ; // remove SS_LEFT say from a static
lStyle |= SS_RIGHT ; // add SS_RIGHT say to a static

::SetWindowLong( ::GetDlgItem( m_hWnd, ID_MYCONTROL ), GWL_STYLE ), lStyle ) ;

You will find this works reasonably well under Win32.  The results are however unpredictable under Win16 on many controls (basically try it and see what happens).  Unfortunately this is a Windows API limitation (MFC builds on top of this), so there is no way to fix this.  However there are ways around...

2. If you want to just enable/disable, show/hide a control you can do this without changing the style.  This works under Win16 or Win32

GetDlgItem( ID_MYCONTROL )->EnableWindow( FALSE ) ; // disable a control
GetDlgItem( ID_MYCONTROL )->EnableWindow( TRUE ) ; // enable a control
GetDlgItem( ID_MYCONTROL )->ShowWindow( SW_SHOW ) ; // show a control
GetDlgItem( ID_MYCONTROL )->ShowWindow( SW_HIDE ) ; // hide a control

In general you will want to only hide controls if they are disabled otherwise the user will get odd results (they can tab into an invisible control for example)

3. Now looking at your specific problem (switching a list box from multi-line to single line selection on the fly)

You will find changing a single list box between the 2 styles does not really work under Win16 (I don't think it really works under Win32).  However you can produce the exact same appearance to the user quite easily :-

i. In the dialog resource add _TWO_ list boxes at identical positions, set one to be multi-selection, set one to be single selection

ii. Add a member function to flip to multi-line selection mode.  This would hide the single selection list and disable it, and then show the multi selection list and enable that.

iii. Add a member function to flip to single selection mode.  This would hide the multi selection list and disable it, and then show the single selection list and enable that.

iv. Add a member function(s) to test which mode you are in, e.g.

BOOL CMyDialog::IsMultiSelectionMode()
{
   return GetDlgItem( ID_LIST_MULTI )->IsWindowEnabled() ;
}

BOOL CMyDialog::IsSingleSelectionMode()
{
   return GetDlgItem( ID_LIST_SINGLE )->IsWindowEnabled() ;
}


v. When you populate the list box initially (or update its contents later), make sure you make the same changes into both list boxes (alternatively you ould update only the current list box, and then copy items over to the other one only when you switch mode).

vi. In WM_INITDIALOG (OnInitDialog) set the initial state (single or multi selection) of the list box by calling ii. or iii.

vii. When you want to flip mode do the same - call ii or iii.

viii. If you want to make the whole thing a bit cleaner (e.g. if you're doing lots of updating on the contents of the lists) you can write your own AddString, etc. function which calls the AddString for both list boxes (rather than you directly calling it yourself)
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Best tools for Backup and restore of Wordpress site 6 57
move a line in eclipse 3 102
only14 challenge 19 76
How to convert MFC::CString to UTF8 wchar_t* 10 207
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question