We help IT Professionals succeed at work.

C# giving my form focus

turn123
turn123 asked
on
Hi,

I am having trouble giving my form focus.

This is what I've tried so far which successfully brings it to the top when called but it doesn't give the form focus.

What do I need to do to give it focus as well?

Thank you,
Jonathan
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            this.TopMost = true;
            this.Focus();
            this.BringToFront();
            this.Activate();
            cbCostCodeIn.Text = serialPort1.ReadExisting().Replace("+", "").Trim();
            txtEmployeeID_Leave(null, null);
        }

Open in new window

Comment
Watch Question

Top Expert 2010

Commented:

I presume cbCostCodeIn is a combobox and you want the focus there. You can try the following:
cbCostCodeIn.Focus();
I guess It may be something in the txtEmployeeID_Leave handler as well.
When another control gets focus, currently focused control's Leave event will be automatically called...Calling cbCostCodeIn.Focus(); will fire txtEmployeeID_Leave if the txtEmployeeID_Leave was having the focus...so you can consider not calling txtEmployeeID_Leave..
Usually Activate() already gives focus to the form, and I don"t think you need to make it Top.
I would suggest only a combination of:

this.BringToFront();
this.Activate();

Tell me what happend...

Regards
Thank you for the suggestions.

kris_per,

The problem I'm having is I can't get any part of the form focus.  Commenting out everything but

this.TopMost = true;
this.Focus();
this.BringToFront();
this.Activate();

still brings the form to the front but does not give it focus.

felipe_schau,

this.BringToFront();
this.Activate();

does not bring the form to the front.

Thank you,
Jonathan
Top Expert 2010

Commented:

Check Enabled property of the form and the controls is true.

Also i think serialPort1_DataReceived could have been called from a thread...so try using Invoke to call the setfocus code in main thread as shown below:

class Form1 : Form
{
...
...
 private delegate void SetFocusDelegate();

private void SetFormFocus()
        {
            // try all focus/activate calls here
        }

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
this.Invoke(new SetFocusDelegate(SetFormFocus));

}

Avtivate() should bring it to the front tool... inversing the two methods wouldn"t work in your case I guess.
When you run this app is it the one you"re workin with or does it stay behind, like minimized?

Another question, what exactly do yolu want to happen when you give focus to your form?
Maybe it is focused and you want to do something else...
kris_per,

What reference do I need for SetFocusDelegate?

felipe_schau,

When I run this app it stays behind until it gets input from COM1 at which point the event is called and it should come to the front and receive focus so the user can process the input.

Currently it comes to the front but does not have focus (the application that had focus when this one came to the front retains the focus and that is what I'm trying to change).

When I give it focus I want keys (such as tab) to work on the form.

Thank you,
Jonathan
Top Expert 2010

Commented:

I am not sure what do you mean by 'what reference'. Have the delegate and the SetFormFocus method within this form class...use 'this' for to call Invoke...
Hi kris_per,

When I use SetFocusDelegate it won't compile due to SetFocusDelegate type or namespace not being found.

I am wondering what I need to add for it to find the type or namespace.

Thank you,
Jonathan
Top Expert 2010

Commented:

I get the same error when I comment the line 'private delegate void SetFocusDelegate();'.

Make sure you have added this line somewhere within your class (as I have shown in my code above)

private delegate void SetFocusDelegate();
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
"Currently it comes to the front but does not have focus (the application that had focus when this one came to the front retains the focus and that is what I'm trying to change)."

Does the taskbar entry for your program flash by chance?
kris_per,

Sorry after adding that in it will take focus for 2-3 seconds the first time and then gives it back to the application that had focus previously.

Any further attempts don't do anything.

Idle_Mind,

It does flash and ends up being orange.

Thank you,
Jonathan
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
"It does flash and ends up being orange."

That is an OPERATING SYSTEM setting and has nothing to do with the way your application was written.  =\

See here for more info:
http://www.pctools.com/guides/registry/detail/540/

It can also be done with Tweak UI:
http://www.microsoft.com/windowsxp/downloads/powertoys/xppowertoys.mspx

Oh joy...

Thank you very much.

I will hopefully get approval to try this tonight (all the machines are live machines and are in use during the day).

Jonathan
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Thank you very much!  That took care of the problem.