请问关于c的具体应用场景有哪些?
- 内容介绍
- 相关推荐
本文共计391个文字,预计阅读时间需要2分钟。
您可以使用以下代码来创建一个包含20个PictureBox控件的Panel,并允许用户通过点击任何控件来调用Panel中的方法:
csharppublic class MyPanel : Panel{ public MyPanel() { for (int i=0; i <20; i++) { PictureBox pictureBox=new PictureBox(); pictureBox.Size=new Size(100, 100); // 设置PictureBox的大小 pictureBox.Location=new Point(i % 5 * 110, i / 5 * 110); // 设置PictureBox的位置 pictureBox.Click +=PictureBox_Click; // 为PictureBox添加点击事件 Controls.Add(pictureBox); } }
private void PictureBox_Click(object sender, EventArgs e) { PictureBox clickedPictureBox=sender as PictureBox; if (clickedPictureBox !=null) { // 在这里调用Panel的方法 // 例如:DoSomething(); } }}
我有一个包含20个PictureBox控件的Panel.如果用户点击任何控件,我希望调用Panel中的方法.我该怎么做呢?
public class MyPanel : Panel { public MyPanel() { for(int i = 0; i < 20; i++) { Controls.Add(new PictureBox()); } } // DOESN'T WORK. // function to register functions to be called if the pictureboxes are clicked. public void RegisterFunction( <function pointer> func ) { foreach ( Control c in Controls ) { c.Click += new EventHandler( func ); } } }
我如何实现RegisterFunction()?
此外,如果有很酷的C#功能可以使代码更优雅,请分享.
public void RegisterFunction(EventHandler func) { foreach (Control c in Controls) { c.Click += func; } }
用法:
public MyPanel() { for (int i = 0; i < 20; i++) { Controls.Add(new PictureBox()); } RegisterFunction(MyHandler); }
请注意,这会将EventHandler委托添加到每个控件,而不仅仅是PictureBox控件(如果还有其他控件).更好的方法是在创建PictureBox控件时添加事件处理程序:
public MyPanel() { for (int i = 0; i < 20; i++) { PictureBox p = new PictureBox(); p.Click += MyHandler; Controls.Add(p); } }
EventHandler委托指向的方法如下所示:
private void MyHandler(object sender, EventArgs e) { // this is called when one of the PictureBox controls is clicked }
本文共计391个文字,预计阅读时间需要2分钟。
您可以使用以下代码来创建一个包含20个PictureBox控件的Panel,并允许用户通过点击任何控件来调用Panel中的方法:
csharppublic class MyPanel : Panel{ public MyPanel() { for (int i=0; i <20; i++) { PictureBox pictureBox=new PictureBox(); pictureBox.Size=new Size(100, 100); // 设置PictureBox的大小 pictureBox.Location=new Point(i % 5 * 110, i / 5 * 110); // 设置PictureBox的位置 pictureBox.Click +=PictureBox_Click; // 为PictureBox添加点击事件 Controls.Add(pictureBox); } }
private void PictureBox_Click(object sender, EventArgs e) { PictureBox clickedPictureBox=sender as PictureBox; if (clickedPictureBox !=null) { // 在这里调用Panel的方法 // 例如:DoSomething(); } }}
我有一个包含20个PictureBox控件的Panel.如果用户点击任何控件,我希望调用Panel中的方法.我该怎么做呢?
public class MyPanel : Panel { public MyPanel() { for(int i = 0; i < 20; i++) { Controls.Add(new PictureBox()); } } // DOESN'T WORK. // function to register functions to be called if the pictureboxes are clicked. public void RegisterFunction( <function pointer> func ) { foreach ( Control c in Controls ) { c.Click += new EventHandler( func ); } } }
我如何实现RegisterFunction()?
此外,如果有很酷的C#功能可以使代码更优雅,请分享.
public void RegisterFunction(EventHandler func) { foreach (Control c in Controls) { c.Click += func; } }
用法:
public MyPanel() { for (int i = 0; i < 20; i++) { Controls.Add(new PictureBox()); } RegisterFunction(MyHandler); }
请注意,这会将EventHandler委托添加到每个控件,而不仅仅是PictureBox控件(如果还有其他控件).更好的方法是在创建PictureBox控件时添加事件处理程序:
public MyPanel() { for (int i = 0; i < 20; i++) { PictureBox p = new PictureBox(); p.Click += MyHandler; Controls.Add(p); } }
EventHandler委托指向的方法如下所示:
private void MyHandler(object sender, EventArgs e) { // this is called when one of the PictureBox controls is clicked }

