How to resize a maximized form

G

Guest

I have a situation where I need to display a form in a panel. Everything
works great except if the form is maximized and the panel's size changes. In
this case the maximized form's bounds do not change. I tried updating the
form's Bounds property in the panel's SizeChanged event but this does not
work because the Bounds property seems to be ignored until the form is
restored to the normal state. I also tried using the form's SetBoundsCore
method and setting the form's MaximizedBounds property but these didn't work
either.

Is there any way to change the form's bounds while it is in the maximized
state? I am familiar with MdiClient control and I noticed that the MdiClient
implements the desired behavior but I can not use an MdiClient in my
situation.

Thanks for any help!
Lance
 
L

Linda Liu [MSFT]

Hi Lance,

I performed a test based on your description and did see the problem on my
side.

In fact, there're many limitations when a form is shown within a control.
For example, if the form has a TextBox on it, and the form is shown within
a control, say a Panel, you'll see that you can't set the input caret in
the TextBox when you click in the TextBox.

Your problem is another limitation related to this topic. A workaround of
your problem is to handle the Panel's SizeChanged event and in the event
handler, hide the form and then show the from as a maximized window to
force the form to repaint itself.

The following is the sample code.

using System.Runtime.InteropServices;
[DllImport("user32.dll")]
extern static bool ShowWindow(IntPtr hWnd,int nCmdShow);
int SW_MAXIMIZE = 3;
int SW_HIDE = 0;

void panel1_SizeChanged(object sender, EventArgs e)
{
this.SuspendLayout();
// the 'frm' is the form that is shown within a Panel
ShowWindow(frm.Handle, SW_HIDE );
ShowWindow(frm.Handle, SW_MAXIMIZE);

this.ResumeLayout();
}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Joergen Bech

Why are you using the Bounds property of the form?

If you need the size of the area where you place your
controls, you should use the ClientRectangle property(?)

If you really want the full size of the form, including the
non-client area, I suppose you could read the size of the
working area of the screen the form occupies (in a multi-
monitor setup, this might not be the primary screen), but
that sounds like a hack to me.

---snip---
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Resize
Debug.WriteLine("--------------")
Debug.WriteLine("Bounds: " & Me.Bounds.ToString)
Debug.WriteLine("Client: " & Me.ClientRectangle.ToString)
Debug.WriteLine("WorkingArea: " &
Screen.FromControl(Me).WorkingArea.ToString)
End Sub
---snip---

I am not quite sure what your problem is: As far as I can
tell, the bounds *are* updated when maximizing the form, though
the rectangle reported extends a few pixels beyond the borders
of the working area (i.e. the 3D border + a few pixels of flat border
you normally see when the form is restored).

Or have I misunderstood the question?

Regards,

Joergen Bech
 
J

Joergen Bech

Never mind. I see your point now.

/JB

Why are you using the Bounds property of the form?

If you need the size of the area where you place your
controls, you should use the ClientRectangle property(?)

If you really want the full size of the form, including the
non-client area, I suppose you could read the size of the
working area of the screen the form occupies (in a multi-
monitor setup, this might not be the primary screen), but
that sounds like a hack to me.

---snip---
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Resize
Debug.WriteLine("--------------")
Debug.WriteLine("Bounds: " & Me.Bounds.ToString)
Debug.WriteLine("Client: " & Me.ClientRectangle.ToString)
Debug.WriteLine("WorkingArea: " &
Screen.FromControl(Me).WorkingArea.ToString)
End Sub
---snip---

I am not quite sure what your problem is: As far as I can
tell, the bounds *are* updated when maximizing the form, though
the rectangle reported extends a few pixels beyond the borders
of the working area (i.e. the 3D border + a few pixels of flat border
you normally see when the form is restored).

Or have I misunderstood the question?

Regards,

Joergen Bech
 
G

Guest

Hi Linda,

Thanks a lot for your help and for taking time to understand my issue. Your
suggestion was helpful although the limitations that you pointed out make me
feel like I should rethink my design to see if there is a slick way to avoid
having to show a form in a control.

Thanks again,
Lance
 
L

Linda Liu [MSFT]

Hi Lance,

Thank you for your prompt reply.

Generally speaking, if we need to show something in a container, e.g. a
Panel, or a SplitContainer, we create a UserControl and show the
UserControl in the container.

Showing a UserControl in a container won't have the limitations when we
show a WinForm in the container.

In your scenario, you could create a UserControl for each form you'd like
to show in the panel and then show the UserControl in the panel instead of
the form.

Hope this helps.
If my suggestion is not appropriate to your practice, please feel free to
let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top