Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C# Winforms - keeping form from getting focus when it refreshes in the background

Posted on 2008-06-09
8
Medium Priority
?
1,304 Views
Last Modified: 2013-11-26
Working on a smart client app similar to outlook. The main form has a timer that will refreshes the ui with the latest data (example. check for new messages and display them in a list). The problem is when a user opens one of these items in a new form, when the parent refreshes it pops up in front of the form the user is working on. Any easy way of making the parent stay in the background.
0
Comment
Question by:mtakasaki
  • 3
  • 2
  • 2
7 Comments
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 21747086
Not very clear... Can't you use ShowDialog for a second form?

Another approach is to use main's form Activated event. In the event handler put something like that (pseudo code):

if refreshing then
     childForm.Activate
else // if not refreshing
   // do nothing
end if
0
 
LVL 4

Assisted Solution

by:intrwrks
intrwrks earned 500 total points
ID: 21747397
It shouldn't do this unless the form that is in the background is setting focus to something. A way around this is to create a new thread and then use that new thread to call form.ShowDialog(). This will allow the background form to run and if focus is set it should not popup above the other form.
private void button1_Click(object sender, EventArgs e)
{
            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadedWindowStart));
            thread.Start();
}
 
private void ThreadedWindowStart()
{
            Form2 form2 = new Form2();
            form2.ShowDialog();
}

Open in new window

0
 

Author Comment

by:mtakasaki
ID: 21815997
Found problem... the background form was publishing an event to another smartpart which was setting focus to a control.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 21817469
Still not very clear what happened but I have a feeling that intrwrks's answer wre pointing to a right direction, or at least helped to find it.
0
 

Author Comment

by:mtakasaki
ID: 21824219
The solution had nothing to do with creating a second thread...
 I finally found the problem by debugging the app and removed the code that was setting focus to the control as it was not needed. If it was needed and I had to create  a second thread to handle this then the solution provided would have been awarded points. While the feedback was greatly appreciated it didn't really help in finding the fix.
0
 
LVL 4

Expert Comment

by:intrwrks
ID: 21827277
I was going to remain silent in the request to close your question, but since there was already a response that has stopped the process, I need to point out that the first sentence in my original post referenced the solution that you eventually found. Since I was unable to view any code and did not know if the focus was required, I offered a way around the problem at hand. Although the solution of removing the code causing the focus is a much better solution than the one I provided.

Regards
Jason
0
 

Author Closing Comment

by:mtakasaki
ID: 31465571
Thanks for responding to my question... I awarded points to both cause I could not decide on whose comments where more helpful... Thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month21 days, 3 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question