tags:

views:

49

answers:

1

I'm completely new to GUI programming and need a little help with a list of pictureboxes.

The idea is that I have a list of pictureboxes. When a user clicks on one I want to (for example) change the BorderStyle property of the one selected to be Fixed3D, but change the remaining collection borders to FixedSingle (or something like that). What's the proper way to do something like this? I guess the bigger picture is how do I get a method of one class to call a method of another without having any information about it?

class myPicture
{
  private int _pictureNumber;
  private PictureBox _box;
  public myPicture(int order)
  {
    _box = new List<PictureBox>();
    _box.Click += new System.EventHandler(box_click);
    _pictureNumber = order;
  }
  public void setBorderStyle(BorderStyle bs)
  {
    _box.BorderStyle = bs;
  }
  public void box_click(object sender, EventArgs e)
  {
    //here I'd like to call the set_borders from myPicturesContainer, but I don't know or have any knowledge of the instantiation
  }
}

class myPicturesContainer
{
  private List<myPicture> _myPictures;
  //constructor and other code omitted, not really needed...
  public void set_borders(int i)
  {
    foreach(myPicture mp in _MyPictures)
      mp.setBorderStyle(BorderStyle.FixedSingle);
    if(i>0 && _MyPictures.Count>=i)
      _MyPictures[i].setBorderStyle(BorderStyle.Fixed3d);
  }
}
A: 

You will need to create a Clicked event in your myPicture class and raise that event when it is clicked. Then you will need to attach to this event in your myPicturesContainer for each instance of myPicture that you have.

Here is a very simple example of what I mean:

class myPicture
{
    public event Action<Int32> Clicked = delegate { };

    private int _pictureNumber;

    public void box_click(object sender, EventArgs e)
    {
     this.Clicked(this._pictureNumber);
    }
}

class myPicturesContainer
{
    private List<myPicture> _myPictures;

    public void set_borders(int i)
    {
     foreach (myPicture mp in _myPictures)
     {
      mp.Clicked += pictureClick;
     }
    }

    void pictureClick(Int32 pictureId)
    {
     // This method will be called and the pictureId
     // of the clicked picture will be passed in
    }
}
Andrew Hare
That worked perfectly, thank you!