在 mousehoover 中向方法添加参数
Adding parameters to method in mousehoover
我在制作鼠标吸尘器方法时遇到了问题。该代码首先是一个 for 循环,我在其中创建图片框,在同一个 for 循环中,我想在循环中使用 x 和 y 值。
这是我创建图片框的代码:
for (int x = 0; x < 9; x++){
for (int y = 0; y < 9; y++){
pb[x, y] = new PictureBox();
pb[x, y].Location = new Point(x * 50 + 100, y * 50 + 100);
pb[x, y].Width = 50;
pb[x, y].Height = 50;
pb[x, y].Visible = true;
pb[x, y].BorderStyle = BorderStyle.FixedSingle;
pb[x, y].BringToFront();
this.Controls.Add(pb[x, y]);
EventArgs e = new EventArgs();
object sender;
pb[x, y].MouseHover += new EventHandler(actionmousehoover(x, y, object sender));
}
}
名为 actionmousehoover 的方法是这个:
private void actionmousehoover(int x, int y,object sender, System.EventArgs e){
resethoovercolors();
//Om vertikal
if(vertorhoriz = false){
if(y+1 < 10 || y-1 < 1){
pb[x, y].BackColor = Color.Red;
pb[x, y + 1].BackColor = Color.Red;
pb[x, y - 1].BackColor = Color.Red;
}
}
if(vertorhoriz = true){
if(x+1 < 10 || y-1 < 1){
pb[x, y].BackColor = Color.Red;
pb[x + 1, y].BackColor = Color.Red;
pb[x - 1, y].BackColor = Color.Red;
}
}
}
与此相关的部分:
pb[x, y].MouseHover += new EventHandler(actionmousehoover(x, y));
工作正常,而且 actionmousehoover 方法没有 x 和 y 作为参数。
首先,你不能添加比(object sender和EventArgs e)更多的参数,因为它们是C#预定义的
试试这个
pb[x, y].MouseHover += new EventHandler(this.PB_MouseHover);
private void PB_MouseHover(object sender, EventArgs e)
{
actionmousehoover(x, y)
}
private void actionmousehoover(int x, int y)
{
resethoovercolors();
//Om vertikal
if(vertorhoriz = false)
{
if(y+1 < 10 || y-1 < 1)
{
pb[x, y].BackColor = Color.Red;
pb[x, y + 1].BackColor = Color.Red;
pb[x, y - 1].BackColor = Color.Red;
}
}
if(vertorhoriz = true)
{
if(x+1 < 10 || y-1 < 1)
{
pb[x, y].BackColor = Color.Red;
pb[x + 1, y].BackColor = Color.Red;
pb[x - 1, y].BackColor = Color.Red;
}
}
}
您可以通过创建您自己的继承自 PictureBox 的图片框控件,然后添加您自己需要的属性来实现此目的。
public class MyPictureBox : PictureBox
{
public int X { get; set; }
public int Y { get; set; }
}
MyPictureBox box = new MyPictureBox()
{
Name = "pcBox1",
X = 1,
Y = 2
};
box.MouseHover += pictureBox1_MouseHover;
然后在事件处理程序中,将发送者转换为您的控件并访问属性。
private void pictureBox1_MouseHover(object sender, EventArgs e)
{
var x = ((MyPictureBox)sender).X;
var y = ((MyPictureBox)sender).Y;
}
希望对您有所帮助
您不能向事件处理程序添加参数;但是,您可以创建自己的包含这些参数的派生 EventArgs
class。
public class PictureBoxEventArgs : EventArgs
{
public PictureBoxEventArgs(int x, int y)
{
X = x;
Y = y;
}
public int X { get; }
public int Y { get; }
}
要让图片框使用此 class,您必须创建自己的扩展图片框 class 并覆盖 OnMouseHover
事件触发器。 (这就是传递空 EventArgs
的真正原因:允许您扩展它们。)
public class PictureBoxEx : PictureBox
{
protected override void OnMouseHover(EventArgs e)
{
var args = new PictureBoxEventArgs((Left - 100) / 50, (Top - 100) / 50);
base.OnMouseHover(args);
}
}
为了得到数组索引,我们对new Point(x * 50 + 100, y * 50 + 100)
的坐标进行逆计算。
现在,您可以使用
使用此事件
private void PictureBox_MouseHover(object sender, EventArgs e)
{
if (e is PictureBoxEventArgs args) {
actionmousehoover(args.X, args.Y);
}
}
或通过委托订阅活动
pb[x, y].MouseHover += (s, e) => {
if (e is PictureBoxEventArgs args) {
actionmousehoover(args.X, args.Y);
}
};
我在制作鼠标吸尘器方法时遇到了问题。该代码首先是一个 for 循环,我在其中创建图片框,在同一个 for 循环中,我想在循环中使用 x 和 y 值。
这是我创建图片框的代码:
for (int x = 0; x < 9; x++){
for (int y = 0; y < 9; y++){
pb[x, y] = new PictureBox();
pb[x, y].Location = new Point(x * 50 + 100, y * 50 + 100);
pb[x, y].Width = 50;
pb[x, y].Height = 50;
pb[x, y].Visible = true;
pb[x, y].BorderStyle = BorderStyle.FixedSingle;
pb[x, y].BringToFront();
this.Controls.Add(pb[x, y]);
EventArgs e = new EventArgs();
object sender;
pb[x, y].MouseHover += new EventHandler(actionmousehoover(x, y, object sender));
}
}
名为 actionmousehoover 的方法是这个:
private void actionmousehoover(int x, int y,object sender, System.EventArgs e){
resethoovercolors();
//Om vertikal
if(vertorhoriz = false){
if(y+1 < 10 || y-1 < 1){
pb[x, y].BackColor = Color.Red;
pb[x, y + 1].BackColor = Color.Red;
pb[x, y - 1].BackColor = Color.Red;
}
}
if(vertorhoriz = true){
if(x+1 < 10 || y-1 < 1){
pb[x, y].BackColor = Color.Red;
pb[x + 1, y].BackColor = Color.Red;
pb[x - 1, y].BackColor = Color.Red;
}
}
}
与此相关的部分:
pb[x, y].MouseHover += new EventHandler(actionmousehoover(x, y));
工作正常,而且 actionmousehoover 方法没有 x 和 y 作为参数。
首先,你不能添加比(object sender和EventArgs e)更多的参数,因为它们是C#预定义的
试试这个
pb[x, y].MouseHover += new EventHandler(this.PB_MouseHover);
private void PB_MouseHover(object sender, EventArgs e)
{
actionmousehoover(x, y)
}
private void actionmousehoover(int x, int y)
{
resethoovercolors();
//Om vertikal
if(vertorhoriz = false)
{
if(y+1 < 10 || y-1 < 1)
{
pb[x, y].BackColor = Color.Red;
pb[x, y + 1].BackColor = Color.Red;
pb[x, y - 1].BackColor = Color.Red;
}
}
if(vertorhoriz = true)
{
if(x+1 < 10 || y-1 < 1)
{
pb[x, y].BackColor = Color.Red;
pb[x + 1, y].BackColor = Color.Red;
pb[x - 1, y].BackColor = Color.Red;
}
}
}
您可以通过创建您自己的继承自 PictureBox 的图片框控件,然后添加您自己需要的属性来实现此目的。
public class MyPictureBox : PictureBox
{
public int X { get; set; }
public int Y { get; set; }
}
MyPictureBox box = new MyPictureBox()
{
Name = "pcBox1",
X = 1,
Y = 2
};
box.MouseHover += pictureBox1_MouseHover;
然后在事件处理程序中,将发送者转换为您的控件并访问属性。
private void pictureBox1_MouseHover(object sender, EventArgs e)
{
var x = ((MyPictureBox)sender).X;
var y = ((MyPictureBox)sender).Y;
}
希望对您有所帮助
您不能向事件处理程序添加参数;但是,您可以创建自己的包含这些参数的派生 EventArgs
class。
public class PictureBoxEventArgs : EventArgs
{
public PictureBoxEventArgs(int x, int y)
{
X = x;
Y = y;
}
public int X { get; }
public int Y { get; }
}
要让图片框使用此 class,您必须创建自己的扩展图片框 class 并覆盖 OnMouseHover
事件触发器。 (这就是传递空 EventArgs
的真正原因:允许您扩展它们。)
public class PictureBoxEx : PictureBox
{
protected override void OnMouseHover(EventArgs e)
{
var args = new PictureBoxEventArgs((Left - 100) / 50, (Top - 100) / 50);
base.OnMouseHover(args);
}
}
为了得到数组索引,我们对new Point(x * 50 + 100, y * 50 + 100)
的坐标进行逆计算。
现在,您可以使用
使用此事件private void PictureBox_MouseHover(object sender, EventArgs e)
{
if (e is PictureBoxEventArgs args) {
actionmousehoover(args.X, args.Y);
}
}
或通过委托订阅活动
pb[x, y].MouseHover += (s, e) => {
if (e is PictureBoxEventArgs args) {
actionmousehoover(args.X, args.Y);
}
};