How to set form location to parent form center? [duplicate] - c#

This question already has an answer here:
How to keep form centered to the middle of the screen after resize
(1 answer)
Closed 4 years ago.
I set my Form's StartPosition to CenterParent. Then I change my Form.ClientSize in Form.Load() event.
Because of this my Form is not centered anymore.
Edit:
I use form.ShowDialog() to show the form.

ShowDialog() has overload that takes IWin32Window as a parameter. You can do:
myForm.ShowDialog(this);
and now access Owner.Location in myForm.Load(). Proceed by setting myForm.Location to middle of parent form minus half the width and height of myForm respectively as follows:
Location = new Point(Owner.Location.X + Owner.Width / 2 - ClientSize.Width / 2,
Owner.Location.Y + Owner.Height / 2 - ClientSize.Height / 2)

Related

How to get size of c# windows forms application at runtime?

When we use this.Width and this.Height to get size of a form they get the width and height before run.
I want to know can i get the width and height of my application (The main form) at runtime? I mean after resizing the form or maximizing the form at runtime?
For example I have a main form with a sub form inside it, and a popupControl1 inside the sub form. So I want to change the size of the popupControl1 according to the size of the main form
i tried to do this:
popupControl1.Width = Convert.ToInt32(this.Width / 1.3);
popupControl1.Height = Convert.ToInt32(this.Height / 1.2);
but it dosn't work because this.Width and this.Height gets the width and height befor running the application, so I may want to change the size of the application at runtime by resizing it or the application may be maximized.
I also replaced this.Width and this.Height with this.Parent.Width and this.Parent.Height to get the size of the main form but they also didn't work because this.parent returned null

Pop-up modal dialog with grey background [duplicate]

This question already has answers here:
Center a C# Windows Form inside another window
(1 answer)
Show a child form in the centre of Parent form in C#
(19 answers)
Show Dialog box at center of its parent
(5 answers)
Closed 1 year ago.
I want to make classic a modal Window with dark background.
I don't know why this is so hard to make it. I tried a lot way to do it, however it just doesn't fit with my needs.
I have 10+ modal Windows and they are customized Forms which contain grids, charts, etc.
They are not 'OK, No, Cancel' stuff.
My code basically creates another Form with black background between parent Form and modal Form:
Form f = new Form();
f.BackColor = Color.Black;
f.Size = this.Size;
f.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
f.StartPosition = this.StartPosition;
f.Opacity = 0.6;
f.Show();
notificationSGA nsga = new notificationSGA(Cursor.Position);
nsga.ShowDialog();
f.Dispose();
f.Close();
Above code works perfectly fine. However if I move the parent (master) Form to somewhere instead of center of the screen, the black Form still appears at the center of the screen, not centered to the parent Form.
How can I solve my issue?
Note: This is not duplicated topic with below question:
How to show a pop up message with dark background
Change the .StartPosition to .Manual so that you can set .Location to where your reference form is.
Also, change the owner in the .Show() method for both new Forms.
Form f = new Form();
//(...)
f.Size = this.Size;
f.StartPosition = FormStartPosition.Manual;
f.Location = this.Location;
//(...)
f.Show(this);
using (var nsga = new notificationSGA(Cursor.Position)) {
nsga.StartPosition = FormStartPosition.CenterParent;
nsga.ShowDialog(f);
}
f.Dispose();

How to limit the minimum size of a windows form in visual studio? [duplicate]

This question already has answers here:
Set minimum window size in C# .NET
(4 answers)
Closed 8 years ago.
I am creating a simple Windows form application using Visual C#.
The form is can re-sized by the user (by click and drag). But, I don't want the user to decrease the dimensions of the form beyond a certain limit. How can I set the limits?
Go to your form in VS and press F4 to get to the properties of the form and look for the MinimumSize property:
To set the size from code use:
this.MinimumSize = new Size(100, 100);
Note that this does not prevent minimising/maximising the form! If you want to prevent this, then set the MaximizeBox property to false.
In the WinForm properties there is minimum size property
see here
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.minimumsize(v=vs.110).aspx
To change it on run-time:
int width, height;
this.MinimumSize = new Size(width, height);

Open new form inside existing form [duplicate]

This question already has answers here:
How to CenterParent a non-modal form
(4 answers)
Closed 8 years ago.
I have my main form as my application.
Now when I open a new form say to display settings.
FormB bForm = new FormB(this);
blpForm.Show();
Now this works fine but Form b is a smaller form and it appears outside of the boundaries of my original form. I don't want it to be 'Inside' the original form I just want its initial position to be the centre of the original form.
How do I go about doing that?
You have to set its Location yourself, which means that you also have to set its StartPosition to Manual. It's basic arithmetic to calculate the Location of the dialogue based on the Location and Size of the caller and the Size of the dialogue.
What about this code ? As Rotem said here ,
FormB bForm = new FormB ();
bForm .StartPosition = FormStartPosition.Manual;
bForm .Location = new Point(this.Location.X + (this.Width - bForm.Width) / 2,
this.Location.Y + (this.Height - bForm.Height) / 2); //this is just an example , you can customize the location
bForm .Show();

c# absolute position of control on screen

I'm trying to get the absolute position of a control on the screen. I'm using two monitors and the results aren't really that great...
What I'm doing is opening another form to capture an image, then passing this image to the main form and closing the capture form. I then want the main form to appear in the same place the picture was captured. To get a gist of what I'm trying to do as an example, open Snipping Tool on Windows and capture a snip. The window will then appear in the place that the image was taken.
This is the current code I am using to do this:
Location = new Point(Cursor.Position.X - CaptureBox.Width - CapturePanel.Location.X - CaptureBox.Location.X - 8, Cursor.Position.Y - CaptureBox.Height - CapturePanel.Location.Y - CaptureBox.Location.Y - 30);
CapturePanel contains the CaptureBox control which stores the picture. I'm also taking 8 from the X location and 30 from te Y location to compensate for the form's border and title bar, but the only problem with this is that some computers will be using a different window style, and these numbers will change.
If there is a method that can be used to grab the border and title width/height of windows, that would be great.
EDIT
A solution to this would be:
Location = new Point(
Cursor.Position.X -
CaptureBox.Width -
CapturePanel.Location.X -
CaptureBox.Location.X -
SystemInformation.HorizontalResizeBorderThickness,
Cursor.Position.Y -
CaptureBox.Height -
CapturePanel.Location.Y -
CaptureBox.Location.Y -
SystemInformation.CaptionHeight -
SystemInformation.VerticalResizeBorderThickness
);
With help from King King pointing out SystemInformation to me.
To get the Height of your Window caption, you can try this:
int captionHeight = yourForm.PointToScreen(Point.Empty).Y - yourForm.Top;
To get the Width of the form border, you can try this:
int borderWidth = yourForm.PointToScreen(Point.Empty).X - yourForm.Left;
Also you may want to look at the default caption height by SystemInformation.CaptionHeight.
If you want to get the location of the CaptureBox in screen coordinates, you can use the PointToScreen method:
Point loc = CaptureBox.PointToScreen(Point.Empty);

Categories