John Woodiwiss

Hi Everyone,

I am using C# 2005 building a Windows Form application.

I am trying to build an interface to a multimeter. I have several GroupBoxes which contain switch controls to set the required range for, say DC Volts or AC Volts.

The code for all this is long winded, but is not causing me a problem.

What is causing the problem is that I need to have all the groupBoxes occupying the same space on the form and hide and show them depending upon whether, say, the DC Volts or AC Volts or what ever function is selected.

With the GroupBoxes occupying different areas of the form, my hide and show code works fine. However, as soon as I overlay one GroupBox with another one, the visiblilty control for each GroupBox stops working and the 'bottom' GroupBox appears to control the visibility of all the other GroupBoxes.

I have tried this on a simply test program and see exactly the same problem.

I can see what is happening in FORM.DESIGNER.CS. However, I really don't want to have to work through this and recode everything by hand. Is there any way to stop this happening

Many thanks for your time

John822179

Live long, prosper and understand C#



Re: Windows Forms General C# How do I keep GroupBoxes seperate?

Derek Smyth

Nanu nanu,

A developer I work with took this approach once and to be honest it became a nightmare to co-ordinate the visibility of all the group boxes based on all the combination of inputs. It was a nightmare and if you can help it don't go down that path.

Let me suggest an alternative, might not be suitable, but I believe it will make things easier for you.

Instead of many group boxes on the form that you hide and display. Instead create a user control that contains the controls of and represents each group box. Then as the user changes the input unload and load each user control.

So take for example your AC and DC inputs. Create a user control for AC inputs and a user control for DC inputs. On your form add a placeholder panel, when the user selects AC then clear the placeholder, load the AC user control and add to placeholder, when the user selects DC then clear the placeholder, load the DC user control and add to placeholder.

The advantage of this approach is you can place all the logic in it's relevant user control which will make everything simplier in your main form.

I'll email you an example and you can see if it will help.






Re: Windows Forms General C# How do I keep GroupBoxes seperate?

John Woodiwiss

Hi Derek,

Many thanks for your quick and useful reply.

I would be EXTREMELY grateful for any example code which stops me digging a hole which is any deeper than the one I seem to be in at the moment!

John822179

Live long, prosper and understand C#





Re: Windows Forms General C# How do I keep GroupBoxes seperate?

Derek Smyth

Hey John,

No worries here is the example...

UserControlHideInsteadOfGroupBox.zip

Doesn't do very much but hopefully it will give you an idea of the alternative approach. You might not know much about User Controls. User controls are just like a subform on which you can create a reusable set of controls. Say for example you have two forms that require the same data entry but each form uses the data differently and displays different results. Instead of adding each of the data entry controls seperately on each form (embedding some duplicate logic also) you create a user control containing the data entry controls and share it between forms.

Thats the idea behind them but that doesn't mean they aren't useful in other scenarios and I believe this situation is one of those cases. In the example there are two user controls AC and DC inputs, on the form there is a panel, this acts as a placeholder, when the radio button is clicked for AC the AC user control is loaded and displayed (this user control would contain the data entry for AC). Very simple code but it removes all sorts of logic in hiding textboxes, labels, and any other controls.

Hope this approach proves useful.






Re: Windows Forms General C# How do I keep GroupBoxes seperate?

JohnWein

Have you considered the TabControl





Re: Windows Forms General C# How do I keep GroupBoxes seperate?

sirjis

A possible reason why the bottom one may have been controlling the others is that you actually made them contained inside of it. You probably can't drag the group boxes directly to the right location using the mouse. Instead, get it close and then tap the arrow keys to align it exactly (or use some of the alignment tools).

That being said, it's still not a very elegant solution. A tab control would probably be my first choice. You can even hide the tabs so it doesn't look like a tab control. See nobugz's post on this thread for an example.





Re: Windows Forms General C# How do I keep GroupBoxes seperate?

John Woodiwiss

Hi Derek,

Thanks for the example code.

I think that you are right, this looks to be a much better way of doing things!

It also looks like a way to do a lot of things that I have been trying to sort out for a long time!

Many thanks

John822179

Live long, prosper and understand C#





Re: Windows Forms General C# How do I keep GroupBoxes seperate?

John Woodiwiss

Hi sirjis,

Many thanks for your reply.

I know that it isn't very elegant, but it was all I had at the time!

I have struggled with this problem for a long time and at last, thanks to you, I know how to stop it happening again. I never would have thought that dragging and dropping a GroupBox would have been different to using the alignment tools, but there you go.

I haven't looked at Tab controls yet, but I will, next time I have a couple of hours to myself, Ha! Ha!

Many thanks for your help

Regards

John822179

Live long, prosper and understand C#