473,508 Members | 2,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Center Dialog on Panel - position is too far right? Why?

Hi, I have a simple Problem. I want to center a dialog on a panel of
an application. I do the following:

public class MyDialog : System.Windows.Forms.Form
{
private Panel m_ParentPanel;

public MyDialog(Panel panel)
{
this.m_ParentPanel = panel;
// some controls
}

protected override void OnLayout(LayoutEventArgs levent)
{
int width = this.m_ParentPanel.ClientRectangle.Width - 40;
if (width > 300) width = 300;
int height = 100;

Point screenLocation =
this.m_ParentPanel.PointToScreen(this.m_ParentPane l.Location);

int x = screenLocation.X +
(this.m_ParentPanel.ClientRectangle.Width-width)/2;
int y = screenLocation.Y +
(this.m_ParentPanel.ClientRectangle.Height-height)/2;

this.Bounds = new Rectangle(x, y, width, height);

base.OnLayout (levent);
}
}

The result is nearly what I want, but the dialog box is always shifted
a bit (20px ?) too far right? What is wrong with this way to center
the dialog box? Or better what is the correct way to do it? ;)

Thanks for any help!
Nov 16 '05 #1
2 9467
Since your MyDialog inherits form Form you should have the property
StartPosition. Set this to "CenterParent".

The application that shows this dialog can pass the Panel has the parent and
everything is done for you. You can then ditch all the OnLayout code.

MyDialog myDialog = new MyDialog();
myDialog.ShowDialog(panel1);

With the StartPosition set to CenterParent the dialog will be centered over
the panel.

HTH

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.

"Alexander" wrote:
Hi, I have a simple Problem. I want to center a dialog on a panel of
an application. I do the following:

public class MyDialog : System.Windows.Forms.Form
{
private Panel m_ParentPanel;

public MyDialog(Panel panel)
{
this.m_ParentPanel = panel;
// some controls
}

protected override void OnLayout(LayoutEventArgs levent)
{
int width = this.m_ParentPanel.ClientRectangle.Width - 40;
if (width > 300) width = 300;
int height = 100;

Point screenLocation =
this.m_ParentPanel.PointToScreen(this.m_ParentPane l.Location);

int x = screenLocation.X +
(this.m_ParentPanel.ClientRectangle.Width-width)/2;
int y = screenLocation.Y +
(this.m_ParentPanel.ClientRectangle.Height-height)/2;

this.Bounds = new Rectangle(x, y, width, height);

base.OnLayout (levent);
}
}

The result is nearly what I want, but the dialog box is always shifted
a bit (20px ?) too far right? What is wrong with this way to center
the dialog box? Or better what is the correct way to do it? ;)

Thanks for any help!

Nov 16 '05 #2
Thanks for the quick answer, that would work. But the little program I
am writing is kind of cross platform. It is supposed to work on a PC
and PPC, so I am limited to the functions which are also available for
compact .NET. StartPosition is not part of the compact framework,
thats why I have to use the workaround.
I corrected the position by substraction 10 from the x und y
coordinated and it looks OK, but I am still interrested why the
position is not centered.
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3227
by: Alex | last post by:
Hi Everone, I need some advice on how to setup 4 columns where the outside two are absolute (120px) and the inner two (side by side) are relevent (Fluid) and change with the screen. Here's my...
1
3534
by: RahimAsif | last post by:
Hi guys, I am developing a C# application using VS .NET 2003. Its a typical windows SDI application with a menu and toolbar at the top, treeview on the left and a status bar at the bottom. In my...
11
30078
by: Hoku | last post by:
I am using dual monitors on a dual head ATI AGP4x video card. I installed the Nov 11 2005 release of ATI's catalyst control center with Hydravision to manage the dual monitor setup. I recently...
1
3585
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that...
19
6176
roula
by: roula | last post by:
I HATE DIVS AND DIFFERENT BROWSERS!!! i spent the whole day and night searching for a way to center align stupid divs, the problem is that the code is so sensitive, i lost my mind trying to make them...
1
3467
by: judacris | last post by:
I've seen the threads here about molding 2 divs in a centered fashion. but I can't seem to solve this thing. my blogger blog is functioning well on my site for now, but the blog feed (left) and...
11
4001
by: Mel | last post by:
Does anyone know a way to center the info. on a webpage? I want my web site to work like MySpace.com where the page content remains centered when I resize the browser window. For the life of me I...
4
4496
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
I have a usercontrol that has a panel as a band across the width of the user control When the usercontrol is diaplayed the button is not centered. If I anchor the button to the panel...
8
7851
by: Bob Altman | last post by:
Hi all, This has got to be a dumb question, but... What is the CSS property that I would use to center a table on the web page? If I set the table's Align property in the VS 2008 property...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7383
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7498
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.