标签控制就像stackoverflow一样

时间:2011-06-14 22:56:40

标签: c# winforms visual-studio-2010

是否有人知道c#的Winforms控件类似于stackoverflow使用的Tags控件(见下文)? tag control example

如果没有,你用来处理标签的一些好的替代品是什么?

2 个答案:

答案 0 :(得分:16)

我不久前遇到了同样的问题。我能找到的最接近的是关于标签云的CodeProject文章,所以最终我放弃了找到开箱即用的东西,并自己制作了一个。我已经制作了一个Nuget软件包,源代码可以在GitHub上免费获得。

  

来源(GitHub):https://github.com/nathanchere/FerretLib.WinForms

     

Binary(Nuget):https://www.nuget.org/packages/FerretLib.WinForms

PS:我认为这不应被视为“垃圾邮件”,因为它是专门为满足此问题中提出的需求而编写的。

答案 1 :(得分:7)

在玩了几分钟之后,我有一个非常简单的实现。还有很多工作要做,但你可以看到完成你所追求的一种方式背后的基本前提。

Form1.cs中:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TagInput
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void TagInputContainer_Click(object sender, EventArgs e)
        {
            TextBox box = new TextBox()
            {
                Width = 100,
                Height = 30,
                Font = new Font("Segoe UI Light", 12),
                BorderStyle = BorderStyle.None,
                BackColor = Color.Khaki,
                Location = new Point(0,0),
                Dock = DockStyle.Left,
                Margin = new Padding(2, 0, 0, 0)
            };

            TagInputContainer.Controls.Add(box);
        }
    }
}

Form1.Designer.cs:

namespace TagInput
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.TagInputContainer = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // TagInputContainer
            // 
            this.TagInputContainer.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.TagInputContainer.Location = new System.Drawing.Point(157, 161);
            this.TagInputContainer.Name = "TagInputContainer";
            this.TagInputContainer.Size = new System.Drawing.Size(406, 30);
            this.TagInputContainer.TabIndex = 0;
            this.TagInputContainer.Click += new System.EventHandler(this.TagInputContainer_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(664, 395);
            this.Controls.Add(this.TagInputContainer);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Panel TagInputContainer;
    }
}

工作原理:

在表单上放置一个Panel,称之为TagInputContainer(它将保存所有“标签”)。将Panel的Cursor属性设置为IBeam,以便用户知道可以键入它。当用户单击TagInputContainer时,创建一个“标签”(TextBox),将其DockStyle属性设置为Left,这样它们总是向左移动,因此您不必手动处理位置foreach new“tag”。

你可以做些什么来改善它:

  • 测量字符串,以便TextBox宽度随文本增长和缩小。
  • 实施退格功能,如果您按退格键到最后一个标签,它将启用标签和退格编辑,直到您停止。
  • 在TextBox控件上或旁边绘制“x”,以便用户可以单击以删除它们
  • 处理 space 栏按钮,以便当用户按空格键时,它将创建一个新标签。

  • 你可以做的另一件事是当用户创建一个新标签时,将前一个标签设置为Enabled = false,使其看起来好像刚创建了一个真实标签。对于这种效果,我相信如果你没有文本框上的默认系统3d框效果,但选择更平坦的外观,例如BorderStyle.FixedSingle或BorderStyle.None,它会看起来更好。