I've created a main form that is an MDI form, and also a child form.
How I can make the child form remain centered in the parent MDI form whenever I "maximize" or "restore" the MDI form?
Let me see if I understand you right, you want your MDI application to open your child form in the center of the Form, then keep it there no matter what you do to resize it. The Mdi interface has its own ideas on how to place forms, the first step is to have the Child form set its position in its load event, then you can keep it in the center by using the Parents Resize event. This is a sample using 2 Forms, see if it is what you are looking for.
Form1 Mdi Parent
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Form2 frm2 = new Form2();
frm2.MdiParent = this;
frm2.Show();
}
protected override void OnResize(EventArgs e)
{
CenterForms();
base.OnResize(e);
}
private void CenterForms()
{
foreach (var form in MdiChildren) //This will center all of the Child Forms
{
form.Left = (ClientRectangle.Width - form.Width) / 2;
form.Top = (ClientRectangle.Height - form.Height) / 2;
}
}
}
Form2 Mdi Child
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
Left = (MdiParent.ClientRectangle.Width - Width) / 2;
Top = (MdiParent.ClientRectangle.Height - Height)/2;
}
}
You just need to set the child form starting position in center of Parent Like This
your Child Form
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
You can set the child form property "StartPosition" to CenterParent
OR
When you are loading new form, add this code
Form frm = new Form();
frm.StartPosition = FormStartPosition.Manual;
frm.Location = new Point(this.Location.X + (this.Width - frm.Width) / 2, this.Location.Y + (this.Height - frm.Height) / 2);
frm.Show(this);
Related
I'm writing an app that will play video on the main form or from another form when user clicks Go To Full Screen button. What I want to do is transfer the picture box and playback controls to the Full Screen form. If the user presses the Escape key, the picturebox and playback controls are returned to the main form. The controls show up on the Full Screen form, but the controls don't show up again on the main form after pressing the Escape key.
Here's the code for going to full screen...
private void btnGoToFullScreen_Click(object sender, EventArgs e)
{
frmFullScreen frm = new frmFullScreen();
frm.WindowState = FormWindowState.Maximized;
frm.Owner = this;
pbViewer.Parent = pnlVideoControls.Parent = frm;
pbViewer.Height = frm.Height;
pbViewer.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width / 2 - pbViewer.Width / 2,
Screen.PrimaryScreen.WorkingArea.Height / 2 - pbViewer.Height / 2);
pnlVideoControls.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width / 2 - pnlVideoControls.Width / 2,
Screen.PrimaryScreen.WorkingArea.Height - 200);
frm.Show();
}
And here's the code to return the controls to the main form...
private void frmMain_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
frmMain.Controls.Add(pbViewer);
frmMain.Controls.Add(pnlVideoControls);
pbViewer.Size = Properties.Settings.Default.Portrait;
pbViewer.Location = new Point(16, 24);
pnlVideoControls.Location = new Point(145, 418);
pbViewer.Show(); pbViewer.BringToFront();
pnlVideoControls.Show(); pnlVideoControls.BringToFront();
this.Refresh();
foreach (frmFullScreen frm in this.OwnedForms)
{
frm.Dispose();
}
}
}
NOTE: PictureBox is pbViewer and pnlVideoControls is the panel with playback buttons.
The menu form is an Mdi parent of the randomFacts form, which is the child form.
Whenever I click the quiz button on the menu form, the following code is executed (items are initially hidden on the menu form but are then shown upon clicking the quiz button):
private void btn1Quiz_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Start quiz?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
foreach (Form f in OwnedForms)
{
f.Hide();
}
randomFacts.Hide();
randomFacts.Refresh();
ShowAll(); // shows quiz items
SetTransparency();
}
}
private void ShowAll()
{
labelQuestion.Show();
labelQuestionNumber.Show();
labelScore.Show();
labelScore.Text = "Score: 0";
labelTheme.Show();
btnOption1.Show();
btnOption2.Show();
btnOption3.Show();
btnOption4.Show();
pictureBox1.Show();
SetTransparency();
Image background = new Bitmap("andromeda.jpg");
this.BackgroundImage = null;
this.BackgroundImage = background;
}
private void SetTransparency()
{
labelQuestion.BackColor = Color.Transparent;
labelTheme.BackColor = Color.Transparent;
labelQuestionNumber.BackColor = Color.Transparent;
labelScore.BackColor = Color.Transparent;
}
However, if I click the Facts button first when I load the menu form, and the following code is executed, and then click the quiz button to load the quiz items, the quiz labels lose their transparency and they have a grey background.
private void btn2Facts_Click(object sender, EventArgs e)
{
// menu form is a parent, randomFacts is a child.
this.IsMdiContainer = true;
randomFacts.MdiParent = this;
HideAll();
randomFacts.Show();
randomFacts.Location = new Point(200, 10);
randomFacts.FormBorderStyle = FormBorderStyle.None;
}
I will give more code if necessary. How do I solve this?
I have a form that I want to show centred to its parent, using something like this.
Form f = new Form();
f.StartPosition = FormStartPosition.CenterParent;
f.ShowDialog(this);
The problem occurs if this code is triggered whilst the application is in a minimised state and when the application is restored my form is shown at the top right of the screen, rather than being centred to its parent.
Does anyone know how to fix this issue?
The Standard Windows MessageBox Dialog behaves correctly and when the application is restored from the minimised state, the dialog box is in the correct position.
My suggestion is to "cache" the parent form location when its being minimized (overriding WndProc() method will let the ability to cache the form location before its being minimized).
private Point CachedLocation;
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0112) // WM_SYSCOMMAND
{
if (m.WParam == new IntPtr(0xF020)) // SC_MINIMIZE
{
// save the form location beofore it is minimized
CachedLocation= this.Location;
}
m.Result = new IntPtr(0);
}
base.WndProc(ref m);
}
Now, if the parent form is minimized when calling the child form use the Cached location point (by checking FormWindowState Enum):
private void button1_Click(object sender, EventArgs e)
{
Form f = new Form();
if (this.WindowState == FormWindowState.Minimized)
{
f.Top = (CachedLocation.Y + (this.Height / 2)) - f.Height / 2;
f.Left = (CachedLocation.X + (this.Width / 2)) - f.Width / 2;
f.StartPosition = FormStartPosition.Manual;
f.ShowDialog();
}
else
{
f.StartPosition = FormStartPosition.CenterParent;
f.ShowDialog();
}
}
suppose i have have two sdi form and when apps run then a sdi form show and from there i am showing another sdi form but the problem is i can drga any where the second sdi form which i do not want. in case of MDI form the mdi child form can not be drag out of mdi form boundary.so in my case i want to simulate the same thing. i want no other sdi form can not be drag out form my main sdi form's boundary. so just guide me how to do this.
i could guess that i have to work with form drag event and from there i have to check form top and left but need more suggestion.
private void Form1_Move(object sender, EventArgs e)
{
this.Location = defaultLocation;
}
i try to do it this way but it is not working.
public partial class Form2 : Form
{
Form1 _parent = null;
public Form2()
{
InitializeComponent();
}
public Form2(Form1 parent)
{
InitializeComponent();
_parent = parent;
}
private void Form2_Move(object sender, EventArgs e)
{
if((this.Location.X+this.Width) > _parent.Width)
{
this.Location = new System.Drawing.Point(_parent.ClientRectangle.Width-this.Width,this.Location.Y);
}
if ((this.Location.Y + this.Height) > _parent.Height)
{
this.Location = new System.Drawing.Point(_parent.ClientRectangle.Height - this.Height, this.Location.X);
}
if (this.Location.Y < 0)
{
this.Location = new System.Drawing.Point(this.Location.X);
}
if (this.Location.X < 0)
{
this.Location = new System.Drawing.Point(this.Location.Y);
}
}
}
please guide me where i made the mistake.
thanks
UPDATE
private void Form2_Move(object sender, EventArgs e)
{
int left = this.Left;
int top = this.Top;
if (this.Left < _parent.Left)
{
left = _parent.Left;
}
if (this.Right > _parent.Right)
{
left = _parent.Right - this.Width;
}
if (this.Top < _parent.Top)
{
top = _parent.Top;
}
if (this.Bottom > _parent.Bottom)
{
top = _parent.Bottom - this.Height;
}
this.Location = new Point(left, top);
}
I recomend to you follow the #ProgrammerV5 recomendation. But if you realy need to control the form movement, please see the use of Cursor.Clip property
here are some information: http://www.codeproject.com/Tips/375046/The-Cursor-Clip-Property
Also you may need to do a MouseCapture.
I am trying to open a (non decorated) childform at the upper right corner of my main form no matter if the main form is maximized or at it's normal size.
But no matter how I try I don't get it to open where I want it to.
I found a post that described how to open the form relative to another control in the form, but that didn't work either:
How to display a Modal form in a position relative to the a control in the parent window (opener)
Have tried to search for a few hours now on google for a solution, but either there's no answer (doubdfull) or I am not searching for the tight words combination (more likely).
Could anyone please either point me to a similar question, or help me how to achieve what I am hoping for?
Sounds to me you ought to be using a UserControl that you anchor to the top and right. But let's make a form work. You'll need to wire its Load event so you can move it into the right spot after it rescaled itself. Then you need the main form's LocationChanged and Resize events so you can keep the child form in the right spot.
So a sample program with boilerplate Form1 and Form2 names and a button on Form1 to display the child could look like this:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.button1.Click += button1_Click;
this.Resize += this.Form1_Resize;
this.LocationChanged += this.Form1_LocationChanged;
}
Form child;
private void button1_Click(object sender, EventArgs e) {
if (child != null) return;
child = new Form2();
child.FormClosed += child_FormClosed;
child.Load += child_Load;
child.Show(this);
}
void child_FormClosed(object sender, FormClosedEventArgs e) {
child.FormClosed -= child_FormClosed;
child.Load -= child_Load;
child = null;
}
void child_Load(object sender, EventArgs e) {
moveChild();
}
void moveChild() {
child.Location = this.PointToScreen(new Point(this.ClientSize.Width - child.Width, 0));
}
private void Form1_Resize(object sender, EventArgs e) {
if (child != null) moveChild();
}
private void Form1_LocationChanged(object sender, EventArgs e) {
if (child != null) moveChild();
}
}
Try something like that:
private void button1_Click(object sender, EventArgs e)
{
ChildForm win = new ChildForm();
int screenHeight = Screen.PrimaryScreen.WorkingArea.Height;
int screenWidth = Screen.PrimaryScreen.WorkingArea.Width;
Point parentPoint = this.Location;
int parentHeight = this.Height;
int parentWidth = this.Width;
int childHeight = win.Height;
int childWidth = win.Width;
int resultX = 0;
int resultY = 0;
if ((parentPoint.X + parentWidth + childWidth) > screenWidth)
{
resultY = parentPoint.Y;
resultX = parentPoint.X - childWidth;
}
else
{
resultY = parentPoint.Y;
resultX = parentPoint.X + parentWidth;
}
win.StartPosition = FormStartPosition.Manual;
win.Location = new Point(resultX, resultY);
win.Show();
}
I think you should try something like this:
private void buttonOpen_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
//"this" is the parent form
frm2.DesktopLocation = new Point(this.DesktopLocation.X + this.Width - frm2.Width, this.DesktopLocation.Y);
}
Simple, easy, and works (for me).