CheckBox & RTF Help

My VB App reads a txt document and inserts the text into a richtxt box. Im trying to add 4 check boxes so if the user wants to exclude some data example values(1) and values(2) is their a short way of righting the code other than the example at the bottom. So if i say they uncheck Box 1 & 2 then it would remove the commas between each variable. That the user does not want to show but I need a comma between each one that does show.

                 If CK1 = True Then
                 CK1A = values(0)
                 End If
                 
                 If CK2 = True Then
                 CK2A = values(1)
                 End If
                 
                 If CK3 = True Then
                 CK3A = values(2)
                 End If
                 
                 If CK4 = True Then
                 CK4A = values(3)
                 End If
                 
                 FinalCK = CK1A & "," & CK2A & "," & CK3A & "," & CK4A
                 RTBReport.Text = RTBReport.Text & FinalCK & vbCrLf

Example when I run app with only the first CK1 checked it will show like this in the RTF

 0,,,
 249.65,,,
 499.3,,,

Hope I explained this good enough.
thaburnerAsked:
Who is Participating?
 
ShauliConnect With a Mentor Commented:
Option Explicit
Private FinalCK As String, Values(0 To 3) As String   '<<< modify data types if needed

Private Sub Command1_Click()
Dim cLoop As Integer, CKA(0 To 3) As String
Values(0) = Text1.Text
Values(1) = Text2.Text
Values(2) = Text3.Text
Values(3) = Text4.Text
For cLoop = 0 To CK.Count - 1
    If CK(cLoop).Value = vbChecked Then CKA(cLoop) = Values(cLoop)
Next cLoop
FinalCK = Join(CKA, ",")
FinalCK = Replace(FinalCK, ",,", ",")
If Right(FinalCK, 1) = "," Then FinalCK = Left(FinalCK, Len(FinalCK) - 1)
MsgBox FinalCK
End Sub

S
0
 
ShauliCommented:
Use arrays: make all checkboxes in array (using the same name, and with the index property set from 0 to 3). Use array variables, as in:

Option Explicit
Private CKA(0 To 3) As String, CKA3 As Double, ckIndex As Integer   '<<< modify data types if needed
Private FinalCK As String, Values(0 To 3) As String   '<<< modify data types if needed

Private Sub CK_Click(Index As Integer)
ckIndex = Index
End Sub

Private Sub Command1_Click()
If CK(ckIndex).Value = True Then CKA(ckIndex) = Values(ckIndex)
FinalCK = Join(CKA, ",")
MsgBox FinalCK
End Sub

S
0
 
thaburnerAuthor Commented:
Im having problems getting this to work i created a new project to test this all out and i am getting this in the msgbox  ,,,

Private Sub Command1_Click()
Values(0) = Text1.Text
Values(1) = Text2.Text
Values(2) = Text3.Text
If CK(ckIndex).Value = vbChecked Then CKA(ckIndex) = Values(ckIndex)
FinalCK = Join(CKA, ",")
MsgBox FinalCK
End Sub
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
ShauliCommented:
Sorry, m mistake, try this one (using arrays as above):

Option Explicit
Private FinalCK As String, Values(0 To 3) As String   '<<< modify data types if needed

Private Sub Command1_Click()
Dim cLoop As Integer, CKA(0 To 3) As String
Values(0) = Text1.Text
Values(1) = Text2.Text
Values(2) = Text3.Text
Values(3) = Text4.Text
For cLoop = 0 To CK.Count - 1
    If CK(cLoop).Value = vbChecked Then CKA(cLoop) = Values(cLoop)
Next cLoop
FinalCK = Join(CKA, ",")
MsgBox FinalCK
End Sub

S

0
 
thaburnerAuthor Commented:
Ok if i click on check3 and check4 i still get commas from check 1 and 2 if they were clicked?

For Example if i want all four checked then this will show
Hello,This,Is,Test

If i click Check 3 and 4 i want to see

Is,Test

not

,,Is,Test

I appreciate your help, I see what your doing but i still cant think of a way to write it lol
0
 
thaburnerAuthor Commented:
Thank you so much im glad you could figure it out I will up the points a little more for you generosity
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.