Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.NET CheckedListBox标记?_Vb.net_Checkedlistbox - Fatal编程技术网

VB.NET CheckedListBox标记?

VB.NET CheckedListBox标记?,vb.net,checkedlistbox,Vb.net,Checkedlistbox,CheckedListBox中是否有项目的标记?还是类似的?我希望能够存储与我正在显示的项目相关联的控件和ID。您可以从CheckedListBox继承您自己的控件并创建属性,在C#中是这样的,其余功能与继承的功能相同,因此不需要进一步的代码: public class MyCheckedListbox : System.Windows.Forms.CheckedListBox{ private object thisObj; public object Tag{

CheckedListBox中是否有项目的标记?还是类似的?我希望能够存储与我正在显示的项目相关联的控件和ID。

您可以从CheckedListBox继承您自己的控件并创建属性,在C#中是这样的,其余功能与继承的功能相同,因此不需要进一步的代码:

public class MyCheckedListbox : System.Windows.Forms.CheckedListBox{ private object thisObj; public object Tag{ get{ return this.thisObj; } set{ this.thisObj = value; } } } 公共类MyCheckedListbox:System.Windows.Forms.CheckedListBox{ 私人客体; 公共对象标签{ 获取{返回this.thisObj;} 设置{this.thisObj=value;} } } 编辑:为了每个人的利益,决定加入VB.NET版本

Public Class MyCheckedListBox Inherits System.Windows.Forms.CheckedListBox Private thisObj As Object Public Property Tag As Object Get Tag = thisObj End Get Set (objParam As Object) thisObj = objParam End Set End Property End Class 公共类MyCheckedListBox继承System.Windows.Forms.CheckedListBox 私有thisObj作为对象 作为对象的公共属性标记 得到 Tag=thisObj 结束 集合(对象为objParam) thisObj=objParam 端集 端属性 末级 当然,这很简单,使用拳击,但效果很好


希望这有助于将tommieb75的答案翻译成VB.NET:

Public Class MyCheckedListbox 
    Inherits System.Windows.Forms.CheckedListBox 
    Private thisObj As Object 
    Public Property Tag() As Object 
        Get 
            Return Me.thisObj 
        End Get 
        Set(ByVal value As Object) 
            Me.thisObj = value 
        End Set 
    End Property 
End Class

我在www.developerfusion.com/tools上使用转换器,您不需要标记属性。控件接受任何对象,这意味着您不必只在其中放入字符串。创建一个包含字符串(并覆盖
ToString()
)和所需的任何其他数据成员的类

Public Class Form1

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        MyBase.OnLoad(e)

        CheckedListBox1.Items.Add(New MyListBoxItem() With {.Name = "One", .ExtraData = "extra 1"})
        CheckedListBox1.Items.Add(New MyListBoxItem() With {.Name = "Two", .ExtraData = "extra 2"})
    End Sub

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        For Each obj As Object In CheckedListBox1.CheckedItems
            Dim item As MyListBoxItem = CType(obj, MyListBoxItem)
            MessageBox.Show(String.Format("{0}/{1} is checked.", item.Name, item.ExtraData))
        Next
    End Sub
End Class

Public Class MyListBoxItem
    Private _name As String
    Private _extraData As String

    Public Property Name As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Public Property ExtraData As String
        Get
            Return _extraData
        End Get
        Set(ByVal value As String)
            _extraData = value
        End Set
    End Property

    Public Overrides Function ToString() As String
        Return Name
    End Function

End Class

(被重写的
ToString()
指示框中显示的内容。)

这是用于Windows窗体、WPF、Silverlight还是ASP.NET Web窗体?(这几天有这么多的可能性,这几乎很有趣,但我不得不问。)@Bochur1:好的,我已经编辑了我的答案…希望没有错误…:)