Solved

changing styles of dialog controls

Posted on 1998-09-21
1
326 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 51
Windows Drag & Drop Location 2 108
post4 challenge 28 105
Modbus - whats the maximum I can store in one register? 4 80
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

821 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