Solved

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

Posted on 2008-06-09
8
1,273 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
8 Comments
 
LVL 29

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 29

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now