CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Guest

    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!


  2. #2
    Join Date
    Jul 1999
    Location
    Athens, Hellas
    Posts
    769

    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

  3. #3
    Join Date
    May 1999
    Posts
    3,332

    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.


  4. #4
    Join Date
    Jun 1999
    Location
    Switzerland
    Posts
    58

    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


  5. #5
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    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

  6. #6
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    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!


  7. #7
    Join Date
    Jul 1999
    Location
    Athens, Hellas
    Posts
    769

    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

  8. #8
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    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.


  9. #9
    Join Date
    Jun 1999
    Location
    Switzerland
    Posts
    58

    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


  10. #10
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    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

  11. #11
    Join Date
    Jun 1999
    Location
    Switzerland
    Posts
    58

    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


  12. #12
    Guest

    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")


  13. #13
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    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


  14. #14
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    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


  15. #15
    Join Date
    May 1999
    Posts
    3,332

    Re: THANK YOU ALL!

    not true.
    this code loads a richtextbox

    Licenses.Add "Richtext.Richtextctrl.1"
    Controls.Add "Richtext.Richtextctrl.1", "name"





Page 1 of 2 12 LastLast

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

Featured