Things in an OnLoad event

R

Roy Chastain

I have a form that contains a TabControl and a StatusBar.
Each TabPage in the TabControl has a UserControl on it that receives an OnLoad event.

I have a routine to set string values into the StatusBar
public void SetStatus (string new_text_string)
{
statusbar1.Text = new_text_string; //statusbar1 is global on the form not on a TabPage
statusbar1.Refresh(); // I have also tried invalidate
}

This works fine except when it is called from the OnLoad routine in the UserControl. The routine to set the text gets called and
completes normally, but the status never displays in the status bar.

Can anyone explain what might be happening that is causing this behavior?

Thanks
 
R

Rhett Gong [MSFT]

Hi Roy,
Based on my understanding, you want to display the first usercontrol's
information in the form's status bar. And I think this problem exactly
happens in the Onload of your start tabpage's usercontrol (maybe tabpage1).


Many things created under .NET are in lazy manner. When you get into OnLoad
of your first usercontrol, the status bar is not completely "created". So,
though you use "statusBar1.Text = new_text_string;", you have nothing set
in the status bar text. We can see this through following way:
1>remove the control from tabpage1
2>add another tabpage (tabpage2) and place the usercontrol in it.
When we switch from tabpage1 to tabpage2, we can see the status bar text is
changed as expected.

If your control does not use some specified information to set the status
bar, you could set status bar initial text as same as usercontrol will set.
If it is the user who specifies the status text, you can fire other events
to have the text set. (as you have found, the SetStatus method works fine
in other scenarios)

Have a nice day!

Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
R

Roy Chastain

No, you misunderstand. The StatusBar is on the form NOT on the tab page. I have 4 tabpages. Any text destined for the
StatusBar works as long as it does not come from the OnLoad of a control on the TabPage.

Hi Roy,
Based on my understanding, you want to display the first usercontrol's
information in the form's status bar. And I think this problem exactly
happens in the Onload of your start tabpage's usercontrol (maybe tabpage1).


Many things created under .NET are in lazy manner. When you get into OnLoad
of your first usercontrol, the status bar is not completely "created". So,
though you use "statusBar1.Text = new_text_string;", you have nothing set
in the status bar text. We can see this through following way:
1>remove the control from tabpage1
2>add another tabpage (tabpage2) and place the usercontrol in it.
When we switch from tabpage1 to tabpage2, we can see the status bar text is
changed as expected.

If your control does not use some specified information to set the status
bar, you could set status bar initial text as same as usercontrol will set.
If it is the user who specifies the status text, you can fire other events
to have the text set. (as you have found, the SetStatus method works fine
in other scenarios)

Have a nice day!

Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
R

Rhett Gong [MSFT]

Hi Roy,
you want to display the first usercontrol's
information in the form's status bar. And I think this problem exactly

From my initial understanding, the status bar is located on the form (NOT
on the tabpage) and you had the statusbar's text updated from your
tabpage's method (including the Onload). Actually, I have this problem
reproed in my side (see below).

//--------------------------------------------------------------
// mainform
//.....
public class Form1 : System.Windows.Forms.Form
{
public System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.TabPage tabPage3;
private OnloadProblem.UserControl1 userControl11;
//........
}

// usercontrol, I update the statusbar text here
public class UserControl1 : System.Windows.Forms.UserControl
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.CheckBox checkBox1;
private Form1 m_parent = null;
//.................

private void UserControl1_Load(object sender, System.EventArgs e)
{
m_parent.SetStatus("Today is a shining day!");
}
//......
}
//---------------------------------------------------------------------
Using the same UserControl, if we place it in tabpage2, the statusbar will
be correctly updated. And from the debugging information, I find that the
status bar is not completely created (lazy mode in .NET) while you set the
text to the statusbar. So I suggest you set the text in the statusbar as
you will set from tabpage's Onload, if there is no user specific
information needed.

Does this answer your question? If you still have problem, please feel free
to let me know.

Have a nice day!

Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
R

Roy Chastain

This does not make sense.
I have 4 tab pages and it does not matter which one I am clicking on. The text from the OnLoad does not work. I am saying that
even when I switch to the 2nd tabpage (after things have been displayed in the StatusBar) it still does not display text from the
placed in it from the OnLoad event.

I don't see how any type of 'lazy creation' could be an issue since the control has already been created.

I believe that it some other issue with OnLoad plumbing. Now, if you want to suggest that the TabPage does not really know it's
parent at that point, I might believe that, but I don't think that it is a reasonable condition.

I don't really want to have to put the statusbar on each tabpage etc.

Hi Roy,
you want to display the first usercontrol's
information in the form's status bar. And I think this problem exactly

From my initial understanding, the status bar is located on the form (NOT
on the tabpage) and you had the statusbar's text updated from your
tabpage's method (including the Onload). Actually, I have this problem
reproed in my side (see below).

//--------------------------------------------------------------
// mainform
//.....
public class Form1 : System.Windows.Forms.Form
{
public System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.TabPage tabPage3;
private OnloadProblem.UserControl1 userControl11;
//........
}

// usercontrol, I update the statusbar text here
public class UserControl1 : System.Windows.Forms.UserControl
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.CheckBox checkBox1;
private Form1 m_parent = null;
//.................

private void UserControl1_Load(object sender, System.EventArgs e)
{
m_parent.SetStatus("Today is a shining day!");
}
//......
}
//---------------------------------------------------------------------
Using the same UserControl, if we place it in tabpage2, the statusbar will
be correctly updated. And from the debugging information, I find that the
status bar is not completely created (lazy mode in .NET) while you set the
text to the statusbar. So I suggest you set the text in the statusbar as
you will set from tabpage's Onload, if there is no user specific
information needed.

Does this answer your question? If you still have problem, please feel free
to let me know.

