How to change a combobox style?

I have a dialob that contains a combobox.
this combo box is initialized with the style  CBS_DROPDOWNLIST (which is preset through the dialog editor).
the problem is that I need to change this style at run time to CBS_DROPDOWN according to another radio button which
the user might click it.
 
I've tried using ModifyStyle() - the function returned a success value, but the combo box didn't change its style.
 
udibsAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
mikeblasConnect With a Mentor Commented:
> I've tried using ModifyStyle() - the function returned
 > a success value, but the combo box didn't change its style.

Windows controls are just plain old windows. They're implemented by Windows itself, of course, but they're just like the windows you code for your own applications.

They have window procedures and style bits. When they're created, they look at their style bits and see how they should create themselves. Once they're created, they go along responding to messages sent to them.

It's possible that you change the style on a window by using SetWindowLong(). Doing so causes the window to receive WM_STYLECHANGED and WM_STYLECHANGING messages. The window may respond to those messages and alter its appearance, drawing, memory management, or other features to accomodate the changed styles.

Then again, it may not. Maybe it doesn't want to recreate itself, or significantly change its style or layout after it's already been created. Maybe doing a significant change causes serious user interface issues (such as bouncing focus around needlessly and unpredictably, for instance, or screwing-up mouse capture).

Some Windows controls cache their styles and only pay attention to them during creation. Some allow you to flip style bits back and forth after craetion to change the style. Many controls cache some style bits and not others.

The CBS_DROPDOWN, CBS_DROPDOWNLIST, and CBS_SIMPLE styles are all examples of styles that _can't_ be change after the window is created. If your user interface demands that a window
switch back and forth among these styles, you'll find that modifying the style isn't enough.

You have two reasonable alternatives:

1) You could create multiple controls, one with each of the styles you want. Show one, hide the others. When you need to switch, you'll want to copy all the data and state interesting to you from the old control to the newly visible control. The problem with this is that it may cause you fits when referencing the control elsewhere in the window. You can solve most of those problems. You'll also have to remember to juggle tabbing order appropriately.

2) You can create a single control. Then, when it comes time to switch, you can destroy that control and create another one in its stead. The benefit here is that the controls can share IDs if only one is created at a time. You'll have less trouble with tabbing order, but might have problems with focus (eg, what if the destroyed control had input focus?). The tradeoff is that copying the data may become a little bit more difficult.

That's the way it is.

..B ekiM
0
 
kakamnaCommented:
Have u trid InvalidRect();
Or UpdateAllViews();
0
 
ZoppoCommented:
Hi udibs,

I think it's not possible to do this using ModifyStyle. There's an article on codeguru with this subject: http://www.codeguru.com/combobox/SwitchCombo.shtml

hope that helps,

ZOPPO
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
jhanceCommented:
Have you tried using SetWindowLong()?

I've not tried this myself but I tend to agree with Zoppo.

One workaround comes to mind, however:

In the dialog create two combo boxes, one with each style.  Then depending on the state of your radio button, disable and hide the first one and enable and show the second one.  Use a combination of EnableWindow() and ShowWindow() to do this.
0
 
V_BapatCommented:
Hi udibs
There was a similar question 3-4 months back which was answered. Search in E-E for that. I am sure you will get it.

Vicky
0
 
MichaelSCommented:
You can use another way, just create two comboboxes and show only the right one. If you need another type, hide current and show right.
0
 
WynCommented:
Try use SetWindowLong() GetWindowLong and  use ^ to change the style.
Wyn
0
 
ZoppoCommented:
Hi jhance, hi Wyn,

I'm very sure SetWindowLong even does not work when ModifyStyle doesn't, because ModifyStyle just calls the _AfxModifyStyle function, which itsef calls SetWindowLong:

AFX_STATIC BOOL AFXAPI _AfxModifyStyle(HWND hWnd, int nStyleOffset,
DWORD dwRemove, DWORD dwAdd, UINT nFlags)
{
 ASSERT(hWnd != NULL);
 DWORD dwStyle = ::GetWindowLong(hWnd, nStyleOffset);
 DWORD dwNewStyle = (dwStyle & ~dwRemove) | dwAdd;
 if (dwStyle == dwNewStyle)
  return FALSE;

 ::SetWindowLong(hWnd, nStyleOffset, dwNewStyle);
 if (nFlags != 0)
 {
  ::SetWindowPos(hWnd, NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | nFlags);
 }
 return TRUE;
}

ZOPPO
0
 
WynCommented:
O.K,but the code seems work,why do not?
Would you please tell me?
Thanx
Wyn
0
 
ZoppoCommented:
Hi Wyn,

When you take a look with i.e. Spy++ at a combo-box you'll see that a DROPDOWNLIST combo box consits of one single window object of class ComboBox and a DROPDOWN combo box of a window object of class ComboBox and a child window object of class Edit.

I think that the Create function from the combo box uses this style to determine whether to create the child edit box or not, so these style are not really comparable with window styles as WS_CHILD or WS_VISIBLE, they're rather some kind of creation flags.

ZOPPO
0
 
ZoppoCommented:
Or, again,

3) take a look at the code at the given URL http://www.codeguru.com/combobox/SwitchCombo.shtml and use it to just give a drop-down combobox the look and feel of a drop-down-list combo box as you need it.

ZOPPO
0
 
MichaelSCommented:
Hi, mikeblas
Few comments ago I already propose the same as you :)
0
 
jhanceCommented:
Hi, MichaelS

As long as we're jockeying for position, I'll just point out that I proposed the same thing first!

>One workaround comes to mind, however:

>In the dialog create two combo boxes,
>one with each style.  Then depending
>on the state of your radio button,
>disable and hide the first one and
>enable and show the second one.  Use a
>combination of EnableWindow() and
>ShowWindow() to do this.
0
 
MichaelSCommented:
Oops, you are right 8-)
0
 
mikeblasCommented:
MichaelS> Few comments ago I already propose the same as you :)
 jhance> I'll just point out that I proposed the same thing first!

I guess you both did. But you did so in a scant two or three sentences, offering no background about why that technique is necessary. It's up to udibs to pick an answer, but it seems to me that a clear explanation of what's really going on is crucial to the learning process.

Also, jhance's answer advises to use both ShowWindow() and EnableWindow() when only ShowWindow() is necessary.

..B ekiM
0
 
udibsAuthor Commented:
no need to fight guys, you were all about  half a day late. i figured it out myself. (creating two combo's)
but thanks anyway.

have a nice day.
0
All Courses

From novice to tech pro — start learning today.