473,549 Members | 2,726 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 11864
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
2999
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
3500
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
1997
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
36088
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
2913
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
17435
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
2166
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
1692
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
1892
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
2844
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
7524
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
7451
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7812
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5372
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
5089
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
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1944
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
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
766
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.