Solved

C# Winform panel within a panel

Posted on 2016-09-28
4
55 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 70

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 70

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
InputLanguage 1 26
Database Connections Not being returned to Connection Pool 7 27
Footer for each row on Gridview 2 21
VB.Net Tranfer data between to VB.Net apps 8 26
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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