views:

39

answers:

4

Hello, i was wondering how could i add .customString to PictureBox object.
Something like:

PictureBox box = new PictureBox();
box.CustomString = "string here";

And then later on i would be access it.

MessageBox.Show(boxname.CustomString);

Thank you.

+2  A: 

The easiest way to do it is to use the Tag property:

PictureBox box = new PictureBox();
box.Tag = "string here";

And, later:

MessageBox.Show((string)box.Tag);
Mike Caron
+3  A: 

If you want to add a property to an existing control the best way would be to derive MyCustomPictureBox from PictureBox and add the new property to your derived version:

public class MyCustomPictureBox : PictureBox
{

    public string CustomString {get; set;}

}
Dave Swersky
Thank you. This helped.
Semas
A: 
public class MyPictureBox : PictureBox
{
   public MyPictureBox(...) :base(....) {}   // duplicated ctors

   public string   CustomString {get; set;}
}

Now, using it will be a bit trickier. If you've created the original picturebox by drag'n'dropping it in the Winforms designer, then you'll have to go into the myform.designer.cs file, and replace the instances of "PictureBox" with "MyPictureBox"

James Curran
A: 

Hi,

You could create a new class called MyPictureBox which derives from PictureBox. In the new class you can add your custom property. Something like the below.

public class MyPictureBox : PictureBox
{
  public MyPictureBox():base()
  {}

  public string CustomString
  {
    get{}
    set{}
  }
}

Now you can use the new class just like you would the PictureBox only difference is the yours has the custom property / logic.

Enjoy!

Doug