我有2个标签;我想捕获Label以更改此Label的Text。
你可以看到这个gif很容易想象。
此gif图像创建2个Label,但我只更改第一个Label的文本。无法更改第二个标签的文字。
我认为问题是SelectedControl无法检测控件被选中。
我的代码适用于所有活动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);
}
}
当点击按钮添加新标签(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();
}
这是TextBox_TextChanged
事件:
private void txtIDImg_TextChanged(object sender, EventArgs e)
{
if (SelectedLabel == null) return;
SelectedLabel.Text = txtIDImg.Text;
}
答案 0 :(得分:1)
在您的control_MouseDown
中您正在更改SelectedControl
和propertyGrid1.SelectedObject
属性,但在txtIDImg_TextChanged
中您设置了SelectedLabel.Text
。 SelectedLabel
中未设置control_MouseDown
。