|
-
July 20th, 1999, 02:53 PM
#1
Placing controls at runtime?
Instead of dragging and dropping controls at design time, I'd like to load and place controls at runtime. However, I can't seem to find anything that shows
me how to do that. Please help!
-
July 21st, 1999, 01:33 AM
#2
Re: Placing controls at runtime?
I think you cannot place controls at run time. Only thing you can do at run time is to generate multiple instances of one control during runtime. Another way is to create the desired control at design time but have it invisible and when you need it at run time, just set it's visible property to true.
Michael Vlastos
Company MODUS SA
Development Department
Athens, Greece
Tel: +3-01-9414900
-
July 21st, 1999, 01:36 AM
#3
Re: Placing controls at runtime?
you can dynamically load controls at runtime in VB 6 by calling the Add method of the Controls collection, e.g.
me.controls.add "progIdOfControlGoesHere"
and yes, you could use Control Arrays and use the Load statement as Dr_Michael said.
-
July 21st, 1999, 01:56 AM
#4
Re: Placing controls at runtime?
Once you created a control array at design time, even with just one item, it's straight forward to create new items at runtime using the <load> command:
'Suppose you created Text1(0) at design time
Load Text1(1)
'set properties as required
With Text1(1)
.Move ..to a place where it should be.
.MaxLength = ...
.Widt = ...
.etc = ..
.Visible = True
End With
'Don't forget to make it visible! Default is visible = false
You can remove the control(s)
Unload Text1(1).
You can only unload controls that were added at design time
Jan
-
July 21st, 1999, 03:00 AM
#5
Re: Placing controls at runtime?
Hi
SoftCircuits have an excellent example of how to move/create controls at run-time (a little like the IDE).
You can get the file here http://ftp://ftp.softcircuits.com/vbsrc/formdsgn.zip - be warned though, IE5 kept timing out when I just tried it, although FTP access from DOS was much better.
Chris Eastwood
CodeGuru - the website for developers
http://www.codeguru.com/vb
-
July 21st, 1999, 03:03 AM
#6
Re: Placing controls at runtime?
I dont know why Dr_Michael rated this post with -ve.
If you are not using VB 6.0, ( which allows controls to be created at run time ) the only way is to have "new" controls is to have them as elements of a Control Array and Jan gives a good example too!. The only way you can define a control array is by setting its index property to 0 ( you can set to other integers also, though!) at design time - it is readonly at runtime.
ps: I think, if some one rates a post with -ve, he/she should also provide some reason why they thought it was off-topic or wrong!
-
July 21st, 1999, 03:09 AM
#7
Re: Placing controls at runtime?
Of course, I can explain why I rated this such a way. It's not something personal of course (instead I can say I own votes to Jan Businger for rating one of my posts - if we can say that), but I think that although his answer was really a good peace of code, I don't think that answered the question: Anonymous asked how to CREATE controls at run time and NOT how to REGENERATE them. My answer was very short and Lothar's answer was a better one. So i must not forget to rate it... It's better to have explanations and not misapprehensions!
Michael Vlastos
Company MODUS SA
Development Department
Athens, Greece
Tel: +3-01-9414900
-
July 21st, 1999, 03:52 AM
#8
Re: Placing controls at runtime?
Infact, It may actually be possible to Create controls at run-time even in VB 5.0 - using the rough way of Win32 API. All that one has to do is Findout the right ClassName (WIndows class name) and use CreateWindow or CreateWindowEx fns.
It sure is difficult and one could be called "Real Pro" if he can get it working w/o a few initial crashes!:-) It would be going back to dark ages.
-
July 21st, 1999, 04:12 AM
#9
Re: Placing controls at runtime?
OK guys, this is what you expect I suppose
Option Explicit
Private WithEvents txtBx As TextBox
Private Sub Form_Load()
Set txtBx = Form1.Controls.Add("VB.TextBox", "txtBox")
With txtBx
.Visible = True
.Width = 400 'or whatever
.Height = 200 ' or whatever
End With
End Sub
This is a de novo created textbox at runtime.
Jan
-
July 21st, 1999, 04:25 AM
#10
Re: Placing controls at runtime?
Hi Ravi
You can create a control at run-time using CreateWindow(ex) - I've got an example at http://www.codeguru.com/vb/articles/1638.shtml, and the vbAccelerator site (http://www.vbaccelerator.com) has all of it's source-code/controls based around creating controls this way.
Chris Eastwood
CodeGuru - the website for developers
http://www.codeguru.com/vb
-
July 21st, 1999, 05:56 AM
#11
Re: Placing controls at runtime? // My last..experiment with it
Hi,
The controls on the form (Form1) are totally <de novo>, so created all at runtime
You only need form1 and put the code (below) on it.
/////////////////////////////////////
Option Explicit
Private WithEvents tbxNovo1 As TextBox
Private WithEvents tbxNovo2 As TextBox
Private WithEvents cbtNovo1 As CommandButton
Private Sub cbtNovo1_Click()
Dim ctrl As Control
For Each ctrl In Form1.Controls
MsgBox "There is a control named: " & ctrl.Name
Next
End Sub
Private Sub Form_Load()
Set tbxNovo1 = Form1.Controls.Add("VB.TextBox", "txtName")
Set tbxNovo2 = Form1.Controls.Add("VB.TextBox", "txtAddress")
Set cbtNovo1 = Form1.Controls.Add("VB.CommandButton", "cmdTest")
tbxNovo1.Move 100, 100, 1200, 200
tbxNovo1.Visible = True
tbxNovo2.Move 100, tbxNovo1.Top + 400, 1200, 200
tbxNovo2.Visible = True
With cbtNovo1
.Move 1500, 100, 900, 400
.Visible = True
.Caption = "Check It"
End With
Form1.Show
End Sub
Play with it
Jan
-
July 21st, 1999, 03:55 PM
#12
THANK YOU ALL!
Thanks to everyone who responded to my question. I've gotten it to work with controls such as TextBox and PictureBox, but not with controls like RichTextBox and my own controls. Now I just need to figure out how to find the right ids...
Thanks again!
betsey
(aka "Anonymous")
-
July 22nd, 1999, 06:14 AM
#13
Re: Placing controls at runtime? // My last..experiment with it
This code works only in VB 6.0
Controls collections doesnot have a .Add method in 5.0. It was added only for 6.0. Lother already stated that
-
July 22nd, 1999, 06:16 AM
#14
Re: THANK YOU ALL!
Becasuse you have to appropriate Libraries.
The standard windoes controls come in with VB's runtime libraries that are added to any project.
But you want Common controls or Richtextbox you have to load those libraries by code or add their reference in "Referencs" optoin of Project menu.
Same goes with your custom controls also
-
July 22nd, 1999, 06:28 AM
#15
Re: THANK YOU ALL!
not true.
this code loads a richtextbox
Licenses.Add "Richtext.Richtextctrl.1"
Controls.Add "Richtext.Richtextctrl.1", "name"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|