在Flowlayoutpanel中找到USERCONTROL

发布于 2025-02-05 18:49:52 字数 6834 浏览 1 评论 0原文

下午好!

该表单具有带有用户列表的Flowlayoutpanel(在USERCONTROL中创建) 使用从JSON文件中获取的数据添加到Flowlayoutpanel中。

如何通过隐藏元素来通过文本字段搜索用户名标签。 例如,我输入“ Ben”,Flowlayoutpanel中与请求不匹配的所有元素都隐藏了。只有用户名中的“ ben”的人保留

usercontrol代码:

Imports System.ComponentModel

Partial Public Class CustomListItem
    Inherits UserControl

    Private _UserName As String
    Private _ID As Integer
    Private _Login As String
    Private _Title As String
    Private _Dep As String
    Private _Mail As String
    Private _Phone As String
    Private _Picture As Image

    <Category("Custom Props")>
    Public Property UserName() As String
        Get
            Return _UserName
        End Get
        Set(ByVal value As String)
            _UserName = value
            Lbl_UserName.Text = value
        End Set
    End Property

    <Category("Custom Props")>
    Public Property ID() As Integer
        Get
            Return _ID
        End Get
        Set(ByVal value As Integer)
            _ID = value
            Lbl_ID.Text = value
        End Set
    End Property

    <Category("Custom Props")>
    Public Property Login() As String
        Get
            Return _Login
        End Get
        Set(ByVal value As String)
            _Login = value
            Lbl_Login.Text = value
        End Set
    End Property

    <Category("Custom Props")>
    Public Property Title() As String
        Get
            Return _Title
        End Get
        Set(ByVal value As String)
            _Title = value
            Lbl_Title.Text = value
        End Set
    End Property

    <Category("Custom Props")>
    Public Property Dep() As String
        Get
            Return _Dep
        End Get
        Set(ByVal value As String)
            _Dep = value
            Lbl_Dep.Text = value
        End Set
    End Property

    <Category("Custom Props")>
    Public Property Phone() As String
        Get
            Return _Phone
        End Get
        Set(ByVal value As String)
            _Phone = value
            Lbl_Phone.Text = value
        End Set
    End Property


    <Category("Custom Props")>
    Public Property Mail() As String
        Get
            Return _Mail
        End Get
        Set(ByVal value As String)
            _Mail = value
            Lbl_Mail.Text = value
        End Set
    End Property

    <Category("Custom Props")>
    Public Property Picture() As Image
        Get
            Return _Picture
        End Get
        Set(ByVal value As Image)
            _Picture = value
            pic_img.Image = value
        End Set
    End Property

    Private Sub CustomListItem_MouseLeave(sender As Object, e As EventArgs) Handles Me.MouseLeave
        BackColor = Color.White
    End Sub

    Private Sub CustomListItem_MouseEnter(sender As Object, e As EventArgs) Handles Me.MouseEnter
        BackColor = Color.Silver
    End Sub
End Class

”在此处输入图像说明“

表单代码:

Imports System.IO
Imports Newtonsoft.Json

Public Class Form1
    Public listUser As List(Of UserList)
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load


        listUser = New List(Of UserList)()
        Dim json = File.ReadAllText("D:\Users.json")

        listUser = JsonConvert.DeserializeObject(Of List(Of UserList))(json)
        Dim listItems = New CustomListItem(29) {}
        FlowLayoutPanel1.Controls.Clear()
        Dim i As Integer = 0
      
        For Each UserList In listUser.Take(30)
            listItems(i) = New CustomListItem With {
                .ID = UserList.Id,
                .UserName = UserList.UserName,
                .Login = UserList.Login,
                .Title = UserList.Title,
                .Dep = UserList.Dep,
                .Mail = UserList.Mail,
                .Phone = UserList.Phone,
                .Picture = My.Resources.User128
            }

            FlowLayoutPanel1.Controls.Add(listItems(i))
            i += 1
        Next UserList

    End Sub
End Class
Public Class UserList
    Public Property Id() As Integer
    Public Property UserName() As String
    Public Property Login() As String
    Public Property Title() As String
    Public Property Dep() As String
    Public Property Mail() As String
    Public Property Phone() As String
