C# .NET集合和访问对象方法
我对GUI编程一无所知,需要一些关于pictureboxes列表的帮助 我的想法是我有一个图片盒列表。当用户单击其中一个时,我希望(例如)更改所选对象的BorderStyle属性以进行Fixed3D,但将其余集合边框更改为FixedSingle(或类似内容)。这样做的正确方法是什么?我想更大的问题是如何让一个类的方法调用另一个类的方法而不需要任何信息C# .NET集合和访问对象方法,c#,collections,C#,Collections,我对GUI编程一无所知,需要一些关于pictureboxes列表的帮助 我的想法是我有一个图片盒列表。当用户单击其中一个时,我希望(例如)更改所选对象的BorderStyle属性以进行Fixed3D,但将其余集合边框更改为FixedSingle(或类似内容)。这样做的正确方法是什么?我想更大的问题是如何让一个类的方法调用另一个类的方法而不需要任何信息 class myPicture { private int _pictureNumber; private PictureBox _box
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);
}
}
classmypicture
{
私人int_PictureEnumber;
私人影音盒;
公共myPicture(整数顺序)
{
_box=新列表();
_box.Click+=newsystem.EventHandler(box\u Click);
_PictureEnumber=订单;
}
公共样式(边框样式bs)
{
_box.BorderStyle=bs;
}
公共无效框\u单击(对象发送者,事件参数e)
{
//在这里,我想从myPicturesContainer中调用set_borders,但我不知道或不知道该实例化
}
}
类myPicturesContainer
{
私人名单(图片),;
//构造函数和其他代码省略了,实际上不需要。。。
公共无效集_边框(int i)
{
foreach(myPicture中的myPicture mp)
mp.setBorderStyle(BorderStyle.FixedSingle);
如果(i>0&&u MyPictures.Count>=i)
_MyPictures[i].setBorderStyle(BorderStyle.Fixed3d);
}
}
您需要在myPicture
类中创建一个Clicked
事件,并在单击时引发该事件。然后,您需要在myPicturesContainer
中为您拥有的每个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
}
}
classmypicture
{
单击的公共事件操作=委托{};
私人int_PictureEnumber;
公共无效框\u单击(对象发送者,事件参数e)
{
点击此按钮(此按钮);
}
}
类myPicturesContainer
{
私人名单(图片),;
公共无效集_边框(int i)
{
foreach(myPicture中的myPicture mp)
{
mp.Clicked+=图片单击;
}
}
无效图片单击(Int32 pictureId)
{
//将调用此方法,并且pictureId
//将传入已单击图片的名称
}
}
这在很大程度上取决于您使用的UI框架。例如,在WPF中,可以使用属性绑定和样式(可能还有模板)来实现。