首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Winforms Tags组件

Winforms Tags组件
EN

Stack Overflow用户
提问于 2018-02-10 10:54:23
回答 1查看 289关注 0票数 1

标签不是我想要的东西,但由于缺乏更好的选择,我使用了标签。我想知道的是,如果有一个组件或任何我可以使用,将创建一个类似于这个网站的标签字段的输入。

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

有没有什么办法可以让我得到我想要的东西?

EN

回答 1

Stack Overflow用户

发布于 2018-02-10 19:02:37

正如我在我的评论中所写的那样,自己做这件事非常简单

(我只花了大约15分钟写了下面的例子)这里是一个代码example+output的开始-“标签标签”对象与一个按钮来处理自己,请阅读我的评论里面。

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

代码语言:javascript
运行
复制
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

输出:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48716776

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档