End Class

“在此处输入图像说明”

另一项请求,以阐明这些行的含义(特别是其中的数字)

 Dim listItems = New CustomListItem(29) {}
...
 For Each UserList In listUser.Take(30)

据我了解,这是Flowlayoutpanel中创建的USERCONTROL的数量。但是我在JSON文件中的条目数可能不同,我不知道。每次JSON文件中的条目都无法计数。告诉我,如何摆脱Flowlayoutpanel中创建的特定数量的USERCONTROL数量?

预先感谢您的回答和评论。祝你有美好的一天!

当然,JSON文件本身:

[
{
"id":"1",
"UserName":"Fred Smith",
"Login":"f.smith",
"Title":"engineer",
"Dep":"IT infrastcomcture",
"Mail":"[email protected]",
"Phone":"111",
},
{
"id":"2",
"UserName":"Ben Taylor",
"Login":"b.taylor",
"Title":"programmer",
"Dep":"IT infrastcomcture",
"Mail":"[email protected]",
"Phone":"100",
},
{
"id":"3",
"UserName":"Steve Harris",
"Login":"s.harris",
"Title":"System Administrator",
"Dep":"IT infrastcomcture",
"Mail":"[email protected]",
"Phone":"263",
},
{
"id":"4",
"UserName":"Tom Walker",
"Login":"t.walker",
"Title":"engineer",
"Dep":"IT infrastcomcture",
"Mail":"[email protected]",
"Phone":"263",
},
{
"id":"5",
"UserName":"Tom Davis",
"Login":"t.davis",
"Title":"engineer",
"Dep":"IT infrastcomcture",
"Mail":"[email protected]",
"Phone":"200",
},
{
"id":"6",
"UserName":"Ben Walker",
"Login":"b.walker",
"Title":"System Administrator",
"Dep":"IT infrastcomcture",
"Mail":"[email protected]",
"Phone":"167",
},
]

Good afternoon!

The form has a FlowLayoutPanel with a list of users (created in UserControl)
A UserControl is added to the FlowLayoutPanel with data taken from the json file.

How can I search for the UserName tag through a text field by hiding the elements.
For example, I enter "Ben", all elements in the FlowLayoutPanel that do not match the request are hidden. Only those with "Ben" in the UserName remain

UserControl Code:

Imports System.ComponentModel

Partial Public Class CustomListItem
    Inherits UserControl

    Private _UserName As String
    Private _ID As Integer
    Private _Login As String
    Private _Title As String
    Private _Dep As String
    Private _Mail As String
    Private _Phone As String
    Private _Picture As Image

    <Category("Custom Props")>
    Public Property UserName() As String
        Get
            Return _UserName
        End Get
        Set(ByVal value As String)
            _UserName = value
            Lbl_UserName.Text = value
        End Set
    End Property

    <Category("Custom Props")>
    Public Property ID() As Integer
        Get
            Return _ID
        End Get
        Set(ByVal value As Integer)
            _ID = value
            Lbl_ID.Text = value
        End Set
    End Property

    <Category("Custom Props")>
    Public Property Login() As String
        Get
            Return _Login
        End Get
        Set(ByVal value As String)
            _Login = value
            Lbl_Login.Text = value
        End Set
    End Property

    <Category("Custom Props")>
    Public Property Title() As String
        Get
            Return _Title
        End Get
        Set(ByVal value As String)
            _Title = value
            Lbl_Title.Text = value
        End Set
    End Property

    <Category("Custom Props")>
    Public Property Dep() As String
        Get
            Return _Dep
        End Get
        Set(ByVal value As String)
            _Dep = value
            Lbl_Dep.Text = value
        End Set
    End Property

    <Category("Custom Props")>
    Public Property Phone() As String
        Get
            Return _Phone
        End Get
        Set(ByVal value As String)
            _Phone = value
            Lbl_Phone.Text = value
        End Set
    End Property


    <Category("Custom Props")>
    Public Property Mail() As String
        Get
            Return _Mail
        End Get
        Set(ByVal value As String)
            _Mail = value
            Lbl_Mail.Text = value
        End Set
    End Property

    <Category("Custom Props")>
    Public Property Picture() As Image
        Get
            Return _Picture
        End Get
        Set(ByVal value As Image)
            _Picture = value
            pic_img.Image = value
        End Set
    End Property

    Private Sub CustomListItem_MouseLeave(sender As Object, e As EventArgs) Handles Me.MouseLeave
        BackColor = Color.White
    End Sub

    Private Sub CustomListItem_MouseEnter(sender As Object, e As EventArgs) Handles Me.MouseEnter
        BackColor = Color.Silver
    End Sub
