A container problem

C

Cem Louis

Hi Bill,

I think I have some same problem as I wrote in codeproject boards. I put a
textbox on my dialog and I want to send the text to the child's label. I
think it is the same problem(I work only in SDI systems before, these MDI
systems are new for me...). If you have time can you quick check the code? I
am sending my Parent, Dialog and Child.cs files...

Thank you very much,
Cem Louis

/////Parent.cs/////

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Test1
{
/// <summary>
/// Summary description for Parent.
/// </summary>
public class Parent : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Parent()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (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.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2});
this.menuItem1.Text = "Show Dialog";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "Show!";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// Parent
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.IsMdiContainer = true;
this.Menu = this.mainMenu1;
this.Name = "Parent";
this.Text = "Parent";

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Parent());
}

private void menuItem2_Click(object sender, System.EventArgs e)
{
Test1.Dialog Dialog = new Test1.Dialog(this);
Dialog.Show();
}
}
}

///// Dialog.cs /////

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Test1
{
/// <summary>
/// Summary description for Dialog.
/// </summary>
public class Dialog : System.Windows.Forms.Form
{
private Form MainForm;

private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public string sample_text;

public Dialog(Form frmMain)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
this.MainForm = frmMain;
}
public Dialog()
{
InitializeComponent();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(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.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 104);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "Show Child";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(96, 64);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// Dialog
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox1,
this.button1});
this.Name = "Dialog";
this.Text = "Dialog";
this.ResumeLayout(false);

}
#endregion

private void button1_Click(object sender, System.EventArgs e)
{
this.sample_text = this.textBox1.Text;
Test1.Child chform = new Test1.Child();
chform.MdiParent = this.MainForm;
chform.Show ();
}
}
}

///// Child.cs /////

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Test1
{
/// <summary>
/// Summary description for Child.
/// </summary>
public class Child : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Label label1;
Test1.Dialog Dialog = new Test1.Dialog();

public Child()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(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.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(56, 80);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(168, 48);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// Child
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1});
this.Name = "Child";
this.Text = "Child";
this.Paint += new
System.Windows.Forms.PaintEventHandler(this.Child_Paint);
this.ResumeLayout(false);

}
#endregion

private void Child_Paint(object sender, PaintEventArgs e)
{
if(Dialog.sample_text == "")
{
this.label1.Text = "No text written";
}
else
{
this.label1.Text = Dialog.sample_text;
}

}
}
}
 
M

msnews.microsoft.com

Your problem is that in the Child you declare (and instantiate) a new
dialog - meaning you will never find the original dialog when you try to
read it's text.
Instead you should create a new constructor in your child, and transfer the
string as a parameter to this constructor - or the Dialog if that's better.

/Freddy

Cem Louis said:
Hi Bill,

I think I have some same problem as I wrote in codeproject boards. I put a
textbox on my dialog and I want to send the text to the child's label. I
think it is the same problem(I work only in SDI systems before, these MDI
systems are new for me...). If you have time can you quick check the code? I
am sending my Parent, Dialog and Child.cs files...

Thank you very much,
Cem Louis

/////Parent.cs/////

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Test1
{
/// <summary>
/// Summary description for Parent.
/// </summary>
public class Parent : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Parent()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (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.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2});
this.menuItem1.Text = "Show Dialog";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "Show!";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// Parent
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.IsMdiContainer = true;
this.Menu = this.mainMenu1;
this.Name = "Parent";
this.Text = "Parent";

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Parent());
}

private void menuItem2_Click(object sender, System.EventArgs e)
{
Test1.Dialog Dialog = new Test1.Dialog(this);
Dialog.Show();
}
}
}

///// Dialog.cs /////

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Test1
{
/// <summary>
/// Summary description for Dialog.
/// </summary>
public class Dialog : System.Windows.Forms.Form
{
private Form MainForm;

private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public string sample_text;

public Dialog(Form frmMain)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
this.MainForm = frmMain;
}
public Dialog()
{
InitializeComponent();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(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.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 104);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "Show Child";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(96, 64);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// Dialog
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox1,
this.button1});
this.Name = "Dialog";
this.Text = "Dialog";
this.ResumeLayout(false);

}
#endregion

