Check boxes Generation

Posted on 2005-03-08
Medium Priority
Last Modified: 2010-04-17
This is a VB6 Question
I have labels on a form that are being generated dinamically, I need to be able to generate 10 check boxes dinamicaly for each label generated .
is this possible????

Thank you
Question by:NEW_INDIA
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 13486372

The code would look basically the same as the the sample I gave you that deals with only one checkbox.  Create a new project and add a Label.  To the right of the Label, add then CheckBoxes, Check1 thru Check10.  Now for the Label and all the CheckBoxes, set the Index property to 0 (zero):

Private Sub Command1_Click()
    Dim idx As Integer
    idx = Label1.Count
    Load Label1(idx)
    Label1(idx).Left = Label1(0).Left
    Label1(idx).Top = Label1(idx - 1).Top + Label1(idx - 1).Height + 15
    Label1(idx).Visible = True
    Load Check1(idx)
    Check1(idx).Left = Check1(0).Left
    Check1(idx).Top = Check1(idx - 1).Top + Check1(idx - 1).Height + 15
    Check1(idx).Visible = True

    Load Check2(idx)
    Check2(idx).Left = Check2(0).Left
    Check2(idx).Top = Check2(idx - 1).Top + Check2(idx - 1).Height + 15
    Check2(idx).Visible = True

    Load Check3(idx)
    Check3(idx).Left = Check3(0).Left
    Check3(idx).Top = Check3(idx - 1).Top + Check3(idx - 1).Height + 15
    Check3(idx).Visible = True

    ' Code repeats for Check4 thru Check9
    ' ...
    ' ...
    ' ...

    Load Check10(idx)
    Check10(idx).Left = Check10(0).Left
    Check10(idx).Top = Check10(idx - 1).Top + Check10(idx - 1).Height + 15
    Check10(idx).Visible = True
End Sub
LVL 23

Expert Comment

ID: 13488277
Dim fso As New FileSystemObject
    Dim f As File
    Dim fsoStream As TextStream
    Dim Lines() As String
    Dim intPrev As Integer
    Dim LineCount As Integer
    Dim Lastchk As Integer
    Set f = fso.GetFile("c:\users.txt")
    Set fsoStream = f.OpenAsTextStream(ForReading)
    Lines() = Split(Trim(fsoStream.ReadAll), vbCrLf)
    'MsgBox ("No. of Lines: " & UBound(Lines))
For i = 1 To UBound(Lines) + 1
intPrev = i - 1
Load chk(i)

'n = chk(i).Move(0, 10, "", "")
chk(i).Top = chk(intPrev).Top + chk(intPrev).Height
chk(i).Caption = "Checkbox " & i & Name
chk(i).Visible = True
'chk(0).Visible = False

Next i

    Set f = fso.GetFile("C:\users.txt")
    Set fsoStream = f.OpenAsTextStream(ForReading)
    ' Read the file line by line, printing the results to the Form
    Do While Not fsoStream.AtEndOfStream
       'ReDim strArray(cntLineCount)
       intLine = fsoStream.AtEndOfLine
        'Debug.Print strLine

        LineCount = UBound(Lines)
ReDim strArray(LineCount)
For x = 0 To LineCount
        strArray(x) = fsoStream.ReadLine
        'MsgBox strArray(i)
        'lblUsers(i).Caption = strArray(i)
        chk(x).Caption = "Save : " & strArray(x) & " ? "
        'Lastchk = UBound(x)
        chk(LineCount + 1).Visible = False
Next x

        cntLineCount = fsoStream.Line
       ' MsgBox cntLineCount
    Set fsoStream = Nothing
    Set f = Nothing
    Set fso = Nothing

I did it like that because when I put the items in the 2nd loop in the first loop it kept crashing out because it said it would pass the end of file. Because I added 1 to the UBound(Lines). I hope this helps :)

FYI - You need a reference to the Microsoft Runtime Scripting which you can do by going to Project --> References and checking the checkbox next to it in that list :)
LVL 23

Expert Comment

ID: 13488307
I forgot to mention that it reads the amount of lines from the text file and then that determines how many controls need to be created with regards to the checkboxes and then it loads each Name from the text file into each of the controls.

In the code, put this line of code in general declarations :

Private strArray() As String   '<-- This is the string array for holding all of the values
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

LVL 23

Expert Comment

ID: 13488314
For this to work you need one checkbox which is called chk and you change the Index property from null to a 0 ( zero ) and then the coding does the rest with regards to creating the control :)

Expert Comment

ID: 13493730
or you can use some sort of CheckedListBox
LVL 23

Expert Comment

ID: 13540845
New India, what is happening with this question, just out of curiousty :) ??

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Simple Linear Regression
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question