Solved

changing styles of dialog controls

Posted on 1998-09-21
1
328 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

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 54
ODBC Connection Logging, ADO.NET 6 73
How to know only "File created" with EventLog 3 35
sum13 challenge 24 149
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: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 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