Solved

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

Posted on 2008-06-09
8
1,291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
8 Comments
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 125 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 125 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
Technology Partners: 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

707 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