VB.NET CheckedListBox Tag?

Is there a tag for an element in a CheckedListBox? Or something similar? I would like to be able to store the identifier associated with the element that I am showing.

+4
source share
3 answers

You can inherit your own control from CheckedListBox and create a property, in C # it will be like that, the rest of the functions will remain the same as they are inherited, therefore additional additional code is not required:

  public class MyCheckedListbox: System.Windows.Forms.CheckedListBox {
     private object thisObj;
     public object Tag {
        get {return this.thisObj;  }
        set {this.thisObj = value;  }
     }
 }

Edit: Decided to include a version of VB.NET for all who benefit ...

  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

Of course, this is understandable and uses boxing, but it works great ...

Hope this helps

+2
source

You do not need the Tag property. The control accepts any object, which means that you do not need to insert only rows in it. Create a class with a string (and overridden ToString() ) and any other data members that you need.

 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 

(The designated ToString() determines what will be displayed in the field.)

+7
source

Translation of tommieb75 on 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 

I use the translator at www.developerfusion.com/tools

0
source

Source: https://habr.com/ru/post/1300351/


All Articles