Windows Forms - Opening forms within forms

D

Dan Tallent

I just realized I used the MdiParent property for setting this location.
This was something I was playing with. It should just be Parent


Dan Tallent said:
I appreciate the help.

I currently have code that is using a child form (ZipCode) within the
parent (Customer)

When the child closes the parent comes back to life. The child has its
FormBorderStyle = FixedToolWindow and cannot move outside the area of the
parent (Customer).
This approach seems to be working. It also allows me to use the ZipCode
form in other locations within the program such as for a Vendors address.
If the Customer form is minimized or moved the ZipCode form travels with
it. I am thinking of using an Inherited version of the form class so I can
add the Active method for use in all of the forms.

I have not attempted to Inherit from a control before and I'm not sure how
I will be able to use it within the IDE.....yet.

Thanks everyone for your help. If you see any problems with this
approach please feel free to point them out.

Thanks again,
Dan


//---------------------------------------------------------------------------

private void button4_Click(object sender, EventArgs e)

{

this.Active(false);



Child1 C = new Child1();

C.FormClosed+=new FormClosedEventHandler(C_FormClosed);

C.TopLevel = false;


C.Parent = this;

C.Top = (this.MdiParent.Top + (this.Height - C.Height) / 2);

C.Left = (this.MdiParent.Left + (this.Width - C.Width) / 2);



C.BringToFront();

C.Show();

}



void C_FormClosed(object sender, FormClosedEventArgs e)

{


this.Active(true);


}

private void Active(bool status)

{

foreach (Control oControl in this.Controls)

{

oControl.Enabled = status;


}

}

//---------------------------------------------------------------------------









I think the loop with have to have a condition within it to ignore child
forms. I haven't written this code yet, but it seems likely.
Thanks for the help
Dan













I dont know off the top of my head, but I think you could place all
Customer controls in a container, leave the ZIP "modal" panel outside
that container and when you need to disable the Customer controls, you
only disable their container... one line. Now, again, I am not sure of
that and I don't currently have an easy way of checking...
Thinking about it, even if disabling the container doesn't work, the
fact they all belong to the same container (and the ZIP controls
don't) should make your looping strategy straight forward.- Hide quoted
text -

- Show quoted text -

While I agree to a degree with Peter Duniho on the possibility of
cluttered screen, I think that keeping the ZIP as a panel inside the
form diminishes that clutter.

And yes, the panel does have Enabled property.

Here's a working example. button1 is on the "main" form and switches
to modal panel, button2 is on the modal panel and switches back to
main form view. Of course you can play with panel positions.

using System;
using System.Windows.Forms;

namespace ModalPanel
{
public class Form1 : Form
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
ShowModalPanel(true);
}

private void button2_Click(object sender, EventArgs e)
{
ShowModalPanel(false);
}

private void ShowModalPanel(bool show)
{
panelMainForm.Enabled = !show;
panelModal.Visible = show;
}

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panelMainForm = new System.Windows.Forms.Panel();
this.panelModal = new System.Windows.Forms.Panel();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button1 = new System.Windows.Forms.Button();
this.checkBox2 = new System.Windows.Forms.CheckBox();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.panelMainForm.SuspendLayout();
this.panelModal.SuspendLayout();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// panelMainForm
//
this.panelMainForm.Controls.Add(this.label1);
this.panelMainForm.Controls.Add(this.button1);
this.panelMainForm.Controls.Add(this.groupBox1);
this.panelMainForm.Location = new System.Drawing.Point(34, 30);
this.panelMainForm.Name = "panelMainForm";
this.panelMainForm.Size = new System.Drawing.Size(292, 361);
this.panelMainForm.TabIndex = 0;
//
// panelModal
//
this.panelModal.Controls.Add(this.button2);
this.panelModal.Controls.Add(this.textBox2);
this.panelModal.Controls.Add(this.checkBox1);
this.panelModal.Location = new System.Drawing.Point(332, 124);
this.panelModal.Name = "panelModal";
this.panelModal.Size = new System.Drawing.Size(200, 141);
this.panelModal.TabIndex = 0;
this.panelModal.Visible = false;
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Location = new System.Drawing.Point(37, 74);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(80, 17);
this.checkBox1.TabIndex = 0;
this.checkBox1.Text = "checkBox1";
this.checkBox1.UseVisualStyleBackColor = true;
//
// groupBox1
//
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Controls.Add(this.comboBox1);
this.groupBox1.Controls.Add(this.checkBox2);
this.groupBox1.Location = new System.Drawing.Point(15, 44);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(261, 125);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "groupBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(192, 295);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// checkBox2
//
this.checkBox2.AutoSize = true;
this.checkBox2.Location = new System.Drawing.Point(7, 20);
this.checkBox2.Name = "checkBox2";
this.checkBox2.Size = new System.Drawing.Size(80, 17);
this.checkBox2.TabIndex = 0;
this.checkBox2.Text = "checkBox2";
this.checkBox2.UseVisualStyleBackColor = true;
//
// comboBox1
//
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(7, 44);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 21);
this.comboBox1.TabIndex = 1;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(15, 13);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(97, 13);
this.label1.TabIndex = 2;
this.label1.Text = "label1 label1 label1";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(7, 72);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 2;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(37, 42);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 20);
this.textBox2.TabIndex = 1;
//
// button2
//
this.button2.Location = new System.Drawing.Point(41, 108);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 2;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(543, 433);
this.Controls.Add(this.panelModal);
this.Controls.Add(this.panelMainForm);
this.Name = "Form1";
this.Text = "Form1";
this.panelMainForm.ResumeLayout(false);
this.panelMainForm.PerformLayout();
this.panelModal.ResumeLayout(false);
this.panelModal.PerformLayout();
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Panel panelMainForm;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.CheckBox checkBox2;
private System.Windows.Forms.Panel panelModal;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.Button button2;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top