Solved

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

Posted on 2008-06-09
8
1,270 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now