Ave
Ave

Reputation: 4430

Can't capture Label_Mouse event in Winform C#

I have 2 Label; I want to capture Label to change Text of this Label.

You can see this gif to imagine easy.

Gif description

This gif image creates 2 Label, but I only change the text of first Label. Can't change the text of second Label.

I think the problem is SelectedControl can't detect control is selected.

My code in all event MouseDown, MouseMove,....

private void control_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        panel2.Invalidate(); //unselect other control
        SelectedControl = (Control) sender;
        Control control = (Control) sender;
        mouseX = -e.X;
        mouseY = -e.Y;
        control.Invalidate();
        DrawControlBorder(sender);
        propertyGrid1.SelectedObject = SelectedControl;
        control.TextChanged += new System.EventHandler(this.txtIDImg_TextChanged);
    }
}

This will create new Label when click button to add new Label (btnAddCharacter).

private Label SelectedLabel;
List<Label> activeLabels = new List<Label>();
public bool btnAddCharacterClick = false;
public List<String> lstLabelName = new List<string>();
public void btnAddCharacter_Click(object sender, EventArgs e)
{
    txtIDImg.Focus();
    SelectedLabel = new Label();
    Random rnd = new Random();
    int randNumber = rnd.Next(1, 1000);
    String LableName = "Lbl_" + activeLabels.Count();
    panel2.Invalidate(); //unselect other control
    SelectedControl = SelectedLabel;
    SelectedLabel.Invalidate();
    DrawControlBorder(SelectedLabel);
    SelectedLabel.Name = LableName;
    SelectedLabel.AutoSize = true;
    SelectedLabel.Text = txtIDImg.Text;
    SelectedLabel.BackColor = Color.Transparent;
    SelectedLabel.MouseEnter += new EventHandler(control_MouseEnter);
    SelectedLabel.MouseLeave += new EventHandler(control_MouseLeave);
    SelectedLabel.MouseDown += new MouseEventHandler(control_MouseDown);
    SelectedLabel.MouseMove += new MouseEventHandler(control_MouseMove);
    SelectedLabel.MouseUp += new MouseEventHandler(control_MouseUp);
    panel2.Controls.Add(SelectedLabel);
    activeLabels.Add(SelectedLabel);
    lstLabelName.Add(SelectedLabel.ToString());
    btnSave.Enabled = true;
    btnDelete.Enabled = true;
    btnDeleteAll.Enabled = true;
    btnAddCharacterClick = true;
    txtIDImg.Text = "";
}

Point postPoint;
private void control_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        postPoint = new Point();
        Control control = (Control) sender;
        Point nextPosition = new Point();
        nextPosition = panel2.PointToClient(MousePosition);
        nextPosition.Offset(mouseX, mouseY);
        control.Location = nextPosition;
        postPoint = nextPosition;        
        Invalidate();
    }
}

private void DrawControlBorder(object sender)
{
    Control control = (Control) sender;
    Rectangle Border = new Rectangle(
        new Point(control.Location.X - DRAG_HANDLE_SIZE/2,
            control.Location.Y - DRAG_HANDLE_SIZE/2),
        new Size(control.Size.Width + DRAG_HANDLE_SIZE,
            control.Size.Height + DRAG_HANDLE_SIZE));
    Rectangle NW = new Rectangle(
        new Point(control.Location.X - DRAG_HANDLE_SIZE,
            control.Location.Y - DRAG_HANDLE_SIZE),
        new Size(DRAG_HANDLE_SIZE, DRAG_HANDLE_SIZE));
    Rectangle N = new Rectangle(
        new Point(control.Location.X + control.Width/2 - DRAG_HANDLE_SIZE/2,
            control.Location.Y - DRAG_HANDLE_SIZE),
        new Size(DRAG_HANDLE_SIZE, DRAG_HANDLE_SIZE));
    Rectangle NE = new Rectangle(
        new Point(control.Location.X + control.Width,
            control.Location.Y - DRAG_HANDLE_SIZE),
        new Size(DRAG_HANDLE_SIZE, DRAG_HANDLE_SIZE));
    Rectangle W = new Rectangle(
        new Point(control.Location.X - DRAG_HANDLE_SIZE,
            control.Location.Y + control.Height/2 - DRAG_HANDLE_SIZE/2),
        new Size(DRAG_HANDLE_SIZE, DRAG_HANDLE_SIZE));
    Rectangle E = new Rectangle(
        new Point(control.Location.X + control.Width,
            control.Location.Y + control.Height/2 - DRAG_HANDLE_SIZE/2),
        new Size(DRAG_HANDLE_SIZE, DRAG_HANDLE_SIZE));
    Rectangle SW = new Rectangle(
        new Point(control.Location.X - DRAG_HANDLE_SIZE,
            control.Location.Y + control.Height),
        new Size(DRAG_HANDLE_SIZE, DRAG_HANDLE_SIZE));
    Rectangle S = new Rectangle(
        new Point(control.Location.X + control.Width/2 - DRAG_HANDLE_SIZE/2,
            control.Location.Y + control.Height),
        new Size(DRAG_HANDLE_SIZE, DRAG_HANDLE_SIZE));
    Rectangle SE = new Rectangle(
        new Point(control.Location.X + control.Width,
            control.Location.Y + control.Height),
        new Size(DRAG_HANDLE_SIZE, DRAG_HANDLE_SIZE));

    Graphics g = panel2.CreateGraphics();
    ControlPaint.DrawBorder(g, Border, Color.Gray, ButtonBorderStyle.Dotted);
    ControlPaint.DrawGrabHandle(g, NW, true, true);
    ControlPaint.DrawGrabHandle(g, N, true, true);
    ControlPaint.DrawGrabHandle(g, NE, true, true);
    ControlPaint.DrawGrabHandle(g, W, true, true);
    ControlPaint.DrawGrabHandle(g, E, true, true);
    ControlPaint.DrawGrabHandle(g, SW, true, true);
    ControlPaint.DrawGrabHandle(g, S, true, true);
    ControlPaint.DrawGrabHandle(g, SE, true, true);
    g.Dispose();
}

And here is TextBox_TextChanged event:

private void txtIDImg_TextChanged(object sender, EventArgs e)
{
    if (SelectedLabel == null) return;
    SelectedLabel.Text = txtIDImg.Text;
}

Upvotes: 0

Views: 77

Answers (1)

ntohl
ntohl

Reputation: 2125

In Your control_MouseDown You are changing the SelectedControl and maybe the propertyGrid1.SelectedObject properties, but in the txtIDImg_TextChanged You set the SelectedLabel.Text. SelectedLabel is not set in the control_MouseDown.

Upvotes: 1

Related Questions