2009-08-20 76 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