在玩了几分钟之后,我有了一个非常简单的实现。在这方面还有很多工作要做,但您可以看到实现您所追求的一种方法背后的基本前提。
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(它将包含所有“标签”)。将 Panel 的 Cursor 属性设置为IBeam,以便用户知道他们可以输入它。当用户在 TagInputContainer 中单击时,创建一个“标签”(TextBox),将其 DockStyle 属性设置为 Left,以便它们始终向左移动,这样您就不必为每个新的“标签”手动处理 Location。
你可以做些什么来改进它: