473,396 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Calling Classes in MainWindow from MDI Child Window

Hi, I'm new to c# and come from a C / C++ background.
My MainWindow class has a function called DrawChart.
I open an MDI Child Window and want it to make a call from
its class MDIChild to DrawChart which is the MainWindow
class. Also, I want the chart counter to be incremented
on each call to DrawChart, without being set back to its
original value.

From my MDIChild class, If I say:

MainWindow xxx = new MainWindow ();
xxx.DrawChart ();

My counter gets re-initialized every time I call DrawChart.
Can anyone provide an answer ( in code ) if possible.

Thanks in advance.
Shaun Pudwell.

Nov 15 '05 #1
5 4489
Hi Shaun,

"Shaun Pudwell" <sh***********@tiscali.co.uk> wrote in message
news:04****************************@phx.gbl...
Hi, I'm new to c# and come from a C / C++ background.
My MainWindow class has a function called DrawChart.
I open an MDI Child Window and want it to make a call from
its class MDIChild to DrawChart which is the MainWindow
class. Also, I want the chart counter to be incremented
on each call to DrawChart, without being set back to its
original value.

From my MDIChild class, If I say:

MainWindow xxx = new MainWindow ();
xxx.DrawChart ();

My counter gets re-initialized every time I call DrawChart.
Can anyone provide an answer ( in code ) if possible.


Hmm...The problem is apparent, but the explanation is not. You are
creating a new instance of MainWindow each time and calling DrawChart on
that. I assume that you *want* to call the DrawChart method on the instance
of the MainWindow class that encapsulates the visible MDI parent form (which
is, in turn, parent to the MDI child windows). This can most easily be done
like this:

// Within an MDI child window class.
MainWindow xxx = (MainWindow)this.MdiParent;
xxx.DrawChart ();

Regards,
Dan
Nov 15 '05 #2

Hi,

Thanks, I will try that one.

Didn't see anything like this in either of the two books I have nor in
the Scribble demo!
Regards

Shaun Pudwell.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3
Shaun,

While there is certainly nothing wrong with this approach, you will
now have a circular reference. The MDIParent object has a reference
to the MDIChild object and the MDIChild object has a reference to the
parent. You have to be Very careful with circular references.

Another approach would be to put an event in the MDIChild class. In
the MDIParent class write an event handler that calls or does your
drawing task. Then in the child you simply raise the event whenever
you want the drawing to happen.

This is just another approach FYI.
Shaun Pudwell <sh***********@tiscali.co.uk> wrote in message news:<OX*************@TK2MSFTNGP12.phx.gbl>...
Hi,

Thanks, I will try that one.

Didn't see anything like this in either of the two books I have nor in
the Scribble demo!
Regards

Shaun Pudwell.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #4
Hi Doug,

"Doug Taylor" <do*********@accipitersoftware.com> wrote in message
news:e1**************************@posting.google.c om...
Shaun,

While there is certainly nothing wrong with this approach, you will
now have a circular reference. The MDIParent object has a reference
to the MDIChild object and the MDIChild object has a reference to the
parent. You have to be Very careful with circular references.

Another approach would be to put an event in the MDIChild class. In
the MDIParent class write an event handler that calls or does your
drawing task. Then in the child you simply raise the event whenever
you want the drawing to happen.

This is just another approach FYI.


A few things:

1. What I suggested to Shaun will not create a circular reference
situation. In the code snippet, the MainWindow variable is declared local to
the routine. Once the routine exits, the variable goes out of scope and the
reference is no more.

2. If Shaun *were* to create a circular reference situation (say he
assigns a reference to the MainWindow instance to a *member* variable of the
child window instance) this would still be no big deal. The garbage
collector implementation in .NET will still reclaim both instances when they
are no longer "reachable" by the application.

3. Subscribing to an event creates a reference from the event source
object to the event subscriber object. Thus your suggestion would, in fact,
create a circular reference situation. Fortunately this is no big deal (see
#2).

Regards,
Dan
Nov 15 '05 #5
Daniel,

All very good points. Thanks.

"Daniel Pratt" <ko******************@hotmail.com> wrote in message news:<ua**************@TK2MSFTNGP11.phx.gbl>...
Hi Doug,

"Doug Taylor" <do*********@accipitersoftware.com> wrote in message
news:e1**************************@posting.google.c om...
Shaun,

While there is certainly nothing wrong with this approach, you will
now have a circular reference. The MDIParent object has a reference
to the MDIChild object and the MDIChild object has a reference to the
parent. You have to be Very careful with circular references.

Another approach would be to put an event in the MDIChild class. In
the MDIParent class write an event handler that calls or does your
drawing task. Then in the child you simply raise the event whenever
you want the drawing to happen.

This is just another approach FYI.


A few things:

1. What I suggested to Shaun will not create a circular reference
situation. In the code snippet, the MainWindow variable is declared local to
the routine. Once the routine exits, the variable goes out of scope and the
reference is no more.

2. If Shaun *were* to create a circular reference situation (say he
assigns a reference to the MainWindow instance to a *member* variable of the
child window instance) this would still be no big deal. The garbage
collector implementation in .NET will still reclaim both instances when they
are no longer "reachable" by the application.

3. Subscribing to an event creates a reference from the event source
object to the event subscriber object. Thus your suggestion would, in fact,
create a circular reference situation. Fortunately this is no big deal (see
#2).

Regards,
Dan

Nov 15 '05 #6

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

Similar topics

0
by: Cyril Vi?ville | last post by:
Hello everybody, I would like to know how to block the destroy of the MainWindow with the cross and with right click and close? I have actually a basic start : my $window = MainWindow->new;...
2
by: Reply Via Newsgroup | last post by:
Folks, I was sure this could be done - or at least sure I had seen reference to it at some point or other - but now, after having gone for a cup of tea, am begining to doubt my original belief....
0
by: Raffi B. | last post by:
I have a form named MainWindow in my project. Visual Studio C# Express automatically creates MainWindow.Designer.cs and MainWindow.resx as partial classes on MainWindow. I am trying to add a new C#...
0
by: Marcus | last post by:
How can I access the windows that are not the current mainwindow. For instance with Internet Explorer (iexplore) one can create several browser windows within the same "instance" of iexplore. I...
3
by: Opa | last post by:
Hi , I have a form with javasript which launches a popup via the showModalDialog() method. I get the dialog to open, now I am trying to first get a reference to the calling form from the popup...
4
by: Bugs | last post by:
Hi, I wonder if anyone can help me out. I'm building a vb.net application that has a form with a panel that contains several other sub forms (as a collection of controls). What I'm wanting to...
1
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
The code below is pretty simple. Calling Talker() in the parent returns "Parent", and calling Talker() in the child returns "Child". I'm wondering how I can modify the code so that a call to the...
0
by: Phil Thompson | last post by:
On Sunday 04 May 2008, Lance Gamet wrote: I think QMainWindow.setCentralWidget() is what you are looking for. The eric4 IDE probably covers most things you'll ever need. Phil
1
by: nupuragr82 | last post by:
I have a parent form and on button click I am calling a child page where i have a textbox and a button. On button click of child form I am passing the value of the Textbox to the Textbox in parent...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.