How to get the main window object from outside the object?

F

Funnyman

I'm pretty new to .net Windows Forms, but I've done a lot of searching
and can't find an answer to this question. Here's what I'm trying to
do:

main() {
Form1 myForm = new Form1();
Application::Run(myForm);
}

Function1()
{
// I need to modify elements of myForm either
// through a class function or by manipulating the
// object itself.
return;
}


Basically the app has the main window thread (Application::Run) and
then a listener thread for events coming in from other applications,
and those events need to manipulate the data being shown in the main
window.

Right now, I have hacked it up so that I have a static Form1 pointer
in the Form1 class, and whenever an event is received I do the
following:
Form1::thisForm->Function();
but, like I said, this feels like a hack.

Any suggestions as to the correct way of doing this would be greatly
appreciated. Thanks very much!

--Mike
 
O

Oliver Sturm

Hello Funnyman,
Basically the app has the main window thread (Application::Run) and
then a listener thread for events coming in from other applications,
and those events need to manipulate the data being shown in the main
window.

There's no really good answer to your question, because you're going wrong
here. You shouldn't be manipulating data that is encapsulated in the main
from class from outside that class. A basic OO paradigm. If you need have
that data changed based on an event, then you should catch that event
inside the class. If that is totally impossible (and I have to admit I'm
not entirely sure why it would be), you should introduce a public
interface to the class that holds the data and call those public methods
to have the necessary modifications made.
Form1::thisForm->Function();

Use C#, man - that looks just funny in 2007 :) Interesting, I never
noticed back in 1996... just joking, in a way...


Oliver Sturm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top