Have a nice day!

Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
R

Rhett Gong [MSFT]

I am a bit confused. ;(
Could you send a simple repro sample to me please?

Ps: to send email, please note to remove online from my email address.
Thanks for your patience.

Have a nice day!

Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
R

Rhett Gong [MSFT]

Hi Roy,
Try following way in your main code (not repro sample) to see if it could
resolve your problem:
//------------------------------ in user control
-----------------------------------------------
//add a member variable
private Form1 m_parent = null;
// then add a method to get the mainform's reference as this
public void PassParent(Form1 f1)
{
m_parent = f1;
}
// now in the usercontrol's OnLoad, we change the status bar's text
private void UserControl1_Load(object sender, System.EventArgs e)
{
if(m_parent != null)
m_parent.SetStatus("Today is a shining day!");
}

//------------in
mainform--------------------------------------------------------------------
---
// we need to pass itsreference to usercontrol as follows
public Form1()
{
InitializeComponent();
userControl11.PassParent(this); // call usercontrol's method
}
//--------------------------------------------------------------------------
---------------------------
Please feel free to let me know if you have any thing unclear.

Have a nice day!

Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
R

Roy Chastain

I tried the work around code below. It does not resolve the problem either.

Hi Roy,
Try following way in your main code (not repro sample) to see if it could
resolve your problem:
//------------------------------ in user control
-----------------------------------------------
//add a member variable
private Form1 m_parent = null;
// then add a method to get the mainform's reference as this
public void PassParent(Form1 f1)
{
m_parent = f1;
}
// now in the usercontrol's OnLoad, we change the status bar's text
private void UserControl1_Load(object sender, System.EventArgs e)
{
if(m_parent != null)
m_parent.SetStatus("Today is a shining day!");
}

//------------in
mainform--------------------------------------------------------------------
---
// we need to pass itsreference to usercontrol as follows
public Form1()
{
InitializeComponent();
userControl11.PassParent(this); // call usercontrol's method
}
//--------------------------------------------------------------------------
---------------------------
Please feel free to let me know if you have any thing unclear.

Have a nice day!

Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
R

Rhett Gong [MSFT]

Hi Roy,

In your mail, you said my suggestion worked in a simple repro app, I'd like
to know if the simple repro was made from ground up or seperate some
related code from your project? Is it is a new project, I suggest you try
seperating the problematic part from your current project (hopefully one or
several forms).

Besides, from your description, the statusbar control had been created when
executing the usercontrol's OnLoad event, I'd like to confirm this. You may
add the follow line in the SetStatus method in your form and run your
program again:
Debug.Assert(this.statusBar1.Created);
Debug.Writeline("StatusBar handle = {0}", this.statusBar.Handle);

If the assert dialog does not occur, we can now make sure the statusbar had
been created. Maybe there are still some unknown issues,
could you try loading your user control in the run-time instead, so that we
can get a message log using Spy++ to see if the statusbar receives the
SB_SETTEXT /WM_SETTEXT message,
(you may set capture window to the Form , set filter to log general and
statusbar related messages then set the log file on the output tab). if
the statusbar does not receive the set text message, you may spy on the
status bar window and compare its handle value with the debug output when
we in SetStatus.

Here are some suggestions I can think of now, I hope it will help you
somehow to narrow down this issue, if the suggestions is not helpful, I
think this issue needs further investigation, a repro sample is needed to
reproduce this problem on my side, so that I can get more information and
dig deeper into this issue. If you have any findings when trying my
suggestions or able to make the repro sample , please feel free to reply
this thread to let me know. I'm glad to assist you on this issue.


Best regards,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
R

Roy Chastain

Thank you VERY much for your persistence on this issue. I know that the StatusBar is created because I already have other text
displaying in it.

Yes, the repro was developed from the ground up.

I don't see any difference between the simple repro code and this other than this project has MUCH more code in it and many more
controls being created on page when it loads.

At this point I have MANY more pressing issues in this product. I have about 10 outstanding problems on the news groups that are
not being responded to. I need to work them instead of this one.

Again thank for your efforts and follow through, but I am not going to pursue this problem anymore at this time.

Hi Roy,

In your mail, you said my suggestion worked in a simple repro app, I'd like
to know if the simple repro was made from ground up or seperate some
related code from your project? Is it is a new project, I suggest you try
seperating the problematic part from your current project (hopefully one or
several forms).

Besides, from your description, the statusbar control had been created when
executing the usercontrol's OnLoad event, I'd like to confirm this. You may
add the follow line in the SetStatus method in your form and run your
program again:
Debug.Assert(this.statusBar1.Created);
Debug.Writeline("StatusBar handle = {0}", this.statusBar.Handle);

If the assert dialog does not occur, we can now make sure the statusbar had
been created. Maybe there are still some unknown issues,
could you try loading your user control in the run-time instead, so that we
can get a message log using Spy++ to see if the statusbar receives the
SB_SETTEXT /WM_SETTEXT message,
(you may set capture window to the Form , set filter to log general and
statusbar related messages then set the log file on the output tab). if
the statusbar does not receive the set text message, you may spy on the
status bar window and compare its handle value with the debug output when
we in SetStatus.

Here are some suggestions I can think of now, I hope it will help you
somehow to narrow down this issue, if the suggestions is not helpful, I
think this issue needs further investigation, a repro sample is needed to
reproduce this problem on my side, so that I can get more information and
dig deeper into this issue. If you have any findings when trying my
suggestions or able to make the repro sample , please feel free to reply
this thread to let me know. I'm glad to assist you on this issue.


Best regards,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
R

Rhett Gong [MSFT]

Glad to be of assist.

You can open this thread at any time you want. See you then. ;-)


Have a nice day!
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 

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