none
how do I change the text of an item in an existing CheckedBoxList? RRS feed

  • Question

  • Suppose the  user of my program sees a checkedboxlist of various items, for instance colors.   He wants to change the fifth line, which has text of  "magenta" to  now display "purple".   He selects that line, clicks an EDIT button, which brings up a textbox that I provide where he can type in "purple.".   My  question is: how do I then revise the item in the checkedboxlist, and refresh the view so that the  user now sees 'purple' in the fifth line?   I don't want to lose any 'checked' values in the process either.

    Thanks

    Saturday, December 8, 2018 11:32 AM

Answers

  • Hello,

    The basics are find the item via FindString, if index is not -1 then set the new value. The Check state is not affected.

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim index = CheckedListBox1.FindString(TextBox1.Text)
            If index <> -1 Then
                CheckedListBox1.Items(index) = TextBox2.Text
            End If
        End Sub
    End Class

    And this is for when you are on the line and want that item text to change.

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If CheckedListBox1.SelectedIndex > -1 Then
                CheckedListBox1.Items(CheckedListBox1.SelectedIndex) = TextBox1.Text
            End If
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites




    Saturday, December 8, 2018 12:03 PM
    Moderator