【问题标题】:Winforms Tags ComponentWinforms 标签组件
【发布时间】:2018-07-20 20:52:45
【问题描述】:

标签不是我正在寻找的正确词,但由于缺乏更好的选择,我一直使用它。我想知道是否有一个组件或任何我可以使用的东西会创建一个类似于这个网站的标签字段的输入。

基本上,我想要一个与此处使用的字段完全相同的字段。当您输入可能的选项列表时,这些选项将变为可用,一旦选择了一个,您就可以继续选择另一个。除非它的名称是我还没有想到的,否则我在任何搜索中都找不到。

反正我能得到我想要的吗?

【问题讨论】:

  • 自己做很简单,一种方法是创建一个继承Label的类并编写代码来提供您需要的功能(文本、图像、单击事件处理程序来处理对象等...)。您也可以将自定义“标签”对象添加到FlowLayoutPanel 控件,我的行为将与图片中显示的相同
  • 它只是一个复选框,Appearance = Button,FlatStyle = Flat 和一个表示检查状态的Image。
  • 我认为您可以使用包含左侧列表视图和右侧文本框的面板来执行此类操作

标签: c# winforms components


【解决方案1】:

正如我在评论中所写的那样,自己完成它非常简单
(我只花了大约 15 分钟来为您编写下面的示例)这是一个 代码示例+输出开始——“标签标签”对象用一个按钮来处理自己,请看我的cmets里面。

澄清:此代码需要改进以适应所有可能的情况,但您可以从中学习如何创建自定义控件的基本思想。

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        FlowLayoutPanel1.Padding = New Padding(3, 3, 3, 3)

        Dim g As Graphics = Me.CreateGraphics()
        Dim size As SizeF
        ' check how much width needed for the string "Winforms"...'
        size = g.MeasureString("Winforms", Me.Font)

        Dim tagwinforms As New TagObject("Winforms", size.Width + TagObject.BtnRemoveWidth + 20, FlowLayoutPanel1.Height - 8)
        tagwinforms.Init()
        FlowLayoutPanel1.Controls.Add(tagwinforms)

        ' check how much width needed for the string "C#"...'
        size = g.MeasureString("C#", Me.Font)

        Dim tagcsharp As New TagObject("C#", size.Width + TagObject.BtnRemoveWidth + 20, FlowLayoutPanel1.Height - 8)
        tagcsharp.Init()
        FlowLayoutPanel1.Controls.Add(tagcsharp)

        g.Dispose()
    End Sub

End Class

Public Class TagObject
    Inherits Label
    Public Shared Property BtnRemoveWidth As Int16 = 20
    Public Shared Property BtnRemoveHeight As Int16 = 20
    ' note: you can add get set methods and in the set method you can change value in runtime '
    Public Property DescriptionText As String
    Private Property TagHeight As Int16
    Private Property TagWidth As Int16
    Private btnRemove As PictureBox

    ' you can add any property you need backcolor forecolor etc...'
    Sub New(ByVal descriptionText As String, ByVal width As Int16, ByVal height As Integer)
        Me.DescriptionText = descriptionText
        Me.TagHeight = height
        Me.TagWidth = width
        Me.Font = New Font("ARIAL", 8, FontStyle.Bold)
    End Sub

    Public Sub Init()
        Me.Text = DescriptionText
        Me.Width = TagWidth
        Me.Height = TagHeight
        Me.TextAlign = ContentAlignment.MiddleCenter
        Me.BackColor = Color.FromArgb(30, 30, 30)
        Me.ForeColor = Color.White
        btnRemove = New PictureBox()
        btnRemove.Height = BtnRemoveHeight
        btnRemove.Width = BtnRemoveWidth
        btnRemove.Location = New Point(TagWidth - btnRemove.Width - 1, TagHeight / 2 - btnRemove.Height / 2)
        ' original image url: https://www.google.co.il/search?q=close+icon+free&safe=off&rlz=1C1ASUM_enIL700IL700&source=lnms&tbm=isch&sa=X&ved=0ahUKEwjVuJnbk5vZAhXKesAKHRXqDX8Q_AUICigB&biw=1440&bih=769#imgrc=2p_iHiqieStqCM:'
        btnRemove.Image = My.Resources.CloseIcon
        btnRemove.Cursor = Cursors.Hand
        AddHandler btnRemove.Click, AddressOf btnRemove_Click
        Me.Controls.Add(btnRemove)
    End Sub

    Private Sub btnRemove_Click(sender As Object, e As EventArgs)
        ' the user wants to delete this tag...'
        Me.Dispose()
    End Sub
End Class

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 2022-01-14
    • 2012-06-14
    相关资源
    最近更新 更多