July 20th, 1999, 02:53 PM
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!
me how to do that. Please help!
|
Click to See Complete Forum and Search --> : Placing controls at runtime? July 20th, 1999, 02:53 PM 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! Dr_Michael July 21st, 1999, 01:33 AM 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 Lothar Haensler July 21st, 1999, 01:36 AM 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. Jan Businger July 21st, 1999, 01:56 AM 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 Chris Eastwood July 21st, 1999, 03:00 AM 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 Ravi Kiran July 21st, 1999, 03:03 AM 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! Dr_Michael July 21st, 1999, 03:09 AM 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 Ravi Kiran July 21st, 1999, 03:52 AM 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. Jan Businger July 21st, 1999, 04:12 AM 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 Chris Eastwood July 21st, 1999, 04:25 AM 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 Jan Businger July 21st, 1999, 05:56 AM 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 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") Ravi Kiran July 22nd, 1999, 06:14 AM 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 Ravi Kiran July 22nd, 1999, 06:16 AM 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 Lothar Haensler July 22nd, 1999, 06:28 AM not true. this code loads a richtextbox Licenses.Add "Richtext.Richtextctrl.1" Controls.Add "Richtext.Richtextctrl.1", "name" July 22nd, 1999, 10:27 AM I added references to the libraries I could find (from the References box) and still couldn't figure it out. "VB.ControlName" doesn't work, "ControlName" doesn't work, ControlName.ProgID doesn't work....I'm confused. :-( Jan Businger July 22nd, 1999, 10:35 AM Thanks Ravi I didn't realize that Jan July 22nd, 1999, 11:51 AM That looks really helpful, but how do you find out the correct things to fill in the blanks as in your example? Lothar Haensler July 22nd, 1999, 11:56 AM I'm not sure what you mean by "fill in the blanks". you can look up the progids of all OCXs in the registry. Or, easier: add a refernce to that OCX. Then look up the library name in the object browser. then use that name in your add statement and watch the error message! It will tell you what the correct progid is. Funny, isn't it? There may be better ways but this one worked. July 22nd, 1999, 12:40 PM That did the trick! Thanks again........... betsey codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |