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. Adding/Removing parent's keypressed handler on MDI child VisibleChange method

Adding/Removing parent's keypressed handler on MDI child VisibleChange method

Scheduled Pinned Locked Moved C#
dockerquestion
5 Posts 3 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.
  • D Offline
    D Offline
    dsovino
    wrote on last edited by
    #1

    Hi, I want to disable a keypress handler of a mdi container form. I'm adding this on the container's Form_load: this.KeyPress += new KeyPressEventHandler(Form1_KeyPressed); and this on the child's VisibleChange event: if (this.Visible) { principal.KeyPress -= new KeyPressEventHandler(principal.Form1_KeyPressed); } else { principal.KeyPress += new KeyPressEventHandler(principal.Form1_KeyPressed); } where principal is a property assigned on the child's load, in order to call the objects on the parent form: principal = (FormMain)this.MdiParent; When i run this the first time works. The handler works until I turn visible the child form for the first time (which tells me the -= is working), removing the parent's handler. After i visible=false the child, the handler gets added back, but now it calls 2 times the event attached instead of 1, and so on with every on/off of the child (3, 4, 5)... why? :doh: thanks a lot for your time. daniel

    C L 2 Replies Last reply
    0
    • D dsovino

      Hi, I want to disable a keypress handler of a mdi container form. I'm adding this on the container's Form_load: this.KeyPress += new KeyPressEventHandler(Form1_KeyPressed); and this on the child's VisibleChange event: if (this.Visible) { principal.KeyPress -= new KeyPressEventHandler(principal.Form1_KeyPressed); } else { principal.KeyPress += new KeyPressEventHandler(principal.Form1_KeyPressed); } where principal is a property assigned on the child's load, in order to call the objects on the parent form: principal = (FormMain)this.MdiParent; When i run this the first time works. The handler works until I turn visible the child form for the first time (which tells me the -= is working), removing the parent's handler. After i visible=false the child, the handler gets added back, but now it calls 2 times the event attached instead of 1, and so on with every on/off of the child (3, 4, 5)... why? :doh: thanks a lot for your time. daniel

      C Offline
      C Offline
      CKnig
      wrote on last edited by
      #2

      Ok: the problem is with the if (this.Visible), because this will be Form1 if the child is - well - a child of Form1 and you are handling the VisibleChanged event there. Just replace this with child.Visible or (Control)sender (I am assuming you didn't change the event-param-names for the second one. May I ask: why doing it this way? Just put if (child.Visible == true) return; to the From1_KeyPressed - handler.

      D 1 Reply Last reply
      0
      • C CKnig

        Ok: the problem is with the if (this.Visible), because this will be Form1 if the child is - well - a child of Form1 and you are handling the VisibleChanged event there. Just replace this with child.Visible or (Control)sender (I am assuming you didn't change the event-param-names for the second one. May I ask: why doing it this way? Just put if (child.Visible == true) return; to the From1_KeyPressed - handler.

        D Offline
        D Offline
        dsovino
        wrote on last edited by
        #3

        wow, faster than i could edit it. I accidentaly posted it before finishing it out. No, i'm not handling the visiblechange on form1 (parent), just on the child. this.visible actually refers to the child. i had to do it this way because the child form has a "searcher" textbox to a "SELECT blabla like" query on the textchange to a database.

        1 Reply Last reply
        0
        • D dsovino

          Hi, I want to disable a keypress handler of a mdi container form. I'm adding this on the container's Form_load: this.KeyPress += new KeyPressEventHandler(Form1_KeyPressed); and this on the child's VisibleChange event: if (this.Visible) { principal.KeyPress -= new KeyPressEventHandler(principal.Form1_KeyPressed); } else { principal.KeyPress += new KeyPressEventHandler(principal.Form1_KeyPressed); } where principal is a property assigned on the child's load, in order to call the objects on the parent form: principal = (FormMain)this.MdiParent; When i run this the first time works. The handler works until I turn visible the child form for the first time (which tells me the -= is working), removing the parent's handler. After i visible=false the child, the handler gets added back, but now it calls 2 times the event attached instead of 1, and so on with every on/off of the child (3, 4, 5)... why? :doh: thanks a lot for your time. daniel

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

          Hi. i'm not sure if you have to do it that way, but if i have to unsubscribe eventhandlers i use to keep them as member variables in my class:

          public MyForm:Form
          {
          // some code here;
          private KeyPressEventHandler myHandler;
          public MyForm()
          {
          //...
          InitializeComponent();
          myHandler = new KeyPressEventHandler(Form1_KeyPressed);
          subscribe();
          //...
          }
          public void subscribe()
          {
          this.KeyPress += myHandler;
          }
          public void unsubscribe()
          {
          this.KeyPress -= myHandler;
          }
          private void Form1_KeyPressed(object sender, KeyPressEventArgs e)
          {
          // some stuff here..
          }
          }

          and from your MDI Child form you do simply call now the subscribe() and unsubscribe() methods. hope this helps m@u

          D 1 Reply Last reply
          0
          • L Lost User

            Hi. i'm not sure if you have to do it that way, but if i have to unsubscribe eventhandlers i use to keep them as member variables in my class:

            public MyForm:Form
            {
            // some code here;
            private KeyPressEventHandler myHandler;
            public MyForm()
            {
            //...
            InitializeComponent();
            myHandler = new KeyPressEventHandler(Form1_KeyPressed);
            subscribe();
            //...
            }
            public void subscribe()
            {
            this.KeyPress += myHandler;
            }
            public void unsubscribe()
            {
            this.KeyPress -= myHandler;
            }
            private void Form1_KeyPressed(object sender, KeyPressEventArgs e)
            {
            // some stuff here..
            }
            }

            and from your MDI Child form you do simply call now the subscribe() and unsubscribe() methods. hope this helps m@u

            D Offline
            D Offline
            dsovino
            wrote on last edited by
            #5

            nope, didn't help, i keep getting the same problem.... i have to do it this way, if i try to go with the return way on the handler, the textbox that i want to work on doesn't get the typed text...

            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