private void button1_Click(object sender, System.EventArgs e)
{
this.sample_text = this.textBox1.Text;
Test1.Child chform = new Test1.Child();
chform.MdiParent = this.MainForm;
chform.Show ();
}
}
}

///// Child.cs /////

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Test1
{
/// <summary>
/// Summary description for Child.
/// </summary>
public class Child : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Label label1;
Test1.Dialog Dialog = new Test1.Dialog();

public Child()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(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.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(56, 80);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(168, 48);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// Child
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1});
this.Name = "Child";
this.Text = "Child";
this.Paint += new
System.Windows.Forms.PaintEventHandler(this.Child_Paint);
this.ResumeLayout(false);

}
#endregion

private void Child_Paint(object sender, PaintEventArgs e)
{
if(Dialog.sample_text == "")
{
this.label1.Text = "No text written";
}
else
{
this.label1.Text = Dialog.sample_text;
}

}
}
}
 
C

Cem Louis

Hi Freddy,

If you have time can you give a sample? (I am a total newbie about these MDI
type of things)... If you don't have time, it's not a problem.

Thanx anyway,
Cem Louis

msnews.microsoft.com said:
Your problem is that in the Child you declare (and instantiate) a new
dialog - meaning you will never find the original dialog when you try to
read it's text.
Instead you should create a new constructor in your child, and transfer the
string as a parameter to this constructor - or the Dialog if that's better.

/Freddy

Cem Louis said:
Hi Bill,

I think I have some same problem as I wrote in codeproject boards. I put a
textbox on my dialog and I want to send the text to the child's label. I
think it is the same problem(I work only in SDI systems before, these MDI
systems are new for me...). If you have time can you quick check the
code?
I
am sending my Parent, Dialog and Child.cs files...

Thank you very much,
Cem Louis

/////Parent.cs/////

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Test1
{
/// <summary>
/// Summary description for Parent.
/// </summary>
public class Parent : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Parent()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (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.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2});
this.menuItem1.Text = "Show Dialog";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "Show!";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// Parent
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.IsMdiContainer = true;
this.Menu = this.mainMenu1;
this.Name = "Parent";
this.Text = "Parent";

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Parent());
}

private void menuItem2_Click(object sender, System.EventArgs e)
{
Test1.Dialog Dialog = new Test1.Dialog(this);
Dialog.Show();
}
}
}

///// Dialog.cs /////

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Test1
{
/// <summary>
/// Summary description for Dialog.
/// </summary>
public class Dialog : System.Windows.Forms.Form
{
private Form MainForm;

private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public string sample_text;

public Dialog(Form frmMain)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
this.MainForm = frmMain;
}
public Dialog()
{
InitializeComponent();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(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.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 104);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "Show Child";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(96, 64);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// Dialog
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox1,
this.button1});
this.Name = "Dialog";
this.Text = "Dialog";
this.ResumeLayout(false);

}
#endregion

private void button1_Click(object sender, System.EventArgs e)
{
this.sample_text = this.textBox1.Text;
Test1.Child chform = new Test1.Child();
chform.MdiParent = this.MainForm;
chform.Show ();
}
}
}

///// Child.cs /////

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Test1
{
/// <summary>
/// Summary description for Child.
/// </summary>
public class Child : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Label label1;
Test1.Dialog Dialog = new Test1.Dialog();

public Child()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(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.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(56, 80);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(168, 48);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// Child
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1});
this.Name = "Child";
this.Text = "Child";
this.Paint += new
System.Windows.Forms.PaintEventHandler(this.Child_Paint);
this.ResumeLayout(false);

}
#endregion

private void Child_Paint(object sender, PaintEventArgs e)
{
if(Dialog.sample_text == "")
{
this.label1.Text = "No text written";
}
else
{
this.label1.Text = Dialog.sample_text;
}

}
}
}
 

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