2011-06-14 50 views
11

是否有人知道c#的Winforms控件類似於stackoverflow使用的標籤控件(見下文)? tag control example標籤控件像stackoverflow的

如果不是,那麼您用來處理標籤的一些很好的選擇是什麼?

+2

這只是一個標籤。如果你想使它看起來完全一樣,那麼你需要一些Padding並通過覆蓋OnPaint()來繪製3D邊框樣式。 – 2011-06-14 23:13:24

+1

確實如此,但我也想要輸入控件,它不僅僅是一個標籤... – Anders 2011-06-15 01:20:33

+1

它是一個純文本框。玩一下ToolBox中的控件,看看有什麼可用的。 – 2011-06-15 01:23:35

回答

6

好了幾分鐘後,我有一個非常簡單的實現。還有很多工作要做,但你可以看到完成你所追求的目標的基本前提。

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; 
    } 
} 

工作原理:

在窗體上放置一個面板,稱之爲TagInputContainer(將舉辦所有的「標籤」)。將面板的光標屬性設置爲IBeam,以便用戶知道他們可以輸入它。當用戶點擊TagInputContainer時,創建一個「標籤」(TextBox),將其DockStyle屬性設置爲Left,以便它們總是向左移動,因此您不必手動處理新的「標籤」位置。

,你能做些什麼來改善它:

  • 措施的字體繩子,讓文本框寬度的增長和與文本收縮。
  • 實施退格功能,如果您將退格按到最後一個標記,則會啓用對標記和退格的編輯,直到您停止。
  • 在TextBox控件畫一個「X」或他們身邊,這樣用戶可以點擊刪除它們
  • 處理好空間欄按鈕,這樣,當用戶按下空格鍵就會創建一個新的標籤。

  • 您可以做的另一件事是當用戶創建一個新標籤時,將以前的標籤設置爲Enabled = false,以便它看起來好像剛剛創建了一個真正的標籤。對於這種效果,我相信如果對文本框沒有默認的系統3D盒效果,但是選擇更平整的外觀(如BorderStyle.FixedSingle或BorderStyle.None),效果會更好。

+0

我不知道你可以初始化一個這樣的控件(就像在TagInputContainer_Click中)。那個語法會被調用什麼,我可以用我自己的類來做到這一點嗎? – Jimmy 2014-07-01 20:00:19

16

不久前,我遇到了你的問題尋找同樣的事情。我能找到的最接近的是CodeProject關於標籤雲的文章,所以最終我放棄了尋找某種可以立即使用的東西,並且自己做了一個。我已經制作了一個Nuget包,源代碼可以在GitHub上免費獲得。

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

二進制(的NuGet):https://www.nuget.org/packages/FerretLib.WinForms

PS:我不認爲這應該被認爲是 '垃圾郵件',因爲它是專門用於解決與此問題相同的需求。