-
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
-
you can create a control array and use
load controlname(index)
to load a new control
-
could u pl give me the code
-
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'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
-
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
-
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!!
-
HA! can you get shorter than this?
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
-
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.
-
Well, both you and i have to is that way anyway, since we have vb5, so i'm not arguing.
-
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'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