473,581 Members | 2,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to call a form-Parent function that does not even yet to develop


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.
Nov 15 '05 #1
3 11868
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.

Nov 15 '05 #2
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.


Nov 15 '05 #3

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.
| > >
| > >
| >
| >
|
|
|

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3000
by: rawCoder | last post by:
Hi I am having this InvalidOperationException with message Cannot call Invoke or InvokeAsync on a control until the window handle has been created This is raised when i try to invoke a method with arguments I need to do this as the method is being called Asynchronously from elsewhere using delegate's BeginInvoke method. The form is well...
4
3505
by: Martijn Mulder | last post by:
I have a menu option 'Open...' that opens an OpenFileDialog to select an image file to open. Than I need to call Form.Invalidate() to clear the Form and display the image. This happens in a seperate file-handling class. The problem is that I cannot get access to the Form. The delegate passes in an 'object' that I can cast to...
1
2000
by: tony | last post by:
Hello!! I have an application that consist of several windows forms. Lets call these A,B and C for simplicity. I have one main meny where the user can choose window form A or B or C. When a user bring up a window for the first time I create an instance otherwise I just set visible = true. When a user brings up a window form for example...
8
36098
by: hoofbeats95 | last post by:
I don't think this should be this complicated, but I can't figure it out. I've worked with C# for several years now, but in a web environment, not with windows form. I have a form with a query button on it. If the query returns multiple results, a new window is opened with a grid containing the results. When the user double clicks on the...
6
2917
by: Ryan Liu | last post by:
Hi, I have a form, with some user control in it, usually hung up when I call form.Show(). I debug to there, I found in Visual Studio 2003, the call stack is empty! And CPU is not busy at all. Can anyonoe give me idea about what is happening?
7
17441
by: jp2code | last post by:
I've got a form that posts to itself when submitted. Right now, after the form is submitted, the code checks that all of its fields are valid, then processes the information. If all goes well, visitors are then redirected using Response.Redirect("thanks.asp"). What I'd like to do is replace Response.Redirect("thanks.asp") with something...
7
2170
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I am trying to see if I can call a Library remotely. The library contains a Form that I want to display then pass back some data to user that called this form remotely. I have it working some-what. I am able to call form remotely and return data to client but somewhere after closing remote form and returning data - I get a Windows...
5
1693
by: tcomer | last post by:
Hello, I'm working on an application that grabs some data from the web via HttpWebRequest. I'm using a local objects method to get the data, but the problem is that my form doesn't load until this method has finished what it's doing.. which takes about 10-15 seconds. Here is what I have: Using System.Threading;
2
1893
by: Fabio Mastria | last post by:
My application has to call a webpage with some hidden field, fill their values with some parameters and then call the submit to the form which has the action property set to a page of another web application. all of this, did via javascript. how can i send the data to fill the hidden fields to this web form? Please help me, thank you!...
6
2846
by: amitjaura | last post by:
My problem is that on button click of one form(Form1) I want to change the background color of checkbox in another form(Form2) ..One way I found is to call the form and pass the variable through constructor..Bt I don't want to create new form...The form in which I want 2 send the key is already opened n I don want to call the constructor.. Is it...
0
7868
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8149
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6553
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5674
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5364
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3805
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1403
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1138
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.