• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

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.
0
thaburner
Asked:
thaburner
  • 3
  • 3
1 Solution
 
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
ShauliCommented:
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
 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now