?
Solved

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

Posted on 2008-06-09
8
Medium Priority
?
1,294 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 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

801 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