0

I have 3 checkboxes and 1 textbox

checkbox1, checkbox2, checkbox3

when i check first checkbox1 and then checkbox3 then in textbox it will appear as 1,3 exactly........ !!

using vb.net only.........

2
  • I don't have the appropriate reputation to officially mark this, but this is pretty close to a duplicate of: stackoverflow.com/questions/4090421/… Commented Nov 3, 2010 at 20:18
  • ^^ Which was asked by the same user, just an hour earlier. Commented Nov 3, 2010 at 20:19

1 Answer 1

2

Try:

Private Sub CheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged, CheckBox2.CheckedChanged, CheckBox1.CheckedChanged

    Dim sb As New System.Text.StringBuilder
    sb.Append(CStr(IIf(CheckBox1.Checked, "1 ", "")))
    sb.Append(CStr(IIf(CheckBox2.Checked, "2 ", "")))
    sb.Append(CStr(IIf(CheckBox3.Checked, "3 ", "")))
    TextBox1.Text = sb.ToString.Trim
End Sub
Sign up to request clarification or add additional context in comments.

4 Comments

i wanna use it with 45 check boxes right now ....i wanna try .....with 3 checkboxes and 1 textbox....... if i check checkbox1 and checkbox2 or checkbox3 and checkbox1 it will instantly shown in textbox1 as 1,2 or 1,3
@user495854 is your problem that my answer is ugly with a large number of checkboxes (fair enough) or that it doesn't work even with just 3 checkboxes (if so, how doesn't it work)?
I do not think this is worth a full answer but here is a fix to hawbsl's so that you do not get the trailing comma: Dim sb As New System.Text.StringBuilder Dim text As String sb.Append(CStr(IIf(CheckBox1.Checked, "1, ", ""))) sb.Append(CStr(IIf(CheckBox2.Checked, "2, ", ""))) sb.Append(CStr(IIf(CheckBox3.Checked, "3, ", ""))) text = sb.ToString If text.EndsWith(", ") = True Then TextBox1.Text = text.Substring(0, text.Length - 2) Else TextBox1.Text = text.Trim End If
I think he wanted it to be just like yours, but with commas separating the numbers and with no trailing comma. I sort of think, maybe, based on his duplicate question that is what he wanted. Your answer is correct based on what he put in THIS question so I gave you +1 =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.