Solved

C# Winform panel within a panel

Posted on 2016-09-28
4
43 Views
Last Modified: 2016-10-03
I have a panel(child panel) inside another panel (parent panel).  When the parent panel gets resized, I want to resize the child panel to some ration of the parent panel.
The trick is that I don't know the best practice method of doing this.   Right now, I manually call the child panel resize event from the parent's resize event.  That looks sloppy to me.
Plus, while in the child resize event, I get the parent's rectangle, and use that to set the child's rectangle.  This also seems to cause another resize event to trigger....   Really sloppy.
What is the best practice to do this?

thanks!
0
Comment
Question by:roujesky
  • 2
4 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41820294
can't you just rely on the Anchor property of the controls?
0
 

Author Comment

by:roujesky
ID: 41820433
Depending on some external situations, I may want to make the panel bigger or smaller or change the background color of it.  Also, the child panel will have different components visible or invisible, etc....

thanks!
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41820445
that means that you have complex code running each time the resize event is triggered. instead of redoing this logic every time (which can hundreds of times every second, I would start a timer if not already started that would only do the resizing logic on the Tick event. You will need to test how long you will set the timer to (can probably be something like 250 ms).
1
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 41820910
If you use a (parent) form to resize panels then you may use the ResizeEnd event to do some actions after resizing. E.g.:

namespace ResizingTest
{
    public partial class Form1 : Form
    {
        Size initialSize;
        Size resultingSize;

        public Form1()
        {
            InitializeComponent();
        }

 

        private void Form1_ResizeBegin(object sender, EventArgs e)
        {
            initialSize = this.panel1.Size;
            System.Diagnostics.Debug.WriteLine("FORM RESIZE - - - BEGIN");
        }

        private void Form1_ResizeEnd(object sender, EventArgs e)
        {
            resultingSize = this.panel1.Size;
            if (initialSize != resultingSize)
            {
                System.Diagnostics.Debug.WriteLine("##### --- SIZE CHANGED - - - DO YOUR JOB!!! - - - #####");
            }

            System.Diagnostics.Debug.WriteLine("FORM RESIZE - - - END");
        }
    }
}

Open in new window

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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