I want to display the checked items from checkedlistbox into different textboxes on vb

Fiya 1 Reputation point
2021-02-16T09:09:51.397+00:00

Like checklistbox item 1 in textbox 1
And then the checkedlistbox item 2 in textbox 2. I want to do this for four text boxes.
I am new to this language and I am trying to get the result but I am just not getting the code right. Please help me.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,625 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,286 Reputation points
    2021-02-16T12:07:51.33+00:00

    Hello,

    Try the following which sets the CheckedListBox.DataSource to a list. The list has two properties, one to display text and the other a TextBox to associate with. If this does not meet your requirements please provide more details.

    By associated TextBox

    Full source

    See the project in the following GitHub repository.

    Class

    This call serves as a strongly typed item which when an item in the CheckedListBox changed it's checked state the associated TextBox.Text is set to the value of the selected item or the text property is set to an empty string.

    Public Class Item  
        Public Property Text() As String  
        Public Property TextBox() As TextBox  
        Public Overrides Function ToString() As String  
            Return Text  
        End Function  
    End Class  
    

    Form code

    The ItemCheck event has been set by selecting properties for the CheckedListBox, select the event tab, double click on ItemCheck.

    Public Class Form1  
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
      
            CheckedListBox1.CheckOnClick = True  
      
            Dim itemList As New List(Of Item) From {  
                New Item() With {.Text = "One", .TextBox = TextBox1},  
                New Item() With {.Text = "Two", .TextBox = TextBox2},  
                New Item() With {.Text = "Three", .TextBox = TextBox3},  
                New Item() With {.Text = "Four", .TextBox = TextBox4}  
            }  
      
            CheckedListBox1.DataSource = itemList  
      
        End Sub  
      
        Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) _  
            Handles CheckedListBox1.ItemCheck  
      
            Dim currentItem = CType(CheckedListBox1.SelectedItem, Item)  
      
            If e.NewValue = CheckState.Checked Then  
                currentItem.TextBox.Text = currentItem.Text  
            Else  
                currentItem.TextBox.Text = ""  
            End If  
        End Sub  
    End Class  
    

    Form version 2

    Here we set the TextBox.Text for each item when the form loads.

    Public Class Form1  
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
      
            CheckedListBox1.CheckOnClick = True  
      
            Dim itemList As New List(Of Item) From {  
                New Item() With {.Text = "One", .TextBox = TextBox1},  
                New Item() With {.Text = "Two", .TextBox = TextBox2},  
                New Item() With {.Text = "Three", .TextBox = TextBox3},  
                New Item() With {.Text = "Four", .TextBox = TextBox4}  
            }  
      
            CheckedListBox1.DataSource = itemList  
            For Each item As Item In itemList  
                item.TextBox.Text = item.Text  
            Next  
      
      
        End Sub  
      
        Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) _  
            Handles CheckedListBox1.ItemCheck  
      
            Dim currentItem = CType(CheckedListBox1.SelectedItem, Item)  
      
            If e.NewValue = CheckState.Checked Then  
                currentItem.TextBox.Text = currentItem.Text  
            Else  
                currentItem.TextBox.Text = ""  
            End If  
        End Sub  
    End Class  
    

    Screenshot

    68610-11111111111.png

    By associated TextBox name

    In this version the TextBox name is stored rather than the actual TextBox.

    Class

    Public Class Item  
        Public Property Text() As String  
        Public Property TextBoxName() As String  
        Public Overrides Function ToString() As String  
            Return Text  
        End Function  
    End Class  
      
    

    Form code

    Public Class Form1  
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
      
            CheckedListBox1.CheckOnClick = True  
      
            Dim itemList As New List(Of Item) From {  
                New Item() With {.Text = "One", .TextBoxName = "TextBox1"},  
                New Item() With {.Text = "Two", .TextBoxName = "TextBox2"},  
                New Item() With {.Text = "Three", .TextBoxName = "TextBox3"},  
                New Item() With {.Text = "Four", .TextBoxName = "TextBox4"}  
            }  
      
            CheckedListBox1.DataSource = itemList  
      
        End Sub  
      
        Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) _  
            Handles CheckedListBox1.ItemCheck  
      
            Dim currentItem = CType(CheckedListBox1.SelectedItem, Item)  
      
            Dim TargetTextBox As TextBox = CType(Controls(currentItem.TextBoxName), TextBox)  
      
            If e.NewValue = CheckState.Checked Then  
                TargetTextBox.Text = currentItem.Text  
            Else  
                TargetTextBox.Text = ""  
            End If  
        End Sub  
    End Class  
    
    1 person found this answer helpful.