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. Multiple controls at runtime problem

Multiple controls at runtime problem

Scheduled Pinned Locked Moved C#
graphicshelp
3 Posts 2 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.
  • R Offline
    R Offline
    Radgar
    wrote on last edited by
    #1

    Hi all, here is what I do and need; - I create multiple controls at runtime: (myControl1, myControl2, myControl3...) - I have only one event definition for all my controls: (myControl_Enter) - Everything works fine but what I need is: when the myControl_Enter event is fired, I want "only" that instance of the control to change backcolor. below is the part of the code needed: private int counter = 0; public void AddNewMyControl() { counter += 1; myClass.MyControl myControl = new myClass.MyControl(); myControl.Dock = System.Windows.Forms.DockStyle.Top; myControl.Location = new System.Drawing.Point(0, 0); myControl.Name = "myControl" + counter; myControl.Size = new System.Drawing.Size(536, 168); myControl.Enter += new System.EventHandler(myControl_Enter); this.Controls.Add(myControl); } private void myControl_Enter(object sender, System.EventArgs e) { //change the backcolor of only this instance of myControl } thank you in advance. Radgar "Imagination is more important than knowledge." - Albert Einstein

    G 1 Reply Last reply
    0
    • R Radgar

      Hi all, here is what I do and need; - I create multiple controls at runtime: (myControl1, myControl2, myControl3...) - I have only one event definition for all my controls: (myControl_Enter) - Everything works fine but what I need is: when the myControl_Enter event is fired, I want "only" that instance of the control to change backcolor. below is the part of the code needed: private int counter = 0; public void AddNewMyControl() { counter += 1; myClass.MyControl myControl = new myClass.MyControl(); myControl.Dock = System.Windows.Forms.DockStyle.Top; myControl.Location = new System.Drawing.Point(0, 0); myControl.Name = "myControl" + counter; myControl.Size = new System.Drawing.Size(536, 168); myControl.Enter += new System.EventHandler(myControl_Enter); this.Controls.Add(myControl); } private void myControl_Enter(object sender, System.EventArgs e) { //change the backcolor of only this instance of myControl } thank you in advance. Radgar "Imagination is more important than knowledge." - Albert Einstein

      G Offline
      G Offline
      gnjunge
      wrote on last edited by
      #2

      The sender object that is sent as one of the arguments of the event is a reference to the object throwing the event. so you only have to cast the sender object and set the background color. Like this: private void myControl_Enter(object sender, System.EventArgs e) { ((myClass.MyControl)sender).BackColor = System.Drawing.Color.Green; } Gidon

      R 1 Reply Last reply
      0
      • G gnjunge

        The sender object that is sent as one of the arguments of the event is a reference to the object throwing the event. so you only have to cast the sender object and set the background color. Like this: private void myControl_Enter(object sender, System.EventArgs e) { ((myClass.MyControl)sender).BackColor = System.Drawing.Color.Green; } Gidon

        R Offline
        R Offline
        Radgar
        wrote on last edited by
        #3

        Thank you Gidon. You saved me lots of time. Radgar "Imagination is more important than knowledge." - Albert Einstein

        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