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
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