reaching a function in the motherform

K

Kai Bohli

Hi all

My app is an Outlook style app - that is there's a mainform and I load usercontrols in a panel at
runtime. These usercontrols acts like forms.

What I need to do: Pass two strings into a function on the mainform (from the usercontrols)
Using public properties won't work cause the usercontrols doesn't "know" about the mainform.
For the same reason, I can't use public funtions. Getting the usercontrols parent won't work either.

I've tried to use delegates but can't get it to work. All the samples I've seen uses a class inside
the same form that uses the function. My problem is that I can't figure out how to call the delegate
from another form.

Any help are greatly appreciated !

TIA

Best wishes
Kai Bohli
(e-mail address removed)
Norway
 
J

Justin Rogers

Since you are writing a single document interface (only a single main form),
then
I would suggest using a static class (or static methods on your main form's
class)
and have the user control's access the method that way. In this manner you are
extending an API that each UserControl is capable of consuming if it needs to
interact with your application.

You keep saying that the UserControl's can't use their parent and don't know
about your main form. You should explain this a bit better, since if the
UserControl's
are specifically programmed to operate inside of your main form then they have
to have some reference to it or some way to get ahold of it. If you mean that
the
form isn't cast to your derived form, then you should be writing code that
examines
the type of the form and does the casting as appropriate.

For instance, TopLevelControl returns a Control, however, if this
TopLevelControl
were your main-form, which it probably is, you can find this out using some code
like
so:

OutlookStyleMainForm rootForm = myUserControl.TopLevelControl as
OutlookStyleMainForm;
if ( rootForm != null ) {
// We are hosted in the appropriate container, do some additional work
}
 
H

Herfried K. Wagner [MVP]

* Kai Bohli said:
My app is an Outlook style app - that is there's a mainform and I load usercontrols in a panel at
runtime. These usercontrols acts like forms.

What I need to do: Pass two strings into a function on the mainform (from the usercontrols)
Using public properties won't work cause the usercontrols doesn't "know" about the mainform.
For the same reason, I can't use public funtions. Getting the usercontrols parent won't work either.

\\\
((FooForm)this.FindForm()).Goo = "Bla";
///
 
K

Kai Bohli

Hi Justin !

Thanks for your excellent reply. In never cease to amaze me the quality, knowledge and the
willingness to share that some of you folk here shows. I've been working with Delphi for years, and
have just started out with C#. Some things are a lot easier to do in Delphi and some things are a
lot easier to do in C# / .NET. I suppose that as soon as I get a hang on it, C# will be as easy as
Delphi for everything :)

Back to your reply:

I followed your suggestion and got it to work like you wrote , which is great:
frmMain rootForm = this.TopLevelControl as frmMain;
frmMain.ShowInfo("HeaderTitle","HeaderText");

But it doesn't stop there :)

In my mainform, I got this code :
public frmMain()
{
InitializeComponent();
....
....
}

public static void ShowInfo(string Title, string Msg)
{
MessageBox.Show(Title + "\n" + Msg);
}

This also works fine, but what I need to do is display Title & msg into a richedit. Now if I call
another (private) function like this:
public static void ShowInfo(string Title, string Msg)
{
ShowHelpInfo(Title,Msg);
}

I get this message:
C:\NET Prosjekt\ProTeriaTransport\Transport\frmMain.cs(114): An object reference is required for the
nonstatic field, method, or property 'ProTeria.Transport.GUI.frmMain.ShowHelpInfo(string, string)'

If I try to refer directly to the richedit:
public static void ShowInfo(string Title, string Msg)
{
richInfo.Clear();
}
then I get this message:
C:\NET Prosjekt\ProTeriaTransport\Transport\frmMain.cs(115):
'ProTeria.Transport.GUI.frmMain.richInfo' denotes a 'field' where a 'class' was expected

I suppose I have to cast the richEdit in one way or another ?

TIA
I would suggest using a static class (or static methods on your main form's
class)
and have the user control's access the method that way. In this manner you are
extending an API that each UserControl is capable of consuming if it needs to
interact with your application.

Best wishes
Kai Bohli
(e-mail address removed)
Norway
 
K

Kai Bohli

Hi Herfried !

The first section of my reply to Justin goes for you to ! :)
This also worked great !
((frmMain)this.FindForm()).ShowHelpInfo("Test 1", "Test2");
\\\
((FooForm)this.FindForm()).Goo = "Bla";
///


Best wishes
Kai Bohli
(e-mail address removed)
Norway
 
J

Justin Rogers

Actually my reply was giving you a couple of options. Rather than using static
methods you can just use an instance method. Here is some code using what
you've posted.

frmMain rootForm = this.TopLevelControl as frmMain;
if ( rootForm != null ) {
// Sanity Check
rootForm.ShowHelpInfo(...);
}

Note in the above it appears that ShowHelpInfo is a non static field of your
form class, and that is completely fine now that you have an actual instance
of your form class. You don't need the statics at this point.

The statics were a second method for making things work in the case that you
couldn't actually find an instance of your class.


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

Kai Bohli said:
Hi Justin !

Thanks for your excellent reply. In never cease to amaze me the quality, knowledge and the
willingness to share that some of you folk here shows. I've been working with Delphi for years, and
have just started out with C#. Some things are a lot easier to do in Delphi and some things are a
lot easier to do in C# / .NET. I suppose that as soon as I get a hang on it, C# will be as easy as
Delphi for everything :)

Back to your reply:

I followed your suggestion and got it to work like you wrote , which is great:
frmMain rootForm = this.TopLevelControl as frmMain;
frmMain.ShowInfo("HeaderTitle","HeaderText");

But it doesn't stop there :)

In my mainform, I got this code :
public frmMain()
{
InitializeComponent();
...
...
}

public static void ShowInfo(string Title, string Msg)
{
MessageBox.Show(Title + "\n" + Msg);
}

This also works fine, but what I need to do is display Title & msg into a richedit. Now if I call
another (private) function like this:
public static void ShowInfo(string Title, string Msg)
{
ShowHelpInfo(Title,Msg);
}

I get this message:
C:\NET Prosjekt\ProTeriaTransport\Transport\frmMain.cs(114): An object reference is required for the
nonstatic field, method, or property
'ProTeria.Transport.GUI.frmMain.ShowHelpInfo(string, string)'
 

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