Hi David,
Because the user control is developed for general use, you can not call the
parent form’s method explicitly.
However, you can use delegate to achieve this.
You can implement a delegate which points to your cmdOkay_Click() method in
parent form and pass it to your user control to invoke.
My sample code is listed below:
"Usercontrol.cs"
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace test
{
public class UserControl1 : System.Windows.Forms.UserControl
{
private Delegate pointer;
private System.ComponentModel.Container components = null;
public UserControl1()
{
InitializeComponent();
}
public UserControl1(Delegate p)
{
InitializeComponent();
this.pointer =p;
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component Designer generated code
private void InitializeComponent()
{
this.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.Name = "UserControl1";
this.Click += new System.EventHandler(this.UserControl1_Click);
this.Load += new System.EventHandler(this.UserControl1_Load);
this.DoubleClick += new
System.EventHandler(this.UserControl1_DoubleClick);
}
#endregion
private void UserControl1_Load(object sender, System.EventArgs e)
{
}
private void UserControl1_DoubleClick(object sender, System.EventArgs e)
{
}
private void UserControl1_Click(object sender, System.EventArgs e)
{
pointer.DynamicInvoke (null);
}
}
}
"Form1.cs"
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace d
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private test.UserControl1 userControl11;
private System.ComponentModel.Container components = null;
public delegate void delegatecall();
private event delegatecall pointer;
public void cmdOkay_Click()
{
label1.Text ="parent method called";
}
public Form1()
{
pointer+=new delegatecall(this.cmdOkay_Click );
InitializeComponent();
}
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()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
this.label1.Location = new System.Drawing.Point(96, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 64);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(296, 238);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
}
}
Hope this helps.
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
| From: " David N" <
[email protected]>
| References: <Okb##
[email protected]>
<
[email protected]>
| Subject: Re: How to call a form-Parent function that does not even yet to
develop
| Date: Wed, 30 Jul 2003 08:29:27 -0700
| Lines: 90
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <
[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: pat-50.bel.netiq.com 65.219.170.50
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:172992
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Mark,
|
| Thanks for you reply.
|
| Can you show me an example of how to raise an event in my control. Or can
| you direct me to a URL that has an example of it?
|
| Thanks.
|
|
| | > David
| > You have to have your control raise an event and the parent form will
have
| > to handle that event. Its the same as the button in your example.
| > Microsoft cant know how every form will handle the button so they raise
| the
| > button click event and you will write a handler. In the same way, you
| wont
| > know how your control is going to be used, so you have to raise an event
| and
| > then handle it in the form as you want it used
![Smile :) :)](/styles/default/custom/smilies/smile.gif)
| >
| > Hope it helps
| > Mark
| >
| > | > >
| > > Hi All,
| > >
| > > I just wonder if in C#, I can develop a user defined control that can
| call
| > > its parent function which is not yet developed. For example, how do I
| > make
| > > my user control call a to-be-developed-function cmdOkay_Click()
function
| > as
| > > described below.
| > >
| > > 1. Create an user control that contains an OK button as below
| > >
| > > Public Class MyButton:System.Windows.Form.UserControl
| > > {
| > > protected System.Windows.Form.Button cmdOkay;
| > > .....
| > > .....
| > > }
| > >
| > > 2. In the cmdOkay_Click() event, call a function in the parent (the
| > parent
| > > is a form whichever this control will be placed on)
| > >
| > > Public Class MyButton:System.Windows.Form.UserControl
| > > {
| > > protected System.Windows.Form.Button cmdOkay;
| > > .....
| > > .....
| > >
| > > private void cmdOkay_Click(object sender,System.EventArgs e)
| > > {
| > >
| > > // This one is easy, because the ParentForm.Close()
function
| > is
| > > known.
| > > this.ParentForm.Close();
| > >
| > >
| > > // But I want to do this.
| > > this.ParentForm.cmdOkay_Click();
| > >
| > >
| > > // Or even better, passing the parameters
| > >
| > > this.ParentForm.cmdOkay_Click(object
sender,System.EventArgs
| > e)
| > >
| > >
| > >
| > >
| > > }
| > > }
| > >
| > >
| > > Thanks.
| > >
| > >
| >
| >
|
|
|