turn123
asked on
C# giving my form focus
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
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);
}
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
I would suggest only a combination of:
this.BringToFront();
this.Activate();
Tell me what happend...
Regards
ASKER
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
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
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(o
{
this.Invoke(new SetFocusDelegate(SetFormFo
}
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...
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...
ASKER
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
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
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...
ASKER
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
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
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();
"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?
Does the taskbar entry for your program flash by chance?
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
Here's a good site on the issue:
http://www.chrisdottodd.com/2007/04/active-window-loses-focus.html
http://www.chrisdottodd.com/2007/04/active-window-loses-focus.html
ASKER
Thank you very much! That took care of the problem.
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..