Results 1 to 12 of 12

Thread: Adding controls at runtime on the basis of the table recs

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Location
    Bombay
    Posts
    8

    Question

    hello,
    can anybody please help me out with this
    I want to create control at runtime when the form is been loaded .the control should be a check box and the number of controls generated should be depending up on the number of records in the database . And the caption property should also be taken from the table, field name (desc).
    Can this thing be posible Please help me out with this


  2. #2
    Lively Member
    Join Date
    May 1999
    Location
    Singapore
    Posts
    116
    you can create a control array and use
    load controlname(index)
    to load a new control
    YC Sim
    Teenage Programmer
    UIN 37903254



  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Location
    Bombay
    Posts
    8
    could u pl give me the code

  4. #4
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ....

    With VB6 you can directly 'Add' controls to a form at runtime. The code for your problem would look like this:

    Code:
    Private Sub LoadControls()
    
    Dim WithEvents newCheck As CheckBox
    Dim rs1 As RecordSet
    Dim I As Integer
    
    Set rs1 = db1.OpenRecordSet("Select * from <table name>")
    for I = 0 to rs1.Fields.Count -1 Step 1
    
       Set newCheck = Me.Controls.Add("VB.CheckBox","Check" & CStr(I))
       newCheck.Caption = rs1.Fields(I).Name
       newCheck.Value = rs1.Fields(I).Value
       newCheck.Visible = True
    
    Next
    
    End Sub
    The only hitch in the above code is I have not provided for a proper display of all checkboxes. Add the code which displays each checkbox at a different location.

    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  5. #5
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    I've seen the post similar to this one some time ago. In the answer below the user decided to use a listview to display the names in checkboxes...perhaps it might help or at least you can trace this guys questions and perhaps find your answer in his questions.

    http://forums.vb-world.net/showthrea...threadid=21669
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  6. #6
    Guest
    Try:

    Code:
    Private Sub Form_Load()
    
        Controls.Add "VB.CheckBox", "Check1"
        Me!Check1.Caption = "CheckBox"
        Me!Check1.Move 0, 0
        Me!Check1.Visible = True
    
    End Sub

  7. #7
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Dynamic Guru!

    Well, Megatron, searching for dynamic addition of controls? Is it pure coincidence that both your posts have corrected my code?

    Well, I must say that that's why a Guru is different from a Lively Member, my own very code, but so short and sweet!

    Thanks a lot Guru!!
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    HA! can you get shorter than this?
    Code:
    Load a(1)
    Nope, but you could probably go for ycsim's sample, a bit more dynamically with
    Code:
    load controlname(controlname.count)
    And it works in VB5 too
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9
    Guest
    honeybee: No problem.

    Kedaman: Some might argue about that because you are actually loading it from a Control Array, hence, you would need at least 1 instance of it on the Form.

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Well, both you and i have to is that way anyway, since we have vb5, so i'm not arguing.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  11. #11
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    On second thoughts....

    Just after posting the above reply, Megatron, I was thinking about your solution, and I think it is very close to useless.

    Your code does not allow me to add a number of controls using a loop, whereas mine does.

    Please allow me to withdraw my earlier comment.

    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  12. #12
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    I'm goanna have to defend megatron here in that your solution was correct and didn't need adding to, wheeas Megs, although not that useful in the context of the question is very useful for other things, I didn't know you could do It like that, and now I do. If he'd either not posted, or posted the same reply as you, nobody would have learned anything

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width