0

我有一组以编程方式生成的标签。我想更改标签的背景和文本,同时:

1-mouse_click 事件
2-鼠标单击并移动到其他标签(我希望用户可以一键更改一些标签(按住它)并移动到相邻标签。我应该将哪个事件用于第二个目的?我使用代码下面是 mouse_click 事件:

    private void labels_Click(object sender, EventArgs e)
    {
        Label lbl = (Label)sender;
        if (lbl.Text == "1")
        {
            lbl.Text = "0";
            lbl.BackColor = Color.FromArgb(225, 0, 0);
        }
        else
        {
            lbl.Text = "1";
            lbl.BackColor = Color.FromArgb(224, 224, 226);
        }
        SetHexNumbers();
    }

在此处输入图像描述

4

1 回答 1

3

您应该使用MouseEnter,但不要忘记将Capturesender 的属性设置为false

试试这个:

bool isMouseDown;

private void labels_Click(object sender, EventArgs e)
{
    DoAction(sender);
}

private void labels_MouseDown(object sender, MouseEventArgs e)
{
    isMouseDown = true;
    DoAction(sender);
}

private void labels_MouseUp(object sender, MouseEventArgs e)
{
    isMouseDown = false;
}


private void label_MouseEnter(object sender, EventArgs e)
{
    if (isMouseDown)
    {
        DoAction(sender);
    }
}

private void DoAction(object sender)
{
    Label lbl = (Label)sender;
    lbl.Capture = false;           //DO NOT FORGET THIS LINE

    if (lbl.Text == "1")
    {
        lbl.Text = "0";
        lbl.BackColor = Color.FromArgb(225, 0, 0);
    }
    else
    {
        lbl.Text = "1";
        lbl.BackColor = Color.FromArgb(224, 224, 226);
    }
    SetHexNumbers();
}
于 2013-03-29T17:27:02.670 回答