StatusBar message from MDI windows

  • Thread starter John J. Hughes II
  • Start date
J

John J. Hughes II

I have an MDI application and would like to display messages to the status
bar of he parent window. I am really not happy about putting a status bar
on every MDI window and would prefer it to only be on the main window.

I have written the below function but think there must be a better way, is
there?

public static void WriteStatusBar(Form frm, string msg)
{
try
{
if(frm != null && frm.Parent != null && frm.Parent.Parent != null)
{
foreach(object c in frm.Parent.Parent.Controls)
{
if(c.GetType().ToString().EndsWith("StatusBar"))
{
StatusBar sb = (StatusBar)c;
sb.Panels[0].Text = msg;
break;
}
}
}
}
catch(Exception stEx)
{ System.Diagnostics.Debug.WriteLine(stEx.Message); }
}


Regards,
John
 
N

Nicholas Paldino [.NET/C# MVP]

John,

What I would do is create a static method on the type of your parent
form. Then, have your controls call that method. When the application
starts, I would just store a reference to the main form in a static
variable, and when the method is called (to set the text), set the text of
the status bar to what is passed in.

Hope this helps.
 
J

John J. Hughes II

Ok if I understand this. I have a MDI container which is a class. In that
class I put a static method to update the status bar. I then call this
static method from the MDI windows.

Does this not mean I have to have a reference to the MDI container class in
every form class that is to call it? I have about 10 DLL that are pulled
into this application and run as MDI or non-MDI windows depending on case.

Regards,
John

Nicholas Paldino said:
John,

What I would do is create a static method on the type of your parent
form. Then, have your controls call that method. When the application
starts, I would just store a reference to the main form in a static
variable, and when the method is called (to set the text), set the text of
the status bar to what is passed in.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


John J. Hughes II said:
I have an MDI application and would like to display messages to the status
bar of he parent window. I am really not happy about putting a status bar
on every MDI window and would prefer it to only be on the main window.

I have written the below function but think there must be a better way, is
there?

public static void WriteStatusBar(Form frm, string msg)
{
try
{
if(frm != null && frm.Parent != null && frm.Parent.Parent != null)
{
foreach(object c in frm.Parent.Parent.Controls)
{
if(c.GetType().ToString().EndsWith("StatusBar"))
{
StatusBar sb = (StatusBar)c;
sb.Panels[0].Text = msg;
break;
}
}
}
}
catch(Exception stEx)
{ System.Diagnostics.Debug.WriteLine(stEx.Message); }
}


Regards,
John
 
N

Nicholas Paldino [.NET/C# MVP]

John,

It means you would have to have a reference to the DLL that contains the
type, yes. What I recommended was the quick and dirty way.

What you really should do is have all of your child forms either derive
from a base class which has an event that is fired when the status is to be
updated, or have them implement an interface which has an event that does
the same thing. Then, your MDI container can subscribe to these events and
update the status bar (or whatever you are doing to show the status) when
necessary.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

John J. Hughes II said:
Ok if I understand this. I have a MDI container which is a class. In that
class I put a static method to update the status bar. I then call this
static method from the MDI windows.

Does this not mean I have to have a reference to the MDI container class in
every form class that is to call it? I have about 10 DLL that are pulled
into this application and run as MDI or non-MDI windows depending on case.

Regards,
John

message news:[email protected]...
John,

What I would do is create a static method on the type of your parent
form. Then, have your controls call that method. When the application
starts, I would just store a reference to the main form in a static
variable, and when the method is called (to set the text), set the text of
the status bar to what is passed in.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


John J. Hughes II said:
I have an MDI application and would like to display messages to the status
bar of he parent window. I am really not happy about putting a status bar
on every MDI window and would prefer it to only be on the main window.

I have written the below function but think there must be a better
way,
is
there?

public static void WriteStatusBar(Form frm, string msg)
{
try
{
if(frm != null && frm.Parent != null && frm.Parent.Parent != null)
{
foreach(object c in frm.Parent.Parent.Controls)
{
if(c.GetType().ToString().EndsWith("StatusBar"))
{
StatusBar sb = (StatusBar)c;
sb.Panels[0].Text = msg;
break;
}
}
}
}
catch(Exception stEx)
{ System.Diagnostics.Debug.WriteLine(stEx.Message); }
}


Regards,
John
 
J

John J. Hughes II

Ah... yes I like that way much better, thanks.

Regards,
John

Nicholas Paldino said:
John,

It means you would have to have a reference to the DLL that contains the
type, yes. What I recommended was the quick and dirty way.

What you really should do is have all of your child forms either derive
from a base class which has an event that is fired when the status is to be
updated, or have them implement an interface which has an event that does
the same thing. Then, your MDI container can subscribe to these events and
update the status bar (or whatever you are doing to show the status) when
necessary.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

John J. Hughes II said:
Ok if I understand this. I have a MDI container which is a class. In that
class I put a static method to update the status bar. I then call this
static method from the MDI windows.

Does this not mean I have to have a reference to the MDI container class in
every form class that is to call it? I have about 10 DLL that are pulled
into this application and run as MDI or non-MDI windows depending on case.

Regards,
John

message news:[email protected]...
text
of
the status bar to what is passed in.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


I have an MDI application and would like to display messages to the status
bar of he parent window. I am really not happy about putting a
status
bar
on every MDI window and would prefer it to only be on the main window.

I have written the below function but think there must be a better
way,
is
there?

public static void WriteStatusBar(Form frm, string msg)
{
try
{
if(frm != null && frm.Parent != null && frm.Parent.Parent != null)
{
foreach(object c in frm.Parent.Parent.Controls)
{
if(c.GetType().ToString().EndsWith("StatusBar"))
{
StatusBar sb = (StatusBar)c;
sb.Panels[0].Text = msg;
break;
}
}
}
}
catch(Exception stEx)
{ System.Diagnostics.Debug.WriteLine(stEx.Message); }
}


Regards,
John
 

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