Like Tree1Likes
  • 1 Post By _Mike

Dynamic Control Event Handler

This is a discussion on Dynamic Control Event Handler within the C# Programming forums, part of the General Programming Boards category; Hey guys. I'm relatively new to C#. I'm only using it because my company wants a User Interface with a ...

  1. #1
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398

    Dynamic Control Event Handler

    Hey guys. I'm relatively new to C#. I'm only using it because my company wants a User Interface with a program they are requesting.

    My program is pretty basic. It generates a variable amount of Label controls depending on how many database entries there are. However, since I am dynamically generating these using the following piece of code:
    Code:
    public Label[] lblMaster = new Label[1000];
    for (i = 0; i < total; i++)
    {
    	lblMaster[i].Size = new System.Drawing.Size(100, 30);
    	lblMaster[i].Location = new System.Drawing.Point(x, y);
    	lblMaster[i].Text = _names[i];
    	lblMaster[i].TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    }
    I removed the code which determines x and y, its not important. But back to my problem, I don't know how to capture when each object is Clicked. I read online I could do something like this:
    Code:
    lblALabel.Click += new EventHandler(something);
    I'm not sure how to go about this. Essentially every object that will be clicked will do the same thing, but just return different information depending on its index.

    Is there a way I can write a catch all event handler that knows the index of the Label that was clicked?

    Thanks!

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    An EventHandler has 'object sender' as the first argument. You can use that to determine which label that triggered the event.


    Quote Originally Posted by carrotcake1029 View Post
    Code:
    public Label[] lblMaster = new Label[1000];
    I suggest using a List<> for this. If 'total' isn't 1000 then you are either wasting memory or causing a buffer overflow, depending on which side of 1000 it is.
    Last edited by _Mike; 06-23-2011 at 06:50 PM.
    carrotcake1029 likes this.

  3. #3
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Great, I got it to work. Thanks.

    Ya, that 1000 was just a temporary solution until I found a proper way to do it. Thanks for the suggestions!

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    Did you look at a DataGridView control bound to your data source?

    Does a lot of Excel like functionality automatically.

    Default Functionality in the Windows Forms DataGridView Control
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    I'll look into it. I feel like you noticed I was calculating the spots manually, hehe. I'm used to doing things the hard way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. button array event handler
    By taniaj in forum C# Programming
    Replies: 2
    Last Post: 03-29-2011, 05:13 PM
  2. event handler
    By hkl01 in forum C# Programming
    Replies: 1
    Last Post: 06-10-2006, 08:07 PM
  3. Finding controls from an event handler
    By bennyandthejets in forum C# Programming
    Replies: 2
    Last Post: 07-03-2005, 04:17 AM
  4. event handler for an array of buttons
    By GanglyLamb in forum C# Programming
    Replies: 4
    Last Post: 04-12-2005, 09:52 AM
  5. confused with args for event handler~
    By black in forum C# Programming
    Replies: 3
    Last Post: 03-03-2004, 02:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21