【问题标题】:C# User generated Label, that can be selected and its properties changedC# 用户生成的标签,可以选择并更改其属性
【发布时间】: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


【解决方案1】:

您不必隐藏/显示任何静态行数,这是一个糟糕的设计选择。您只需要使用 DataGridView 和 Button 将行添加到 DataGridView。这样,用户可以在您的文本框中键入他们想要的任何内容,并从行的组合框中选择他们想要选择的任何内容,还可以使用提交按钮将代码推送到您想要的任何地方在 ui 上进行 db/render 。示例代码可能如下所示-

        private void button1_Click(object sender, EventArgs e)
    {
        DataGridViewColumn dataGridViewColumn = new DataGridViewColumn(new DataGridViewTextBoxCell()) ;
        this.dataGridView1.Columns.Add(dataGridViewColumn);
        DataGridViewColumn dataGridViewColumn2 = new DataGridViewColumn(new DataGridViewComboBoxCell());
        this.dataGridView1.Columns.Add(dataGridViewColumn2);
        
        DataGridViewRow dataGridViewRow = new DataGridViewRow();
        dataGridViewRow.Cells.Add(new DataGridViewTextBoxCell());
        this.dataGridView1.Rows.Add(new DataGridViewRow());
    }

【讨论】:

  • 谢谢,我没有考虑用dgv来实现。我会稍等一下,看看有没有可以满足我原来的ui设计要求的答案。我喜欢那个设计,因为我觉得它看起来更干净。如果我没有得到任何答案,将其标记为答案。
猜你喜欢
  • 2012-04-13
  • 2021-03-31
  • 2015-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多