2009-08-20 64 views
0

我完全不熟悉GUI编程,需要一些图片框的帮助。.NET集合和访问对象方法

这个想法是,我有一个pictureboxes列表。当用户点击一个我想要的(例如)更改所选为Fixed3D的BorderStyle属性,但将剩余的集合边界更改为FixedSingle(或类似的东西)。什么是这样做的正确方法?我猜想更大的情况是,我如何获得一个类的方法来调用另一个类的方法而不需要任何有关它的信息?

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); 
    } 
} 
+0

这很大程度上取决于你所使用的UI框架。例如,在WPF中,您可以使用属性绑定和样式(可能还有模板)。 – 2009-08-20 20:38:17

回答

0

您需要在您的myPicture类来创建一个Clicked事件,并且点击时提高该事件。然后,您需要在您的的myPicture的每个实例中附加此事件。

这里是我的意思一个非常简单的例子:

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 
    } 
} 
+0

工作完美,谢谢! – Diego 2009-08-20 20:53:23