如何使用鼠标输入事件代码?

时间:2014-05-28 09:20:38

标签: c#

想象一下,我有10个按钮,我想使用鼠标输入事件来增加按钮的宽度和高度30。如果我想这样做,我必须逐个使用鼠标事件, 我怎么能只为所有按钮编写一次我的代码。我试图使用foreach循环但不确定我是否应该使用表单加载而不知道如何处理事件。 (窗体)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace main
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (Control item in Controls)
            {
                if (item is Button)
                {
                    // code
                }
            }
        }
    }
}

4 个答案:

答案 0 :(得分:2)

首先,只需 表单上的按钮,即可缩短代码。然后为MouseEnter事件添加一个事件处理程序:

foreach (Button btn in Controls.OfType<Button>())
{
    btn.MouseEnter += new System.EventHandler(btn_MouseEnter);
}

然后在事件中改变宽度:

private void btn_MouseEnter(object sender, System.EventArgs e) 
{
    var senderButton = (Button)sender;
    senderButton.Width += 30;
}

您还必须为MouseLeave添加事件处理程序+事件以再次减小宽度。否则按钮将继续增长:

foreach (Button btn in Controls.OfType<Button>())
{
    btn.MouseEnter += new System.EventHandler(btn_MouseEnter);
    btn.MouseLeave += new System.EventHandler(btn_MouseLeave );
}

private void btn_MouseEnter(object sender, System.EventArgs e) 
{
    var senderButton = (Button)sender;
    senderButton.Width += 30;
}

private void btn_MouseLeave (object sender, System.EventArgs e) 
{
    var senderButton = (Button)sender;
    senderButton.Width -= 30;
}

可以在InitializeComponent()之后或Form_Load事件中添加foreach-loop代码。

private void Form1_Load(object sender, System.EventArgs e)
{
    foreach (Button btn in Controls.OfType<Button>())
    {
        btn.MouseEnter += new System.EventHandler(btn_MouseEnter);
        btn.MouseLeave += new System.EventHandler(btn_MouseLeave );
    }
}

答案 1 :(得分:1)

您可以使用OfType扩展方法轻松遍历所有按钮,如下所示:

foreach(var button in this.Controls.OfType<Button>())
{
    button.MouseEnter += button_MouseEnter;
}

答案 2 :(得分:1)

使用函数来包装你的逻辑。

编写代码以更改方法中的权重和宽度,订阅MouseMove or whatever event that suits you

然后使用GetChildAtPoint找到按钮,以便更改其大小。

// This goes in the 
foreach(var button in this.Controls.OfType<Button>())
{
    button.Mouse += button_IncreaseSize;
}


protected override void button_IncreaseSize(MouseEventArgs e)
{
   // use GetChildAtPoint to get the control
   var button = GetChildAsPoint(new Point(e.X, e.Y));
   // Change the size of button, eg. with Scal, and with the Size property, or by chaning Height and Width propertis manually
   button.Scale(new SizeF(30, 30));
}

答案 3 :(得分:0)

或者您也可以这样做:

private void Form1_Load(object sender, EventArgs e)
{              
    foreach (Control item in this.Controls)
    {
        if (item is Button)
        {
            //item.MouseEnter += (s, ea) => { YourMethodName(); };
              item.MouseEnter += new System.EventHandler(btn_MouseEnter);
        }
    }
}

private void btn_MouseEnter(object sender, System.EventArgs e)
{
   //your code to increase width and height
    var btnSenderButton = sender as Button;
    btnSenderButton.Width += 30;
}