Open new form inside existing form [duplicate] - c#

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();

Related

How to set form location to parent form center? [duplicate]

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)

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);

How to show a form without "showing" it [duplicate]

This question already has answers here:
Load a form without showing it
(8 answers)
Closed 8 years ago.
I have winforms application which has a popup dialog. I need the dialog to popup but without showing itself. This is how I currently do it:
DialogForm myDialog = new DialogForm();
myDialog.Show();
myDialog.Visible = false;
The problem with this method is that for a split second the dialog shows up and then disappears. From a functional point of view it works fine, but it does annoy some users of my application and actually takes the "focus" away from the main form. I've also tried this but it doesn't work:
DialogForm myDialog = new DialogForm();
myDialog.Visible = false;
myDialog.Show();
There has to be a way to create the dialog and run it in the background without having to call "Show()." Thanks for any possible help!
How about doing something like positioning the pop-up window in the left lower corner of the screen?
So even if it's showing for a split second, nobody will still be able to see it.
So before showing your window, write a line such as:
myDialog.Location = new Point(x,y);
// the x,y will be your coordinates of the lower-left margin of Form1

Trying to make a dialog window remember its last position

I am creating a simple dialogue window in C#, and want to remember where it is placed so that I can open another in the same place later (during the same application run, so no need for config files etc). I can easily save the Location (a Point) or the Bounds (a Rectangle), but on creating another form, calling form.ShowDialog() resets both:
Form form= new Form();
form.Location = ptSavedLocation;
//now form.Location is correct
form.ShowDialog();
//now form.Location is default again, and form is displayed where I don't want it.
How can I get the form to respect its Location (or Bounds, or any other appropriate property / setter) ? Thanks!
Set the forms start position to Manual
eg.
Form form= new Form();
form.StartPosition = FormStartPosition.Manual;
form.Location = ptSavedLocation;
//now form.Location is correct
form.ShowDialog();
//now form.Location is default again, and form is displayed where I don't want it.
Set the forms StartPosition property to Manual

Categories