End Class

enter image description here

Form code:

Imports System.IO
Imports Newtonsoft.Json

Public Class Form1
    Public listUser As List(Of UserList)
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load


        listUser = New List(Of UserList)()
        Dim json = File.ReadAllText("D:\Users.json")

        listUser = JsonConvert.DeserializeObject(Of List(Of UserList))(json)
        Dim listItems = New CustomListItem(29) {}
        FlowLayoutPanel1.Controls.Clear()
        Dim i As Integer = 0
      
        For Each UserList In listUser.Take(30)
            listItems(i) = New CustomListItem With {
                .ID = UserList.Id,
                .UserName = UserList.UserName,
                .Login = UserList.Login,
                .Title = UserList.Title,
                .Dep = UserList.Dep,
                .Mail = UserList.Mail,
                .Phone = UserList.Phone,
                .Picture = My.Resources.User128
            }

            FlowLayoutPanel1.Controls.Add(listItems(i))
            i += 1
        Next UserList

    End Sub
End Class
Public Class UserList
    Public Property Id() As Integer
    Public Property UserName() As String
    Public Property Login() As String
    Public Property Title() As String
    Public Property Dep() As String
    Public Property Mail() As String
    Public Property Phone() As String
End Class

enter image description here

Another request to clarify the moment what these lines mean (and specifically the numbers in them)

 Dim listItems = New CustomListItem(29) {}
...
 For Each UserList In listUser.Take(30)

As I understand, this is the number of UserControls created in the FlowLayoutPanel. But my number of entries in the json file may be different, not known to me. And it will not be possible to count each time the entries in the json file. Tell me, how can I get away from the specific number of UserControls created in the FlowLayoutPanel?

Thank you in advance for your answers and comments. Have a good day!

And of course the JSON file itself:

[
{
"id":"1",
"UserName":"Fred Smith",
"Login":"f.smith",
"Title":"engineer",
"Dep":"IT infrastcomcture",
"Mail":"[email protected]",
"Phone":"111",
},
{
"id":"2",
"UserName":"Ben Taylor",
"Login":"b.taylor",
"Title":"programmer",
"Dep":"IT infrastcomcture",
"Mail":"[email protected]",
"Phone":"100",
},
{
"id":"3",
"UserName":"Steve Harris",
"Login":"s.harris",
"Title":"System Administrator",
"Dep":"IT infrastcomcture",
"Mail":"[email protected]",
"Phone":"263",
},
{
"id":"4",
"UserName":"Tom Walker",
"Login":"t.walker",
"Title":"engineer",
"Dep":"IT infrastcomcture",
"Mail":"[email protected]",
"Phone":"263",
},
{
"id":"5",
"UserName":"Tom Davis",
"Login":"t.davis",
"Title":"engineer",
"Dep":"IT infrastcomcture",
"Mail":"[email protected]",
"Phone":"200",
},
{
"id":"6",
"UserName":"Ben Walker",
"Login":"b.walker",
"Title":"System Administrator",
"Dep":"IT infrastcomcture",
"Mail":"[email protected]",
"Phone":"167",
},
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感性不性感 2025-02-12 18:49:52

假设您的flowlayoutpanel中的每个控件是相同的类型:

For Each cli As CustomListItem In FlowLayoutPanel1.Controls
    cli.Visible = (cli.UserName = filterValue)
Next

当且仅当其用户名属性等于指定值时,每个用户控件才能看到。

Assuming that every control in your FlowLayoutPanel is the same type:

For Each cli As CustomListItem In FlowLayoutPanel1.Controls
    cli.Visible = (cli.UserName = filterValue)
Next

Each user control will be visible if and only if its UserName property is equal to the specified value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文