Solved

changing styles of dialog controls

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli‚Ķ
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now