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. Activating a child form

Activating a child form

Scheduled Pinned Locked Moved C#
question
14 Posts 4 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 am writing a program that is going to be operated with an IR controller. In the program Form1 creates a new form Form2. Form2 dlg = new Form2(fname); if (dlg.ShowDialog() == DialogResult.OK) { } Unfortunatly the new form will not receive input unless I click on it with the mouse to make it active. I have tried various commands but in order to make the new form receive the IR commands I must click on the form with the mouse. How can I activate the form programatically. public Form2(string temp) { InitializeComponent(); //this.Focus(); // Tried these //this.Activate(); //this.BringToFront(); }

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

    Hi George,

    electriac wrote:

    the new form will not receive input unless I click on it

    I don't think this is correct. What probably happened is the Control with the lowest TabIndex isn't the one you want it to be. Try playing around with TabIndex (and check TabStop is still at its default value of true). If all that does not help, experiment with Focus, Activate, BringToFront inside the Shown handler, the constructor is too soon for these. Regards,

    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


    I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


    E 1 Reply Last reply
    0
    • L Luc Pattyn

      Hi George,

      electriac wrote:

      the new form will not receive input unless I click on it

      I don't think this is correct. What probably happened is the Control with the lowest TabIndex isn't the one you want it to be. Try playing around with TabIndex (and check TabStop is still at its default value of true). If all that does not help, experiment with Focus, Activate, BringToFront inside the Shown handler, the constructor is too soon for these. Regards,

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


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

      Tried delaying commands with a timer. NG. Unless I click on the form the IR commands are written to my source code. So the compiler is the active form.

      L 1 Reply Last reply
      0
      • E electriac

        Tried delaying commands with a timer. NG. Unless I click on the form the IR commands are written to my source code. So the compiler is the active form.

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

        electriac wrote:

        the compiler is the active form

        Each WinForm application has an active window; what you are saying is somehow your IDE (Visual Studio?) is active instead of your app itself. That is abnormal; what do you do to make your app run? what language is it in? it is a WinForm app isn't it? what happens when you run your app by double-clicking its exe (in bin/debug/ or in bin/release/ subfolders)? BTW: within the app, i.e. assuming your app has focus, it is the Shown event you want, not a timer! :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


        E 1 Reply Last reply
        0
        • E electriac

          I am writing a program that is going to be operated with an IR controller. In the program Form1 creates a new form Form2. Form2 dlg = new Form2(fname); if (dlg.ShowDialog() == DialogResult.OK) { } Unfortunatly the new form will not receive input unless I click on it with the mouse to make it active. I have tried various commands but in order to make the new form receive the IR commands I must click on the form with the mouse. How can I activate the form programatically. public Form2(string temp) { InitializeComponent(); //this.Focus(); // Tried these //this.Activate(); //this.BringToFront(); }

          R Offline
          R Offline
          Roger Wright
          wrote on last edited by
          #5

          I don't know how useful this may be, as I'm a total newbie to C#, but I'm working tonight on the exact same situation - a form creating a form - and mine's working perfectly. The only difference I see is that, rather than passing a value in the new statement, I'm initializing that value in a separate statement immediately after, ie:

          Form2 dlg = new Form2();
          dlg.temp = fname;
          if(dlg.ShowDialog()==DialogResult.OK)
          { ...

          This assumes that Form2 defines a public member temp, of course. I have no idea why it would make a difference, but it just might.

          "A Journey of a Thousand Rest Stops Begins with a Single Movement"

          1 Reply Last reply
          0
          • E electriac

            I am writing a program that is going to be operated with an IR controller. In the program Form1 creates a new form Form2. Form2 dlg = new Form2(fname); if (dlg.ShowDialog() == DialogResult.OK) { } Unfortunatly the new form will not receive input unless I click on it with the mouse to make it active. I have tried various commands but in order to make the new form receive the IR commands I must click on the form with the mouse. How can I activate the form programatically. public Form2(string temp) { InitializeComponent(); //this.Focus(); // Tried these //this.Activate(); //this.BringToFront(); }

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6

            electriac wrote:

            Unfortunatly the new form will not receive input

            What do you mean by this ? Do you have a textbox in your form that doesn't receive what you type on the keyboard as soon as the form is opened? You may have to set its TabIndex property to 0 and TabStop to true.

            E 1 Reply Last reply
            0
            • L Lost User

              electriac wrote:

              Unfortunatly the new form will not receive input

              What do you mean by this ? Do you have a textbox in your form that doesn't receive what you type on the keyboard as soon as the form is opened? You may have to set its TabIndex property to 0 and TabStop to true.

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

              There is no TabIndex or TabStop as the form is receiving input from an IR controller as a keydown event.

              L 1 Reply Last reply
              0
              • L Luc Pattyn

                electriac wrote:

                the compiler is the active form

                Each WinForm application has an active window; what you are saying is somehow your IDE (Visual Studio?) is active instead of your app itself. That is abnormal; what do you do to make your app run? what language is it in? it is a WinForm app isn't it? what happens when you run your app by double-clicking its exe (in bin/debug/ or in bin/release/ subfolders)? BTW: within the app, i.e. assuming your app has focus, it is the Shown event you want, not a timer! :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


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

                I am compiling and executing with CTRL F5 it is a standard windows form in C#. I will have to look into executing outside the compiler. TNX Luc

                1 Reply Last reply
                0
                • E electriac

                  There is no TabIndex or TabStop as the form is receiving input from an IR controller as a keydown event.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #9

                  I don't know if I fully understand your question. But in windows applications, only the active control with the keyboard focus can receive keyboard events. You need to set focus to the textbox (or whatever your control is) before you can send keystrokes to the control.

                  E 2 Replies Last reply
                  0
                  • L Lost User

                    I don't know if I fully understand your question. But in windows applications, only the active control with the keyboard focus can receive keyboard events. You need to set focus to the textbox (or whatever your control is) before you can send keystrokes to the control.

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

                    Update: I have done some more experimenting and find the following. When the program is executed outside the compiler the child Form2 is created but the focus remains on Form1. When compiled with CTRL F5 the focus goes to the compiler. Here is where Form2 is created: Form2=playMKV Application.RemoveMessageFilter(this); if (temp.EndsWith(".mkv")) // its a Matroska file { playMKV dlg = new playMKV(fname); //if (dlg.ShowDialog() == DialogResult.OK) // not necessary as there will always be a result 1 or 0; dlg.ShowDialog(); if (dlg.Number == 1) { string temp2 = "\x22" + fname + "\x22"; System.Diagnostics.Process proc = System.Diagnostics.Process.Start("mplayerc.exe", temp2 + " /fullscreen /play /close "); proc.WaitForExit(); } this.WindowState = System.Windows.Forms.FormWindowState.Maximized; Application.AddMessageFilter(this); return; } I have tried delaying the this.Focus in Form2 as Luc suggested and it seems to make no difference. private void timer1_Tick(object sender, EventArgs e) { timer1.Enabled = false; this.Focus(); this.Activate(); this.BringToFront(); } This Form2 closes on any keydown event there being two options: 1.the "Enter" key is hit or "Enter" is pressed on the IRcontroller in which case the key event is == 13 returning 1 to Form1 or 2.on any other key the Form2 is closed returning 0; Form2 keydownevent if (keyEvent.KeyValue == 13) { num = 1; } else { num = 0; } this.Close(); The program works without problem as long as I click on Form2 with the mouse.

                    1 Reply Last reply
                    0
                    • L Lost User

                      I don't know if I fully understand your question. But in windows applications, only the active control with the keyboard focus can receive keyboard events. You need to set focus to the textbox (or whatever your control is) before you can send keystrokes to the control.

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

                      Shameel TNX for your input as it allowed me to solve the problem. I added an event handler to the listBox on Form2 and the focus was gained by the form. TNX again. :) :) :)

                      L 1 Reply Last reply
                      0
                      • E electriac

                        Shameel TNX for your input as it allowed me to solve the problem. I added an event handler to the listBox on Form2 and the focus was gained by the form. TNX again. :) :) :)

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #12

                        Is there a reason you use Application.RemoveMessageFilter(this)?

                        E 2 Replies Last reply
                        0
                        • L Lost User

                          Is there a reason you use Application.RemoveMessageFilter(this)?

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

                          Yes. The message filter seems to stop the Form2 from receiving the IR commands. Although I have not test without it since implementing the newest changes.

                          1 Reply Last reply
                          0
                          • L Lost User

                            Is there a reason you use Application.RemoveMessageFilter(this)?

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

                            I checked and the keydown event goes to the parent form and not the child unless the Message Filter is removed.

                            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