Winforms标签组件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Winforms标签组件相关的知识,希望对你有一定的参考价值。

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

Tags Field

基本上我想要一个与这里使用的字段完全相同的字段。当您输入可用选项列表时,如果选择了一个选项,您可以继续选择另一个选项。除非它在我尚未想到的名字下,否则我在任何搜索中都找不到。

无论如何我能得到我想要的东西吗?

答案

正如我在评论中写的那样,自己做这件事非常简单 (花了我大约15分钟给你写下面的例子)这里是一个代码示例+输出开始 - “标签标签”对象带有一个按钮来处理自己,请阅读我的评论里面。

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

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

输出:enter image description here

以上是关于Winforms标签组件的主要内容,如果未能解决你的问题,请参考以下文章

更改cefsharp winforms中的选择标签选项

vue3中的fragment(片段)组件

可能是一个愚蠢的问题,但是如何设置 c# 按钮的标签? (winforms)

设计时的 WinForms 组件属性

多个控件到单个控件c#winforms上

在 winforms 中使用控件(链接标签、树视图)