Results 1 to 11 of 11

Thread: Creating forms at runtime?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112

    Question

    Is it possible to create a form at runtime to add controls to? Adding controls is easy enough, but is it possible to do something like this?
    Code:
    Dim A as Userforms
    A.Create "MyForm"
    A.Show
    ...
    Doing this in API is doable but not very good. The goal is to create a userform at runtime.

  2. #2
    Fanatic Member
    Join Date
    Jan 1999
    Location
    UK
    Posts
    554
    I think its best to create the form first and then declare it....

    Code:
      Dim MyDynamicForm as MyCreatedForm
    DocZaf
    {;->


  3. #3
    Guest
    Just like Zaf's code..all you need is something like:

    Code:
    Dim NewForm As Form1
    Newform.show
    This will show an exact copy of form1.

  4. #4
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    For mdi usage have a mdi form called frmMain and a mdi child called frmNew.

    Dim newDocument(256) As New frmNew
    Dim newDocumentNumber As Integer

    Private Sub cmdNew_Click()
    newDocumentNumber = newDocumentNumber + 1
    Set newDocumnent(newDocumentNumber) = New frmNew
    newDocument.Show
    End Sub

    This should do it.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112
    I know the way to declare a new instance of an allready existing class (Form1).
    What i want is to create a normal Form at runtime with no Forms predeclared at designtime.
    Is it possible? Only in API? Can one add Forms controls with normal events to the API created form?

  6. #6
    Lively Member
    Join Date
    Jul 2000
    Location
    Vaxjo, Sweden
    Posts
    85
    Why would you want to do this? Just curious. There is a small workaround you can do. Add an empty form to the project and declare insatnces of it. Then add your controls to it dynamically. But maybe that's not what you're trying to do...

    //Anders

  7. #7

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112
    Heh. Why am I trying to do this? Hmmm. I know that I can declare an empty form and then create instances of it. But I would like to create a Class that creates forms, controls and eventhandlers. This class should be easy to use on different projects without having to declare a static empty form to begin with.
    I have allready made a class that adds controls to an empty form (without controllarrays or predeclared controls) and sets a pointer for each control to an eventhandler function (This way several controls can use the same event function). Now I want to create a Form in the same way.

  8. #8
    New Member
    Join Date
    Jul 2000
    Posts
    10
    Hi Thomas,

    you have almost the same problem than me. I'm desperately looking for a possible eventhandling for dynamically created controls. Do you have a solution for that?

    You can also write to [email protected].

    Thanks

  9. #9
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    But if you're really up for a creating form from scratch, then you can do something like this (this will create 2 Labels, Command Button and a TextBox):
    Code:
    Option Explicit
    
    Type WNDCLASS
        style As Long
        lpfnwndproc As Long
        cbClsextra As Long
        cbWndExtra2 As Long
        hInstance As Long
        hIcon As Long
        hCursor As Long
        hbrBackground As Long
        lpszMenuName As String
        lpszClassName As String
    End Type
    Type POINTAPI
        x As Long
        y As Long
    End Type
    Type Msg
        hWnd As Long
        message As Long
        wParam As Long
        lParam As Long
        time As Long
        pt As POINTAPI
    End Type
    
    Declare Function RegisterClass Lib "user32" Alias "RegisterClassA" (Class As WNDCLASS) As Long
    Declare Function UnregisterClass Lib "user32" Alias "UnregisterClassA" (ByVal lpClassName As String, ByVal hInstance As Long) As Long
    Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
    Declare Function DefWindowProc Lib "user32" Alias "DefWindowProcA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Declare Sub PostQuitMessage Lib "user32" (ByVal nExitCode As Long)
    Declare Function GetMessage Lib "user32" Alias "GetMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long) As Long
    Declare Function TranslateMessage Lib "user32" (lpMsg As Msg) As Long
    Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageA" (lpMsg As Msg) As Long
    Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
    Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Declare Function LoadCursor Lib "user32" Alias "LoadCursorA" (ByVal hInstance As Long, ByVal lpCursorName As Any) As Long
    
    
    Public Const COLOR_WINDOW = 5
    Public Const BM_CLICK = 245
    Public Const CS_VREDRAW = &H1
    Public Const CS_HREDRAW = &H2
    Public Const CS_KEYCVTWINDOW = &H4
    Public Const WS_OVERLAPPED = &H0&
    Public Const WS_POPUP = &H80000000
    Public Const WS_CHILD = &H40000000
    Public Const WS_MINIMIZE = &H20000000
    Public Const WS_VISIBLE = &H10000000
    Public Const WS_DISABLED = &H8000000
    Public Const WS_CLIPSIBLINGS = &H4000000
    Public Const WS_CLIPCHILDREN = &H2000000
    Public Const WS_MAXIMIZE = &H1000000
    Public Const WS_CAPTION = &HC00000 ' WS_BORDER Or WS_DLGFRAME
    Public Const WS_BORDER = &H800000
    Public Const WS_DLGFRAME = &H400000
    Public Const WS_VSCROLL = &H200000
    Public Const WS_HSCROLL = &H100000
    Public Const WS_SYSMENU = &H80000
    Public Const WS_THICKFRAME = &H40000
    Public Const WS_GROUP = &H20000
    Public Const WS_TABSTOP = &H10000
    Public Const WS_MINIMIZEBOX = &H20000
    Public Const WS_MAXIMIZEBOX = &H10000
    Public Const WS_TILED = WS_OVERLAPPED
    Public Const WS_ICONIC = WS_MINIMIZE
    Public Const WS_SIZEBOX = WS_THICKFRAME
    Public Const WS_OVERLAPPEDWINDOW = (WS_OVERLAPPED Or WS_CAPTION Or WS_SYSMENU Or WS_THICKFRAME Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX)
    Public Const WS_TILEDWINDOW = WS_OVERLAPPEDWINDOW
    Public Const WS_POPUPWINDOW = (WS_POPUP Or WS_BORDER Or WS_SYSMENU)
    Public Const WS_CHILDWINDOW = (WS_CHILD)
    Public Const WS_EX_CLIENTEDGE = 512
    Public Const WM_DESTROY = &H2
    Public Const WM_MOVE = &H3
    Public Const WM_SIZE = &H5
    Public Const SW_HIDE = 0
    Public Const SW_SHOWNORMAL = 1
    Public Const SW_NORMAL = 1
    Public Const SW_SHOW = 5
    Public Const SW_MINIMIZE = 6
    Public Const IDC_ARROW = 32512&
    Public Const GWL_WNDPROC = -4
    
    Public lngHwndButton As Long
    Public lngHwndLabel2 As Long
    Public lngHwndLabel1 As Long
    Public lngHwndTextbox As Long
    Public hWndForm As Long
    
    Public Sub Main()
        Dim lngStyle As Long
    
        'Register Class
        Call RegisterClassProc
        'Create a FORM
        lngStyle = WS_OVERLAPPED Or WS_SYSMENU Or WS_CLIPCHILDREN Or WS_CLIPSIBLINGS
        hWndForm = CreateWindowEx(0, "MyCoolClass", "Dynamic Form using CreateEx API from Serge", lngStyle, 0, 0, 400, 300, 0, 0, App.hInstance, ByVal 0&)
        'Create controls on the form
        lngHwndLabel1 = CreateWindowEx(0, "Static", "New Label1", WS_CHILD, 50, 25, 100, 25, hWndForm, 0, App.hInstance, ByVal 0&)
        lngHwndLabel2 = CreateWindowEx(0, "Static", "New Label2", WS_CHILD, 50, 55, 100, 25, hWndForm, 0, App.hInstance, ByVal 0&)
        lngHwndButton = CreateWindowEx(0, "Button", "New Button", WS_CHILD, 50, 90, 100, 25, hWndForm, 0, App.hInstance, ByVal 0&)
        lngHwndTextbox = CreateWindowEx(WS_EX_CLIENTEDGE, "edit", "Sample Text", WS_CHILD, 50, 125, 100, 25, hWndForm, 0, App.hInstance, ByVal 0&)
        If hWndForm <> 0 Then ShowWindow hWndForm, SW_SHOWNORMAL
        'Show controls on the new form
        ShowWindow lngHwndTextbox, SW_SHOWNORMAL
        ShowWindow lngHwndButton, SW_SHOWNORMAL
        ShowWindow lngHwndLabel1, SW_SHOWNORMAL
        ShowWindow lngHwndLabel2, SW_SHOWNORMAL
        MessageProc
        
    End Sub
    Private Sub MessageProc()
        Dim ms As Msg
        
        Do While GetMessage(ms, 0, 0, 0)
            TranslateMessage ms
            DispatchMessage ms
        Loop
    End Sub
    
    Function ProcessWndProc(ByVal lWndProc As Long) As Long
        ProcessWndProc = lWndProc
    End Function
    
    Public Sub RegisterClassProc()
        Dim ws As WNDCLASS
        
        'Prepare Class to be registered
        ws.style = CS_HREDRAW + CS_VREDRAW
        ws.lpfnwndproc = ProcessWndProc(AddressOf WindowProc)
        ws.cbClsextra = 0
        ws.cbWndExtra2 = 0
        ws.hInstance = App.hInstance
        ws.hIcon = 0
        ws.hCursor = LoadCursor(0, IDC_ARROW)
        ws.hbrBackground = COLOR_WINDOW
        ws.lpszMenuName = 0
        ws.lpszClassName = "MyCoolClass"
        'Register Class
        RegisterClass ws
    End Sub
    
    Private Function WindowProc(ByVal hWnd As Long, ByVal message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        Select Case message
            Case WM_DESTROY
                PostQuitMessage (0)
                UnregisterClass "MyCoolClass", App.hInstance
        End Select
        WindowProc = DefWindowProc(hWnd, message, wParam, lParam)
    End Function

  10. #10

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112
    But will I be able to add VB-controls to the from-scratch created form? Tricky I bet, but probably doable by getting the right pointers and right objects.

  11. #11
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Yeap, it will be tricky. You would have to subclass each control on the form to respond to their events.
    That's why Visual Basic was invented (well, kind of), so you don't have to worry about GUI stuff.
    It's just for my own knowledge, I used to do stuff like that: creating forms at runtime, handling control behavior etc...

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