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.Collecti ons;
using System.Componen tModel;
using System.Drawing;
using System.Data;
using System.Windows. Forms;
namespace test
{
public class UserControl1 : System.Windows. Forms.UserContr ol
{
private Delegate pointer;
private System.Componen tModel.Containe r components = null;
public UserControl1()
{
InitializeCompo nent();
}
public UserControl1(De legate p)
{
InitializeCompo nent();
this.pointer =p;
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
#region Component Designer generated code
private void InitializeCompo nent()
{
this.BackColor = System.Drawing. SystemColors.Ac tiveCaption;
this.Name = "UserContro l1";
this.Click += new System.EventHan dler(this.UserC ontrol1_Click);
this.Load += new System.EventHan dler(this.UserC ontrol1_Load);
this.DoubleClic k += new
System.EventHan dler(this.UserC ontrol1_DoubleC lick);
}
#endregion
private void UserControl1_Lo ad(object sender, System.EventArg s e)
{
}
private void UserControl1_Do ubleClick(objec t sender, System.EventArg s e)
{
}
private void UserControl1_Cl ick(object sender, System.EventArg s e)
{
pointer.Dynamic Invoke (null);
}
}
}
"Form1.cs"
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
namespace d
{
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.Label label1;
private test.UserContro l1 userControl11;
private System.Componen tModel.Containe r components = null;
public delegate void delegatecall();
private event delegatecall pointer;
public void cmdOkay_Click()
{
label1.Text ="parent method called";
}
public Form1()
{
pointer+=new delegatecall(th is.cmdOkay_Clic k );
InitializeCompo nent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeCompo nent()
{
this.label1 = new System.Windows. Forms.Label();
this.SuspendLay out();
this.label1.Loc ation = new System.Drawing. Point(96, 40);
this.label1.Nam e = "label1";
this.label1.Siz e = new System.Drawing. Size(96, 64);
this.label1.Tab Index = 0;
this.label1.Tex t = "label1";
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(296, 238);
this.Controls.A dd(this.label1) ;
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHan dler(this.Form1 _Load);
this.ResumeLayo ut(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run (new Form1());
}
private void Form1_Load(obje ct sender, System.EventArg s 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" <dq*****@netiq. com>
| References: <Ok************ *@TK2MSFTNGP10. phx.gbl>
<OI************ **@TK2MSFTNGP12 .phx.gbl>
| 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: <Os************ **@TK2MSFTNGP09 .phx.gbl>
| Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| NNTP-Posting-Host: pat-50.bel.netiq.co m 65.219.170.50
| Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP09.phx.g bl
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1729 92
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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.
|
|
| "Mark" <f1*****@hotmai l.com> wrote in message
| news:OI******** ******@TK2MSFTN GP12.phx.gbl...
| > 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 :)
| >
| > Hope it helps
| > Mark
| >
| > " David N" <dq*****@netiq. com> wrote in message
| > news:Ok******** *********@TK2MS FTNGP10.phx.gbl ...
| > >
| > > 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.U serControl
| > > {
| > > 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.U serControl
| > > {
| > > protected System.Windows. Form.Button cmdOkay;
| > > .....
| > > .....
| > >
| > > private void cmdOkay_Click(o bject sender,System.E ventArgs e)
| > > {
| > >
| > > // This one is easy, because the ParentForm.Clos e()
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.E ventArgs
| > e)
| > >
| > >
| > >
| > >
| > > }
| > > }
| > >
| > >
| > > Thanks.
| > >
| > >
| >
| >
|
|
|