tags:

views:

32

answers:

3

Hello, in my Win Forms app I create an array of dynamic custom controls inside a loop. These, lets call them 'boxes', are like my basic pieces of information. I also create string arrays in other parts of the code that contain the information of this 'boxes', so that for example string[3] is a variable of box[3] and so does stringa[3], stringb[3], stringc[3]... all the arrays with the same index are related to the box with that index. Hope I make myself clear.

Only 2 of this strings are shown in 2 labels inside each custom control 'box' in the array, but the others are there because I want to make something so that when the user clicks one of these controls the other strings can be shown in another control. Sort of something like "More Information...". All the 'boxes' in the array need to have the same event handler because I create +100.

To put it more into context, each custom control 'box' in the array shows the Symbol and the Price of a stock and I want that when the user clicks on each stock more quote information is shown on another special control which is like a placeholder for "More info".

I am thinking of 2 ways to do it:

  • If I could "detect" the index of the clicked control (which is the same in the strings related to it), I could just set this to an int j and all I have to do is show all the strings a,b,c... with index j. Unfortunately I cannot find a way to do this, maybe it is not even possible.
  • The other way I have thought is to create some properties for my custom control which "store" this variables, and in my app instead of assigning strings I would set properties for each control, which I could later retrieve when the control is clicked. I haven't tryed this because I don't know exactly how to do it.

What do you think? Do you know how can I achieve this or do you have a different idea that will work? Please help! Thanks in advance.

+1  A: 

It's kind of a broad implementation question since there are countless ways you could implement something like this.

If you are creating two collections, one with the buttons and one with the information, you potentially could just assign each of the buttons 'Tag' properties to point to the corresponding info and assign a generic OnClick event handler that displays the info.. something like:

infoControl.text = ((InfoClass)((Button)Sender.Tag)).pieceOfInformation;

But again there are many ways to do this, and the choice comes down to how you store your information.

ach
A: 

Hi, i don't know about the first way - got to noodle around more, but in the second way you can extended your custom or built-in control: for example:

public class ExtendedLabel: Label
{
    public string[] MoreInfo { get; set; }
}

and initialize it

public TestForm()
{
    InitializeComponent();
    ExtendedLabel label = new ExtendedLabel();
    label.MoreInfo = new string[] { "test" };
    this.Controls.Add(label);
    label.AutoSize = true;
    label.Location = new System.Drawing.Point(120, 87);
    label.Name = "label1";
    label.Size = new System.Drawing.Size(35, 13);
    label.TabIndex = 0;
    label.Text = label.MoreInfo[0];            
}

And later in your event handler you can use the inside information

Andriy Shvay
A: 

For your first method, you could have a property of your custom control that is the index.

public class Box : Control
{
   // ...existing code

   private int index;
   public int Index
   {
      get
      {
         return index;
      }
      set
      {
         index = value;
      }
   }

}

OR

For your second method, you could have a property of your custom control that is the additional info string.

public class Box : Control
{
   // ...existing code

   private string extraInfo;
   public string ExtraInfo
   {
      get
      {
         return extraInfo;
      }
      set
      {
         extraInfo = value;
      }
   }

}

In either case, you could then access the proper information right in your click handler for the "box".

msergeant
Great idea the first one! I will try it...
VerizonW