Form loses focus [modified]
-
I have a program from which I am calling a subordinate form with the axmediaplayer name axWMP. I want the subordinate form to run from the keyboard but the key strokes are not seen unless I click on the form. It seems that the form loses focus because the keys strokes go to my compiler not the form.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace IRmediacontrol
{
public partial class WMP : Form
{
string fname = "";
public WMP(string filename)
{
InitializeComponent();
fname = filename;
}
// At this point if I press a key the keystroke is reported to some other window.
// If I click on this Window the keystroke will be reported here. How can I keep the
// focus on this window.private void axWMP\_KeyPressEvent(object sender, AxWMPLib.\_WMPOCXEvents\_KeyPressEvent e) { label1.Text = "key press = " + e.nKeyAscii.ToString(); } // The program mis behaves before this is involked. private void button1\_Click(object sender, EventArgs e) { axWMP.URL = fname; axWMP.Ctlcontrols.play(); } }
}
Any help would be appreciated!
modified on Thursday, October 14, 2010 7:16 PM
-
I have a program from which I am calling a subordinate form with the axmediaplayer name axWMP. I want the subordinate form to run from the keyboard but the key strokes are not seen unless I click on the form. It seems that the form loses focus because the keys strokes go to my compiler not the form.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace IRmediacontrol
{
public partial class WMP : Form
{
string fname = "";
public WMP(string filename)
{
InitializeComponent();
fname = filename;
}
// At this point if I press a key the keystroke is reported to some other window.
// If I click on this Window the keystroke will be reported here. How can I keep the
// focus on this window.private void axWMP\_KeyPressEvent(object sender, AxWMPLib.\_WMPOCXEvents\_KeyPressEvent e) { label1.Text = "key press = " + e.nKeyAscii.ToString(); } // The program mis behaves before this is involked. private void button1\_Click(object sender, EventArgs e) { axWMP.URL = fname; axWMP.Ctlcontrols.play(); } }
}
Any help would be appreciated!
modified on Thursday, October 14, 2010 7:16 PM
Just add
axWMP.Focus();
after you make the .play() call.
-
Just add
axWMP.Focus();
after you make the .play() call.
-
I had tried that prior to the .Play call and it didn't work. Your suggestion solved the problem. Many Thanks.
Please mark his answer as the right answer (by clicking on the Good Answer link).
Regards, Nish
My technology blog: voidnish.wordpress.com Code Project Forums : New Posts Monitor This application monitors for new posts in the Code Project forums.
-
Please mark his answer as the right answer (by clicking on the Good Answer link).
Regards, Nish
My technology blog: voidnish.wordpress.com Code Project Forums : New Posts Monitor This application monitors for new posts in the Code Project forums.
Thank you Nish.
-
Just add
axWMP.Focus();
after you make the .play() call.
I'm afraid I still have the problem. I thought that the problem had been solved as clicking on the button caused the key commands to work. In actuality clicking the button was bringing the focus to the form. Here is the code with no button which loses focus.
public WMP(string filename)
{
InitializeComponent();
this.axWMP.KeyPressEvent += new AxWMPLib._WMPOCXEvents_KeyPressEventHandler(this.axWMP_KeyPressEvent);
fname = filename;
axWMP.settings.autoStart = false;
axWMP.URL = fname;
axWMP.Ctlcontrols.play();
axWMP.Focus();
}
// At this point if I press a key the keystroke is reported to some other window.
// If I click on this Window the keystroke will be reported here. How can I keep the
// focus on this window.private void axWMP\_KeyPressEvent(object sender, AxWMPLib.\_WMPOCXEvents\_KeyPressEvent e) { label1.Text = "key press = " + e.nKeyAscii.ToString(); }
-
I'm afraid I still have the problem. I thought that the problem had been solved as clicking on the button caused the key commands to work. In actuality clicking the button was bringing the focus to the form. Here is the code with no button which loses focus.
public WMP(string filename)
{
InitializeComponent();
this.axWMP.KeyPressEvent += new AxWMPLib._WMPOCXEvents_KeyPressEventHandler(this.axWMP_KeyPressEvent);
fname = filename;
axWMP.settings.autoStart = false;
axWMP.URL = fname;
axWMP.Ctlcontrols.play();
axWMP.Focus();
}
// At this point if I press a key the keystroke is reported to some other window.
// If I click on this Window the keystroke will be reported here. How can I keep the
// focus on this window.private void axWMP\_KeyPressEvent(object sender, AxWMPLib.\_WMPOCXEvents\_KeyPressEvent e) { label1.Text = "key press = " + e.nKeyAscii.ToString(); }
I see what you want now, and no, the .Focus() won't do the trick. My question is whether you are trying to capture keystrokes in a generic manner and then pass on the relevant ones to the child form? The issues may lie with where the focus lies. The keypress arguments are going to be sent from whichever control has the focus, not the form itself, so you won't see the event using the hooks you have. What you need to do is capture the keypress event from whichever control happens to have the focus on the active form and then pass the keystroke event down to the sub form for it to use it. If you tell me which version of Studio you're using, I can package up a sample I just made to demonstrate this and send it to you. You can email me that at [edited out now that you've gotten it], if you like.
modified on Thursday, October 14, 2010 8:50 PM
-
I see what you want now, and no, the .Focus() won't do the trick. My question is whether you are trying to capture keystrokes in a generic manner and then pass on the relevant ones to the child form? The issues may lie with where the focus lies. The keypress arguments are going to be sent from whichever control has the focus, not the form itself, so you won't see the event using the hooks you have. What you need to do is capture the keypress event from whichever control happens to have the focus on the active form and then pass the keystroke event down to the sub form for it to use it. If you tell me which version of Studio you're using, I can package up a sample I just made to demonstrate this and send it to you. You can email me that at [edited out now that you've gotten it], if you like.
modified on Thursday, October 14, 2010 8:50 PM
-
I'm afraid I still have the problem. I thought that the problem had been solved as clicking on the button caused the key commands to work. In actuality clicking the button was bringing the focus to the form. Here is the code with no button which loses focus.
public WMP(string filename)
{
InitializeComponent();
this.axWMP.KeyPressEvent += new AxWMPLib._WMPOCXEvents_KeyPressEventHandler(this.axWMP_KeyPressEvent);
fname = filename;
axWMP.settings.autoStart = false;
axWMP.URL = fname;
axWMP.Ctlcontrols.play();
axWMP.Focus();
}
// At this point if I press a key the keystroke is reported to some other window.
// If I click on this Window the keystroke will be reported here. How can I keep the
// focus on this window.private void axWMP\_KeyPressEvent(object sender, AxWMPLib.\_WMPOCXEvents\_KeyPressEvent e) { label1.Text = "key press = " + e.nKeyAscii.ToString(); }
Have you considered using the ProcessCmdKey overload or event ??? instead of keypress event
With great code, comes great complexity, so keep it simple stupid...:-\ :-\
-
I see what you want now, and no, the .Focus() won't do the trick. My question is whether you are trying to capture keystrokes in a generic manner and then pass on the relevant ones to the child form? The issues may lie with where the focus lies. The keypress arguments are going to be sent from whichever control has the focus, not the form itself, so you won't see the event using the hooks you have. What you need to do is capture the keypress event from whichever control happens to have the focus on the active form and then pass the keystroke event down to the sub form for it to use it. If you tell me which version of Studio you're using, I can package up a sample I just made to demonstrate this and send it to you. You can email me that at [edited out now that you've gotten it], if you like.
modified on Thursday, October 14, 2010 8:50 PM
PogoboyMtK wrote:
You can email me that at
If you want to do that again, there is an Email button at the bottom of your message which means you don't have to expose your email address to Googles' page cache at all.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
I see what you want now, and no, the .Focus() won't do the trick. My question is whether you are trying to capture keystrokes in a generic manner and then pass on the relevant ones to the child form? The issues may lie with where the focus lies. The keypress arguments are going to be sent from whichever control has the focus, not the form itself, so you won't see the event using the hooks you have. What you need to do is capture the keypress event from whichever control happens to have the focus on the active form and then pass the keystroke event down to the sub form for it to use it. If you tell me which version of Studio you're using, I can package up a sample I just made to demonstrate this and send it to you. You can email me that at [edited out now that you've gotten it], if you like.
modified on Thursday, October 14, 2010 8:50 PM
Handling the problem by e-mail keeps other interested parties from seeing the solution to a problem. If it is too much for a forum post, you might consider writing a tip&trick or even an article about it. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
Handling the problem by e-mail keeps other interested parties from seeing the solution to a problem. If it is too much for a forum post, you might consider writing a tip&trick or even an article about it. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
Your right. I finally found the answer to the problem. The child form was not the problem. In the parent form I had done this:
this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
Form2 dlg = new Form2(fname);
dlg.ShowDialog();For some reason this causes Form2 to be inactive I know not why.
-
Your right. I finally found the answer to the problem. The child form was not the problem. In the parent form I had done this:
this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
Form2 dlg = new Form2(fname);
dlg.ShowDialog();For some reason this causes Form2 to be inactive I know not why.
The main form represents the entire app (when it closes, the app exits). I'm surprised the dialog was even showing, if you had minimized the main form later on, the dialog would have disappeared too (try right-clicking minimize on the task bar on any app). BTW: these special things you just showed should have been mentioned earlier... Cheers.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
PogoboyMtK wrote:
You can email me that at
If you want to do that again, there is an Email button at the bottom of your message which means you don't have to expose your email address to Googles' page cache at all.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Thanks. It's ok though, because I expose my "utility" email address and not my primary one.
-
Handling the problem by e-mail keeps other interested parties from seeing the solution to a problem. If it is too much for a forum post, you might consider writing a tip&trick or even an article about it. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
Hey Luc, Rather than span the forum with numerous posts along the way, the idea was to figure out what was wrong and then post that, not the entire process of getting there. Cheers :)
-
The main form represents the entire app (when it closes, the app exits). I'm surprised the dialog was even showing, if you had minimized the main form later on, the dialog would have disappeared too (try right-clicking minimize on the task bar on any app). BTW: these special things you just showed should have been mentioned earlier... Cheers.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
Agreed. I didn't even think to ask that yesterday. :doh: