C# WinForms MDI problem - c#

Hello guyes i have one problem i have 1 parent form and 3 children i just want to open them maximized but when i do that in left side comes this 3 controls. How can i open one form without this controls.
If im doing this with wrong way please advice me something does mdi good for such things?
please see this pictures http://img440.imageshack.us/img440/6831/mdinz.jpg
http://img139.imageshack.us/img139/4687/mdi1.jpg

This is a known bug in the MDI implementation, triggered when you create a maximized child window in the parent constructor. This is an example:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
var child = new Form2();
child.MdiParent = this;
child.WindowState = FormWindowState.Maximized;
child.Show();
}
}
You'll see the min/max/restore glyphs displayed twice, restoring the child window leaves the MDI bar on the screen, just as in your first screen shot. The workaround is to move the child creation code to the OnLoad() method. Like this:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
var child = new Form2();
child.MdiParent = this;
child.WindowState = FormWindowState.Maximized;
child.Show();
}
}

You can use the ControlBox, FormBorderStyle, MaximizeBox and MinimizeBox properties to remove the various window UI elements from a form, if you wish.

Related

Getting parent form name of a form

I have a Form (form3)which could be opened from two other forms. Form1 and Form2.
how can I get which one is the parent of the form3?
The term "parent" has a very strict definition in Windows. The Form class derives from Control like all UI classes do, but it is pretty distinct, it is a top-level window. Very unlike the other controls, like Button and TextBox, they are child windows inside a parent window. The parent of a Form is the desktop window, pretty unlikely that you are interested in that one.
So it is pretty meaningless to talk about "the parent of Form3", it is the same parent as Form1 and Form2 and it doesn't help you at all to distinguish which one might have displayed the Form3 window.
Windows does have a way to associate two top-level windows with each other, it has the notion of an owner window. It is meant to implement a tool window or a dialog, an owned window is always displayed on top of its owner and is minimized along with its owner. Creating an owned window is simple:
var toolWindow = new Form3();
toolWindow.Show(this);
This Show() overload takes an argument that indicates its owner, this can be a reference to a Form1 or Form2 object, depending on where this code appears. Inside the Form3 class, you can find the owner back by using the Owner property.
Which is fairly unlikely what you are really talking about, Winforms is frequently a programmer's first introduction to object-oriented programming and dealing with object references is often confounding. If you need a reference to a logical parent in Form3 then just write the code so you pass that parent. Which you do by giving the Form3 class a constructor:
private Form logicalParent;
public Form3(Form parent) {
InitializeComponent();
logicalParent = parent;
}
And creating the window in Form1 or Form2 just takes:
var form = new Form3(this);
form.Show();
You can further improve this code in an object-oriented way by designing a base class for Form1 and Form2, one that has members in common that a class like Form3 would be interested in. Or better yet, an interface that both Form1 and Form2 implement, that reduces the coupling significantly. Last but not least, use events to allow Form3 to notify its logical parent. Probably what you are really looking for.
You can access the Parent form from the Child like this,
Say MainForm is the Form1
MainForm parent = (MainForm)this.Owner;
Or If you want to find the Parent from the hierarchy,
In the Form1 you instantiate Form2 somewhere and pass it a reference to Form1 in ctor:
Form2 f2 = new Form2(this);
In the class definition of Form2 add a field:
private Form1 m_form = null;
In the constructor of the second form set that field:
public Form2(Form1 f)
{
m_form = f;
}
Inside your Form3 you can do this:
var form1 = this.Parent as Form1;
if(form1 != null)
{
//form1.Text
}
var form2 = this.Parent as Form2;
if(form2 != null)
{
//form2.Text
}
Normally I get a name of Parent of control via
MessageBox.Show(control_Name.Parent.Name);
but now we are finding the Parent of a form, so we cannot use the same method because we don't even need to have real Parent and Client relationship to call a Form. So we will have NullReferenceException when we do
MessageBox.Show(this.Parent.Name);
in Form3.
By doing these in Form 1 and Form2 and Form3, I can call the Form3 from both forms.
Form1
Form3 frm3;
public Form1()
{
InitializeComponent();
frm3 = new Form3(this);
}
private void button1_Click(object sender, EventArgs e)
{
frm3.Show();
}
Form2
Form3 frm3;
public Form2()
{
InitializeComponent();
frm3 = new Form3(this);
}
private void button1_Click(object sender, EventArgs e)
{
frm3.Show();
}
Form3
public Form3(Form parent)
{
InitializeComponent();
}
As my conclusion, if the Parent form can only have one, then both of Form1 and Form2 is not Form3 parent Form because either one is not exist but the other one still can call Form3 out. If the Parent form can have more than one, then I think both of Form1 and Form2 are parent forms of Form3.
By the way, if the Parent Form minimized, the Child Form also minimized, then we can make Form1 as Parent Form (or actually owner form) of Form3
Form1
Form3 frm3;
public Form1()
{
InitializeComponent();
frm3 = new Form3(this);
this.AddOwnedForm(frm3);
}
OR
we set the owner of Form3 to the Form who calling it by
Form3
public Form3(Form parent)
{
InitializeComponent();
this.Owner = parent;
}
If you are not agree with me, please give me clear definition of Parent form and Child form.thanks. I try to explain from my point of view and hope someone correct me if I am wrong.

