Making one user control aware of another
-
Hello, I have a problem that I'm still stuck on. I have a form with a pair of user controls. The first user control is displayed when the program runs. When a button is clicked, I want the first user control to remove itself from the form, and the second user control to appear. However, when I write the code for the button, all I can do is get the first UC to disappear; the second UC never draws itself. Someone earlier told me to instantiate an instance of the second user control in the form's constructor, and add this line into the form: this.Parent.Controls.Add(newUserControl); However, this still doesn't solve the issue of the first user control not knowing about the second user control. Can someone point me in the right direction?
-
Hello, I have a problem that I'm still stuck on. I have a form with a pair of user controls. The first user control is displayed when the program runs. When a button is clicked, I want the first user control to remove itself from the form, and the second user control to appear. However, when I write the code for the button, all I can do is get the first UC to disappear; the second UC never draws itself. Someone earlier told me to instantiate an instance of the second user control in the form's constructor, and add this line into the form: this.Parent.Controls.Add(newUserControl); However, this still doesn't solve the issue of the first user control not knowing about the second user control. Can someone point me in the right direction?
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;private System.ComponentModel.Container components = null;
public Form1() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.SuspendLayout(); this.button1.Location = new System.Drawing.Point(64, 112); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "button1"; this.button1.Click += new System.EventHandler(this.button1\_Click); this.button2.Location = new System.Drawing.Point(160, 112); this.button2.Name = "button2"; this.button2.TabIndex = 1; this.button2.Text = "button2"; this.button2.Visible = false; this.button2.Click += new System.EventHandler(this.button2\_Click); 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.button2, this.button1}); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false);
}
private void button1\_Click(object sender, System.EventArgs e)
{
button2.Visible = true;
button1.Visible = false;
}private void button2_Click(object sender, System.EventArgs e)
{
button2.Visible = false;
button1.Visible = true;
}
} -
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;private System.ComponentModel.Container components = null;
public Form1() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.SuspendLayout(); this.button1.Location = new System.Drawing.Point(64, 112); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "button1"; this.button1.Click += new System.EventHandler(this.button1\_Click); this.button2.Location = new System.Drawing.Point(160, 112); this.button2.Name = "button2"; this.button2.TabIndex = 1; this.button2.Text = "button2"; this.button2.Visible = false; this.button2.Click += new System.EventHandler(this.button2\_Click); 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.button2, this.button1}); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false);
}
private void button1\_Click(object sender, System.EventArgs e)
{
button2.Visible = true;
button1.Visible = false;
}private void button2_Click(object sender, System.EventArgs e)
{
button2.Visible = false;
button1.Visible = true;
}
}How does this help answer the question? Where are the user controls?
-
How does this help answer the question? Where are the user controls?
If it helps, here my code: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace Copper { public class frmInit : System.Windows.Forms.Form { private System.Windows.Forms.PictureBox pictureBox1; private Copper.UCInit ucInit1; private System.ComponentModel.Container components = null; public frmInit() { InitializeComponent(); UCCreate newPanel = new UCCreate(); newPanel.Location = new Point(376, 8); newPanel.Size = new Size (280, 140); this.Parent.Controls.Add(newPanel); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmInit)); this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.ucInit1 = new Copper.UCInit(); this.SuspendLayout(); this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image"))); this.pictureBox1.Location = new System.Drawing.Point(0, 0); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(375, 256); this.pictureBox1.TabIndex = 0; this.pictureBox1.TabStop = false; // // ucInit1 // this.ucInit1.Location = new System.Drawing.Point(376, 8); this.ucInit1.Name = "ucInit1"; this.ucInit1.Size = new System.Drawing.Size(280, 184); this.ucInit1.TabIndex = 0; this.ucInit1.Load += new System.EventHandler(this.ucInit1_Load); // // frmInit // this.
-
If it helps, here my code: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace Copper { public class frmInit : System.Windows.Forms.Form { private System.Windows.Forms.PictureBox pictureBox1; private Copper.UCInit ucInit1; private System.ComponentModel.Container components = null; public frmInit() { InitializeComponent(); UCCreate newPanel = new UCCreate(); newPanel.Location = new Point(376, 8); newPanel.Size = new Size (280, 140); this.Parent.Controls.Add(newPanel); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmInit)); this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.ucInit1 = new Copper.UCInit(); this.SuspendLayout(); this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image"))); this.pictureBox1.Location = new System.Drawing.Point(0, 0); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(375, 256); this.pictureBox1.TabIndex = 0; this.pictureBox1.TabStop = false; // // ucInit1 // this.ucInit1.Location = new System.Drawing.Point(376, 8); this.ucInit1.Name = "ucInit1"; this.ucInit1.Size = new System.Drawing.Size(280, 184); this.ucInit1.TabIndex = 0; this.ucInit1.Load += new System.EventHandler(this.ucInit1_Load); // // frmInit // this.
OK here's a quick and dirty way :) Just make the button public. Just add the following to the main form:
ucInit1.button.Clicked += new EventHandler(ShowUCCreate);
....
void ShowUCCreate(object sender, EventArgs e)
{
UCCreate newPanel = new UCCreate();
newPanel.Location = new Point(376, 8);
newPanel.Size = new Size (280, 140);
this.Parent.Controls.Add(newPanel);
}You should get the idea. Once you understand events they make life alot easier. leppie::AllocCPArticle(Generic DFA State Machine for .NET);
-
OK here's a quick and dirty way :) Just make the button public. Just add the following to the main form:
ucInit1.button.Clicked += new EventHandler(ShowUCCreate);
....
void ShowUCCreate(object sender, EventArgs e)
{
UCCreate newPanel = new UCCreate();
newPanel.Location = new Point(376, 8);
newPanel.Size = new Size (280, 140);
this.Parent.Controls.Add(newPanel);
}You should get the idea. Once you understand events they make life alot easier. leppie::AllocCPArticle(Generic DFA State Machine for .NET);
-
I think that should work. Is there a good primer on Events and the EventHandler() method in general, somewhere?
frogb0x wrote: Is there a good primer on Events and the EventHandler() method in general, somewhere? The ones in MSDN are rather difficult to grasp. It took me quite a while to figure out what they are and why I need them. To understand events you need to understand delegates. In fact an event is really just a very limited delegate. A delegate (or function pointer) is basically a function/methods signature. WHat I mean by this is, is that it looks basically like a method but it has the delegate keyword. Think of this as a placeholder for a method. Example:
delegate void FooHandler(string name);
class ABC
{
public FooHandler Foo;public void InvokeFoo()
{
Foo("Hello from ABC");
}
}class XYZ
{
static void MyFoo(string name)
{
Console.WriteLine(name);
}static void Main()
{
ABC abc = new ABC();
abc.Foo = new FooHandler(MyFoo);
abc.InvokeFoo();
}
}Now step into this with the debugger, step into every function (F11). This will show you exactly how it works. This allows you specify a method in a different place or you can change them dynamically. Once you have grasped this, I suggest you look at the MSDN documentation on events. Hope this helps :) leppie::AllocCPArticle(Generic DFA State Machine for .NET);