【发布时间】:2021-06-12 18:10:29
【问题描述】:
我正在尝试开发一种软件,用于为学校等设计和生成批量 ID。在设计部分,用户将能够创建任意数量的文本字段、选择它们、拖动它们和/或轻推它们使用箭头键并编辑它们的属性,例如文本、字体、大小和位置。我想要实现的是一个更简单的 Visual Studio 表单设计器版本。 Here is a screenshot of my UI
到目前为止,我已经创建了一个名为“UserLabel”的用户控件,它继承了 Label 类(我喜欢 Label,因为我可以将背景设置为透明)。单击“添加文本”按钮可以创建一个新的用户标签,并且可以在图片框周围拖动。
public partial class UserLabel : Label
{
private System.Drawing.Point StartPoint;
private bool IsMouseDown = false;
public UserLabel()
{
InitializeComponent();
this.Text = "New Item";
this.AutoSize = true;
this.BackColor = System.Drawing.Color.Transparent;
this.Font = new System.Drawing.Font("Arial", 12);
this.Location = new System.Drawing.Point(5, 5);
this.Size = new System.Drawing.Size(50, 20);
}
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
IsMouseDown = true;
this.BringToFront();
StartPoint = e.Location;
}
}
protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
IsMouseDown = false;
}
protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
{
if (IsMouseDown)
{
this.Left = e.X + this.Left - StartPoint.X;
this.Top = e.Y + this.Top - StartPoint.Y;
}
}
}
但由于标签不可选择,我不知道如何从这里开始。我尝试在选择项目时添加 BorderStyle(见下文),以向用户提供选择了哪个项目的视觉提示。但是,就能够编辑属性而言,这并没有实现任何目标。而且我什至无法移除 BorderSyle 以表示失去焦点。
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
IsMouseDown = true;
this.BringToFront();
StartPoint = e.Location;
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
}
}
谁能指出我如何覆盖 CanFocus 属性(或 Selectable 属性,我不知道)的正确方向或有关如何实现此目的的任何其他建议?我也愿意接受任何其他方法来实现我的目标。
注意:我尝试了一种不同的方法,其中将隐藏一定数量的项目,并且可以由用户 like this 设置为可见。但我不喜欢这样,因为它是重复的,即使 10 项看起来很多,我也不知道用户是否需要更多。
【问题讨论】:
-
表示选中的ul有多种方式。我希望您在班级级别有 List
吗?您还应该有一个变量 currentUserLabel,您可以在单击 UL 时设置该变量。诀窍是创造一种干净的方式来做到这一点。 -
@TaW 我在 youtube 上发现了一个视频,它与我最初计划的一样。 C# - Selectable Controls
标签: c# .net label customization