简体   繁体   中英

how to change a control property during mousedown and mousemove event?

I have a set of labels that are generated programmatically. I want to change the background and text of labels while:

1-the mouse_click event
2-the mouse click and move to other labels (I want that user can change some labels with one click (hold it) and move to neighbor labels. which event should I use for second purpose? I use the code below for mouse_click event:

    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();
    }

在此处输入图片说明

You should use MouseEnter , but don't forget to set the Capture property of the sender to false .

Try this:

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM