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. How to dail with controls at runtime?

How to dail with controls at runtime?

Scheduled Pinned Locked Moved C#
dockerhelptutorialquestion
3 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.
  • M Offline
    M Offline
    mohamad1088
    wrote on last edited by
    #1

    I have made my own control inherited from System.Windows.Forms.UserControl then by using reflection I have created an object from my own control at run time after creation this control have been added to the collection of the container(form) System.Windows.Forms.Form.Controls. the problem is When the control is in the form (before runtime) it's easy to handel any of its events but now how to handel this control's events as (MouseDown , MouseUp , DragDrop , DragEnter and DragOver) as this control has never been created yet ?

    D L 2 Replies Last reply
    0
    • M mohamad1088

      I have made my own control inherited from System.Windows.Forms.UserControl then by using reflection I have created an object from my own control at run time after creation this control have been added to the collection of the container(form) System.Windows.Forms.Form.Controls. the problem is When the control is in the form (before runtime) it's easy to handel any of its events but now how to handel this control's events as (MouseDown , MouseUp , DragDrop , DragEnter and DragOver) as this control has never been created yet ?

      D Offline
      D Offline
      Dr Emmett Brown
      wrote on last edited by
      #2

      So, somewhere in your code you have something like this:

      YourControl control = Activator.CreateInstance(...);
      this.Controls.Add(control);

      You have to associate the events after creating the object, before the control variable goes out of scope:

      YourControl control = Activator.CreateInstance(...);
      this.Controls.Add(control);

      control.MouseDown += ... ;
      control.MouseUp += ... ;

      After writing += press tab and visual studio completes it for you. However if the you are trying to do something internal to the YourControl class, you should encapsulate the behavior in the class, associating the events in the constructor. Cheers,

      rotter

      1 Reply Last reply
      0
      • M mohamad1088

        I have made my own control inherited from System.Windows.Forms.UserControl then by using reflection I have created an object from my own control at run time after creation this control have been added to the collection of the container(form) System.Windows.Forms.Form.Controls. the problem is When the control is in the form (before runtime) it's easy to handel any of its events but now how to handel this control's events as (MouseDown , MouseUp , DragDrop , DragEnter and DragOver) as this control has never been created yet ?

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

        Write the event handler beforehand, so you have something like this in your code:

        public void ControlEventHandler(object sender, EventArgs args)
        {
        }

        and repeat this also for the mouse/drag event handlers. Next, when you create your control at runtime, simply assign these event handlers, like that:

        Control ctl = new Button(); // some dynamic happening here
        ctl.Click += new EventHandler(ControlEventHandler);
        this.Controls.Add(ctl); // add this control at runtime to our control

        regards

        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