ir3ax
ir3ax

Reputation: 1

VB.NET Deleting Text in Textbox Using Checkbox

"can anyone help me in my problem

So i have this checkboxes, so what It does is if a select a checkbox the text of that checkbox will be passed to the HistoryTextbox

My main problem is that I am able to add new checkboxes to the HistoryTextbox, but when i uncheck the checkbox i want the text of the checkbox to be deleted in the HistoryTextbox

Example: When i click chkbox1 value of chkbox1 is ASTHMA the HistoryTextbox should contain ASTHMA Then i click again a new checkbox for hypertension the HistoryTextbox should contain ASTHMA,HYPERTENSION But When i uncheck the checkbox of ASTHMA the asthma will be the only one to be deleted or remove and the HistoryTextbox will only contain the hypertension.".

Upvotes: 0

Views: 306

Answers (1)

41686d6564
41686d6564

Reputation: 19661

It's a little difficult to give you guidance without seeing your code but you seem to be looking for something like this:

Private Sub CheckBoxes_CheckedChanged(sender As Object,
                                      e As EventArgs) Handles CheckBox1.CheckedChanged,
                                                              CheckBox2.CheckedChanged,
                                                              CheckBox3.CheckedChanged ', etc.
    Dim currentCheckBox = DirectCast(sender, CheckBox)
    Dim textToAddOrRemove As String = currentCheckBox.Text & ","
    If currentCheckBox.Checked Then
        ' Add the text.
        HistoryTextbox.Text &= textToAddOrRemove
    Else
        ' Remove the text.
        HistoryTextbox.Text = HistoryTextbox.Text.Replace(textToAddOrRemove, String.Empty)
    End If
End Sub

Another option (especially useful if some of the CheckBoxes have the same text) is to generate the text each time based on the checked CheckBoxes. For example:

Private Sub CheckBoxes_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged,
                                                                                CheckBox2.CheckedChanged,
                                                                                CheckBox3.CheckedChanged ', etc.
    Dim checkedCheckBoxes = Me.Controls.OfType(Of CheckBox).Where(Function(c) c.Checked)
    ' Or:
    'Dim checkedCheckBoxes = {CheckBox1, CheckBox2, CheckBox3}.Where(Function(c) c.Checked)

    HistoryTextbox.Text = String.Join(",", checkedCheckBoxes.Select(Function(c) c.Text))
End Sub

Upvotes: 1

Related Questions