about Show Method in Control Class under System.Windows.Forms (C#)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

my function: myAdjustForm.Show();

I am using the Show method to show the pop-up window to the users
in my C# program.

I found that I have invoke this function TWICE at the beginning before I can
see the pop-up window poping up.

Do you know how I can fix it? Is it related to missing initialization
routines?

Thanks.

Best regards,

Albert
 
Hi Albert,

When you call myAdjustForm.Show(), does your process own the foreground
window? If you do, then the issue is quite strange indeed, would you please
give me some code so that I can try to reproduce the issue on my side?

If the foreground window doesn't belong to your process, then calling
myAdjustForm.Show() will NOT bring it to foreground on win2000/xp, this is
because by default windows will prevent applications from stealing focus,
it will flash the taskbar button instead. In this case, calling Show()
twice will also not solve the issue; therfore it might not be the case
either. (For this issue, you will have to use SetForegroundWindow along
with AttachThreadInput as a workaround, search for these two APIs and you
will much information about it)

If you do post the code, please let me know which .NET version you're
using. Thanks.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Albert,

This is a strange problem that have never happened to me. Can you post
simple compilable sample that demonstrates the problem.
 
Hi,

Thank you for all the replies.

I am using MS VS 2005 (MS Visual C# 2005 77626-009-0000007-41501).

I included the function that I have in my program at the end of my email. I
set a breakpoint inside this function in the debugger. The debugger stops
inside the function when I select to display the pop-up windows.
(myAdjustForm.Visible is false in this case).

Here is my obeservation:
1) 1st time
The program stops at another function in another form after I stepped over
myAdjustForm.Show(). There is no pop-up window showing up.
2) 2nd time
The program stops at myAdjustForm.BringToFront() after I stepped over
myAdjustForm.Show(). The pop-up window shows up as I expected.
3) The pop-up window shows up every time after the second time.

I think this could be a C# compiler bug since I don't understand why the
execution sequence of my program will change after the first time. I tried to
uncheck the optimize code box in the build page but this problem is still
there. So I am not sure that the same problem will show up in a simplified
version of my program if this is compiler related problems.

Do you have any ideas how to change the behavior of the compiler so that it
will generate the code that reflects the program flow?

Best regards,

Albert

******************************************************
private void menuItemAdjust_Click(object sender, EventArgs e)
{
if (myAdjustForm.Visible == true)
myAdjustForm.Hide();
else
{
myAdjustForm.Show();
myAdjustForm.BringToFront();
}
}
*****************************************************
 
Hi Albert,

Thank you for your detailed explanation. However, I'm still not very clear
about the issue here. It's my understanding that stepping through the code
will certainly have impact on the Form's show/hide behavior since you're
showing the form non-modal, which means the IDE will get focus when you
stepped through the code. Let me know if I've misunderstood anything.

Using your test code, I've created a local project to test the behavior:

private Form2 f2 = new Form2();

private void showHideForm2ToolStripMenuItem_Click(object sender,
EventArgs e)
{
if (f2.Visible == true)
f2.Hide();
else
{
f2.Show();
f2.BringToFront();
}
}

This should works correctly if you're running the project instead of
stepping through the .Show or .BringToFront() code.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi,

I expect the program will stop at myAdjustForm.BringToFront() after I step
over
myAdjustForm.Show(). It happens everytime after the first time.

My test can only show the program does reach myAdjustForm.show() function
every time that I select to show this pop-up window from the menu. It is up
to Windows to show the pop-up window or CSC to generate the right code.

In addition, I have similar functions throughout my program to display other
windows. This is the only one that doesn't work right the first time. Can you
explain why?

The above can be observed with/without code optimization enable.

If I cannot make sense out of my program when I step it through, how can I
debug
my program?

Thanks.

Best regards,

Albert
 
Hi Albert,

When the first time .Show gets called, is the form already loaded? i.e.
Does its Load event get called before the first call to its Show()? Also,
what's the Load event doing there?

It would be great if you could send me a reproducible project which could
easily show the issue. Thank you for your effort.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Albert,

Would you please let us know the status of this post? Thanks.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Walter,

I can't send you my program and I think a simplified version may not show
this problem.

Can you elaborate more about the load event since I am not familiar with it?
How can I tell whether the form is loaded?

Do you think this is an initialization problem of my program?
Can you suggest how to generate some debug messages that can help
us to see find the root cause of this problem.

Thanks.

Best regards,

Albert
 
Hi Albert,

Here's some important events of a Form that might be useful for you:
===========
Load: This event is fired when the form is first loaded in the program. It
is called when the Form.Show() or Form.ShowDialog() method is called for
the first time.

Activated: This event is triggered whenever the form gets the application
focus. It is fired when the form is first loaded (along with Load event),
when the form is brought in front, or when the form is restored from the
minimized state.

VisibleChanged: It is called whenever the Visible property of the form is
changed or the form is hidden or shown.

Deactivated: This event is triggered whenever the form loses the
application focus. It is fired when the form is closed, when the form is
brought into background, or when the form is minimized.

Closing: It is fired when the application wishes to be closed, or the
application is in the process of close but has not yet closed.

Closed: It is raised when the application is finally closed.
===========


Also, you might find following article useful:

#ShawnVN's Blog : WinForms Tip-of-the-Day: Avoid subscribing to your own
events
http://weblogs.asp.net/savanness/archive/2003/03/10/3646.aspx


Let me know if there's anything else I can help.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Albert,

I am interested in this issue. Would you mind letting me know the result of
the suggestions? If you need further assistance, feel free to let me know.
I will be more than happy to be of assistance.

Have a great day!

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

Back
Top