question

Fiya-7319 avatar image
0 Votes"
Fiya-7319 asked TroyJavier-7998 commented

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

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.

dotnet-visual-basic
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Do you want to display, in textbox 1, the checkbox item 1, if selected, or the first selected checkbox item (any index)?

0 Votes 0 ·

1 Answer

karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered TroyJavier-7998 commented

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






11111111111.png (4.9 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Can you convert this code to apply in combobox instead of checklistbox

0 Votes 0 ·