Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Form loses focus [modified]

Form loses focus [modified]

Scheduled Pinned Locked Moved C#
csharplinqgraphicshelpquestion
16 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E electriac

    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

    F Offline
    F Offline
    fjdiewornncalwe
    wrote on last edited by
    #2

    Just add

    axWMP.Focus();

    after you make the .play() call.

    E 2 Replies Last reply
    0
    • F fjdiewornncalwe

      Just add

      axWMP.Focus();

      after you make the .play() call.

      E Offline
      E Offline
      electriac
      wrote on last edited by
      #3

      I had tried that prior to the .Play call and it didn't work. Your suggestion solved the problem. Many Thanks.

      N 1 Reply Last reply
      0
      • E electriac

        I had tried that prior to the .Play call and it didn't work. Your suggestion solved the problem. Many Thanks.

        N Offline
        N Offline
        Nish Nishant
        wrote on last edited by
        #4

        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.

        F 1 Reply Last reply
        0
        • N Nish Nishant

          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.

          F Offline
          F Offline
          fjdiewornncalwe
          wrote on last edited by
          #5

          Thank you Nish.

          1 Reply Last reply
          0
          • F fjdiewornncalwe

            Just add

            axWMP.Focus();

            after you make the .play() call.

            E Offline
            E Offline
            electriac
            wrote on last edited by
            #6

            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(); 
                }
            
            F P 2 Replies Last reply
            0
            • E electriac

              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(); 
                  }
              
              F Offline
              F Offline
              fjdiewornncalwe
              wrote on last edited by
              #7

              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

              E OriginalGriffO L 3 Replies Last reply
              0
              • F fjdiewornncalwe

                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

                E Offline
                E Offline
                electriac
                wrote on last edited by
                #8

                If I click on the form all works as it should. I am writing a mediaplayer that functions from a IRremote so I dont want to have to click on the form with a mouse in order to read the key commands.

                1 Reply Last reply
                0
                • E electriac

                  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(); 
                      }
                  
                  P Offline
                  P Offline
                  Paw Jershauge
                  wrote on last edited by
                  #9

                  Have you considered using the ProcessCmdKey overload or event ??? instead of keypress event

                  With great code, comes great complexity, so keep it simple stupid...:-\ :-\

                  1 Reply Last reply
                  0
                  • F fjdiewornncalwe

                    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

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #10

                    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 have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    F 1 Reply Last reply
                    0
                    • F fjdiewornncalwe

                      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

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #11

                      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.

                      E F 2 Replies Last reply
                      0
                      • L Luc Pattyn

                        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.

                        E Offline
                        E Offline
                        electriac
                        wrote on last edited by
                        #12

                        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.

                        L 1 Reply Last reply
                        0
                        • E electriac

                          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.

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #13

                          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.

                          F 1 Reply Last reply
                          0
                          • OriginalGriffO OriginalGriff

                            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.

                            F Offline
                            F Offline
                            fjdiewornncalwe
                            wrote on last edited by
                            #14

                            Thanks. It's ok though, because I expose my "utility" email address and not my primary one.

                            1 Reply Last reply
                            0
                            • L Luc Pattyn

                              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.

                              F Offline
                              F Offline
                              fjdiewornncalwe
                              wrote on last edited by
                              #15

                              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 :)

                              1 Reply Last reply
                              0
                              • L Luc Pattyn

                                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.

                                F Offline
                                F Offline
                                fjdiewornncalwe
                                wrote on last edited by
                                #16

                                Agreed. I didn't even think to ask that yesterday. :doh:

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups