-3

我在应用程序中有 40 个按钮,我需要自定义悬停在状态字段中显示。我已经制作了一个用于添加某个消息和一个要删除的函数,因此在悬停时,它会调用该函数,并且与离开按钮相同。

我想要 40 条不同的消息,其中一种方法是检查鼠标悬停在哪个按钮上。

if(button1.hovered == true){
  string message = "scenario1";
}
elseif(button2.hovered == true){
  scenario2...etc
}

有没有办法检查是否悬停?并在 if 语句中检查它?

我决定添加更多信息,这样可能更容易理解我的观点。

鼠标悬停时向列表视图添加消息。

    void messAdd(object sender, EventArgs e)
    {


        string now = DateTime.Now.ToString("M/d/yyyy") + " - " + DateTime.Now.ToString("HH:mm:ss tt");
        string message = "message 1";
        found = false;

        ListViewItem item = new ListViewItem(message);
        foreach (ListViewItem z in listView1.Items)
        {
            if (z.Text == message)
            { found = true; }
        }

        if (found == false)
        {
            item.SubItems.Add(now.ToString());
            listView1.Items.Add(item);
            listView1.EnsureVisible(item.Index);
        }
        else
        {
            DeleteIfNecessary(message);
        }
    }

鼠标离开时从列表框中删除消息:

    void messdel(object sender, EventArgs e)
    {


        string message = "message 1";
        found = false;

        ListViewItem item = new ListViewItem(message);
        foreach (ListViewItem z in listView1.Items)
        {
            if (z.Text == message)
            { found = true; }
        }

        if (found == true)
        {
            DeleteIfNecessary(message);
        }

    }

我可以为每个按钮制作 4 个这样的功能,但是因为我需要 40 条不同的消息,愚蠢的是,但是没有办法通过该功能发送消息参数,所以我需要使用 if 测试并检查女巫按钮是否悬停然后将消息输出到指定的按钮。我正在使用 Visual Studio 和 Windows 窗体,抱歉没有提到。

4

2 回答 2

4

There is a Control.MouseHover event. You can implement:

private void button_MouseHover(object sender, System.EventArgs e)
{
    doSomething(sender);
}

and for all of your buttons, set event handler for MouseHover to button_MouseHover in IDE, or do it in code:

this.button1.MouseHover += new System.EventHandler(this.button_MouseHover);

By checking the sender parameter you can know which button is calling the event handler.

Update

According to your update in the question, I think you can just use messAdd as the event handler for MouseEnter event for all of your buttons, and use messdel as the event handler for MouseLeave. Again, you don't need to create a copy of these two methods for all of your buttons, you just need to assign the same event handler method for all the buttons, and check sender to know who is calling the event handler - then creating different messages.

The sender is your Button object. Just cast it to a Button and access what you want (text, tag, name, etc.) to know which Button is trying to add/remove message on the list view.

Update 2

Button button = sender as Button;
if (button == null) {
    // not a button, do nothing
    return;
}

string message = String.Empty;
if (sender.Equals(button1)) {
    message = "message1";
} else if (sender.Equals(button2)) {
    message = "message2";
} ...
于 2012-04-20T11:28:58.873 回答
0

我将假设这是 WinForms,因为您没有另外指定。

您可以创建一个 inthoveredId表示悬停的按钮(值-1表示没有悬停)。创建按钮时,将 Tag 属性设置为按钮的 ID 号。

然后将每个按钮注册到这两个功能:

private void buttons_MouseEnter(object sender, System.EventArgs e)
{
    Button btn = ((Button)sender);

    hoveredId = (int)btn.Tag;
}

private void buttons_MouseLeave(object sender, System.EventArgs e)
{
    Button btn = ((Button)sender);

    if ((int)btn.Tag == hoveredId)
        hoveredId = -1;
}

检查悬停的按钮时,可以使用 switch 语句:

switch (hoveredId)
{
  case 1:
    string message = "scenario1";
    break;
  case 2:
    scenario2...etc
    break;
}
于 2012-04-20T11:35:12.593 回答