Windows Form, Z-coordinate

In my application I have problem with some windows forms. They are sometimes fell down under another window.
Is there some Z-coordinate for Form? Or how is this working?
Thank you.
EDIT: I should add, that I'm using Smart Client Software Factory.
You can use the Form.Show(IWin32Window owner) method to spawn a form as a child of another form, which will always keep it above that form.
For example:
class Form1 : Form
{
public Form1()
{
InitializeComponent();
var f2 = new Form2();
f2.Show(this);
}
}
class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
}
When an instance of Form1 is created, it will create and show an instance of the Form2 class as a child. Form1 will be behind Form2 regardless of which form has focus.
EDIT: I took some screenshots of the effect, complete with labels that responded to the GotFocus and LostFocus events of each form to demonstrate in case the lovely blue border wasn't enough:

Visible Property of Picture box not working from another form in C#

Hi I am using windows forms in C#. I am trying to modify the visible property of a picture from main form to another. Initially, the visible property of the picture box is set to false. On a button click from another form, the visible property of the picture box is modified to true.
This is the code written in the Form2 method:
private void button_Click(object sender, EventArgs e)
{
public Form1 frm1 = new Form1();
frm1.pictureBox.Visible= true;
}
Form1 is an instance type, so when you do
public Form1 frm1 = new Form1();
frm1.pictureBox.Visible= true;
you're really just creating a new instance of Form1 completely unrelated from your original Form1, changing a picture-box's visible property on it, and then discarding it.
What you can do, is put a reference to the "parent" Form1 inside your Form2 class.
Here's an example
public partial class Form2 : Form
{
public Form2(Form1 parent)
{
InitializeComponent();
this.Parent = parent;
}
Form1 Parent;
private void button1_Click(object sender, EventArgs e)
{
Parent.pictureBox.Visible= true;
}
...
}
there you create an instance of a form :
public Form1 frm1 = new Form1();
This is then obviously NOT the form you already may have in your page, which you could simply access by its ID.
According to your written code it will create new instance of the desired form, and NOT take the existing open form. Hence to identify the existing open form containing target picture box you need the target form and controlling form be related by like Parent form or MDI Parent Form.
Assuming case of MDI Parent Form (i.e. Controlling form is MDI Parent of Target Form), you need following codes to identify to existing open form:
foreach (Form frm in MdiChildren)
{
if (frm is myTargetForm)
{
//do your code to find control using id of picture box and change the required properties
}
}

in C# WinForms, how can I get rid of the child's control bar?

I'm rewriting a program originally written in FoxPro that is used by town and school tax collectors. Unfortunately, I'm learning this as I go. The program requires windows to be modal - the users follow a specific path, and can't play with more than one window at a time. I have a MDI container form that I open. All child forms are called from there. Right now, I don't maximize the child forms, and it looks like this:
You can see the scroll bars, as it's not maximized. I don't really want them to deal with scroll bars... so I go into my form Login, the child form, and set WindowState to maximized. I get this:
The scroll bars are gone, the child window fits perfectly in the container window, but there are two control bars at the top... the main one for the container, and a second smaller one for the child form, with the second one having double controls on it. I've tried setting MaximizeBox, MinimizeBox, ShowIcon, and ControlBox to false, and have deleted the Text for the child form, and yet that bar is still there. If I click on certain buttons on the smaller bar, the duplicates go away. I'm looking for a way to get rid of the second bar, or hide the controls on it... or anything I haven't thought of that can help.
Why not use UserControls then you can add/remove them from your Main Form, they won't have the overhead that you have with Mdi Forms.
A quick and dirty example you will want to setup property's and events on the UserControl to pass data to and from your main form:
Form1
public partial class Form1 : Form
{
UserControl1 login = new UserControl1();
public Form1()
{
InitializeComponent();
login.ExitEvent += new UserControl1.ExitEventHandler(login_ExitEvent);
}
void login_ExitEvent(object sender, EventArgs e)
{
panel1.Controls.Remove(login);
}
private void button1_Click(object sender, EventArgs e)
{
panel1.Controls.Add(login);
login.BringToFront();
}
}
UserControl
public partial class UserControl1 : UserControl
{
public delegate void ExitEventHandler(object sender, EventArgs e);
public event ExitEventHandler ExitEvent;
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ExitEvent(this, new EventArgs());
}
}

Add Form to a UserControl - is this possible?

Normally, controls are being added to forms. But I need to do an opposite thing - add a Form instance to container user control.
The reason behind this is that I need to embed a third-party application into my own. Converting the form to a user control is not feasible due to complexity.
This is possible by setting the form's TopLevel property to false. Which turns it into a child window, almost indistinguishable from a UserControl. Here's a sample user control with the required code:
public partial class UserControl1 : UserControl {
public UserControl1() {
InitializeComponent();
}
public void EmbedForm(Form frm) {
frm.TopLevel = false;
frm.FormBorderStyle = FormBorderStyle.None;
frm.Visible = true;
frm.Dock = DockStyle.Fill; // optional
this.Controls.Add(frm);
}
}

Categories