1

I am creating a winforms app which generates a number of panels at runtime. I want each panel, when clicked, to open a web link.

The panels are generated at runtime:

for (int i = 0; i < meetings.Count(); i++) {        
 Panel door = new Panel();
 door.Location = new System.Drawing.Point((i * 146) + (i * 10) + 10, 10);
 door.Size = new System.Drawing.Size(146, 300);
 door.BackgroundImage = ConferenceToolkit.Properties.Resources.Door;
 door.Click += new EventHandler(door_Click);
 Controls.Add(door);
}

and I want the event handler to point to a URL that is stored somehow in the Panel attributes. (On a web form I could use Attributes["myAttribute"] but this doesn't seem to work with WinForms):

 private void door_Click(object sender, EventArgs e)
    {
        Panel p = sender as Panel;
        Process.Start(p.Attributes["url"]);
    }
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
Ben
  • 4,281
  • 8
  • 62
  • 103

2 Answers2

2

There are many options for this, you may store URL in the (unused in Panel) Text property:

door.Text = FindUrl(meetings[i]);

Used like:

Process.Start(p.Text);

As alternative you may use general purpose Tag property:

door.Tag = FindUrl(meetings[i]);

With:

Process.Start(p.Tag.ToString());

Tag property is usually right place for these things and, becauase it's of type object, you can even use it to store complex types (in case you need more than a simple string).

See also similar posts for slightly more complex cases: this, this and this.

Community
  • 1
  • 1
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
1

You can store the URL that you want in the Panel's Tag propertie

for example

p.Tag = "www.google.com";

and then you can use it when use cast the Panel in the on click method

reference for the .Tag property

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.tag(v=vs.110).aspx

nonamer92
  • 1,887
  • 1
  • 13
  • 24