Jump to content

[C#] Using multiple panels?

Dobbsjr

I'm trying to create a WinForm application that displays a different panel when a radio button on the main form is checked, but I find that creating multiple panels like that is difficult to manage and irritating. Is there a better way to do this?

Did my post help you? Then make sure to rate it!

Check out my post on symbolic links! || PSU ranking and tiers || Pokemon Thread

 

Link to comment
Share on other sites

Link to post
Share on other sites

Is there a better way to do this?

 

What do these panels contain? Do you have screenshots or example code that we can look at?

Link to comment
Share on other sites

Link to post
Share on other sites

What do these panels contain? Do you have screenshots or example code that we can look at?

The panel only has a single label(for now) and this what I have so far, but whenever I run the program the radio button is being checked and unchecked automatically and the program crashes 

private void rbtnCylinder_CheckedChanged(object sender, EventArgs e)        {            panelCylinder.Visible = false; //panel            rbtnCylinder.Checked = !rbtnCylinder.Checked; //radio button                        if (rbtnCylinder.Checked == true)            {                panelCylinder.Visible = true;            }            else            {                panelCylinder.Visible = false;            }        }

Did my post help you? Then make sure to rate it!

Check out my post on symbolic links! || PSU ranking and tiers || Pokemon Thread

 

Link to comment
Share on other sites

Link to post
Share on other sites

My advice to you is to completely abandon WinForms and use MVVM with WPF as WinForms is simply archaic at this point in time. One usually has a View per panel connected to it's associated ViewModel by the use of a ViewModelLocator. Using a frameworks such a MVVM Light (I like to replace its IoC with Simple Injector); messages can be sent between ViewModels quite easily.

 

Simple MVVM overview: The View (UI) knows nothing about the ViewModel (business logic) and the ViewModel in turn knows nothing about the Model (data source/store ext). All interactions between these layers (should) occur via various abstraction patterns.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...

WPF is really the way to go WinForms are outdated but sometimes if is a very small project you might want to drop the plumbing work associated wit MVVM even if things like Simple MVVM or MVVM light really help to save time.

 

Anyway, if you are hellbent on using WinForms you can create a class that inherits from TabControl and trap the drawing call, that way you get a TabControl without the headers above.

The tabPages can be used like panels and you can than switch between tabs from code whenever the user interacts with your radio buttons, it is also a easy way to create a "wizard-like" application.

 

Traping the call would look like this:

class ExtendedTab : TabControl{    public bool TabHeaders { get; set; }    public ExtendedTab() : base()    {           }    protected override void WndProc(ref Message msg)    {        if (!TabHeaders && msg.Msg == 0x1328 && !DesignMode)        { msg.Result = (IntPtr)1; }        else        { base.WndProc(ref msg); }    }}

You can look up win32 Messages on google if you want to learn more about trapping those calls, its a very handy tool for creating custom controls with WinForms.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×