【发布时间】:2016-05-19 07:34:43
【问题描述】:
我有 2 个标签;我想捕捉标签来改变这个标签的文本。
你可以很容易地看到这个 gif。
这个 gif 图像创建了 2 个标签,但我只更改了第一个标签的文本。无法更改第二个标签的文本。
我认为问题是 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;
}
【问题讨论】:
-
请把 textbox_changed 也分享给我们。
-
@ntohl 我更新了
TextBox_TextChanged事件。