Results 1 to 10 of 10

Thread: Creating a form at runtime without adding it in IDE

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2000
    Location
    Slovenia, Europe
    Posts
    58

    Question Creating a form at runtime without adding it in IDE

    I have a class like this
    VB Code:
    1. Private WithEvents frm As Form
    2.  
    3. Private Sub Class_Initialize()
    4.     Set frm =  new Form
    5. End Sub

    I would like to create a new form when the class initializes.
    And I don't want to add a form at design time.
    The 'new Form' stuff doesnt seem to work.

    Any ideas?

    Regards, Tadej

  2. #2
    Fanatic Member 007shahid's Avatar
    Join Date
    Feb 2001
    Posts
    562
    You can add controls without adding them to the form. See http://www.vbforums.com/showthread.php?threadid=174267
    But I don't know if you can do the same with a form. I guess you will have to add a form during design time and load a new one during runtime. If you don't know how...
    In a new Standard EXE Project, add a Command Button to Form1
    Paste the following in the code section
    VB Code:
    1. Option Explicit
    2. Dim frm As Form1
    3.  
    4. Private Sub Command1_Click()
    5. Set frm = New Form1
    6. frm.Show
    7. End Sub
    THE TIME/WEATHER IS
    Don't know how to use APIs or have problem with them,
    Download API-Guide & API-Viewer from http://www.allapi.net

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2000
    Location
    Slovenia, Europe
    Posts
    58
    I don't want to design the form.
    I want to create it from scratch.

    I also tried the next logical thing
    Forms.Add

    ... but nope, it doesnt work either.


    I am asking myself if it is even possible

    Thanks anyway for trying.

  4. #4
    Fanatic Member 007shahid's Avatar
    Join Date
    Feb 2001
    Posts
    562
    VB.Global.Forms is a read only object showing 'All loaded forms in an application'.
    VB.Global.Licenses 'Manipulates a collection of control licenses for use with Controls.Add'.
    So you could not get it that way. Before I had tried that too.

    Try adding a blank form to the project. You can modify the form properties. Then you can add the controls using the above method.
    THE TIME/WEATHER IS
    Don't know how to use APIs or have problem with them,
    Download API-Guide & API-Viewer from http://www.allapi.net

  5. #5
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011
    To create a form dynamically you'll have to create an array of an already created form(say a blueprint)..
    VB Code:
    1. Public frmArr() As myFrm   'Declare it in a module
    2. Public frmCount As Long   'This too
    3.  
    4. Private Sub Class_Initialize()
    5.  frmCount = frmCount + 1
    6.  ReDim Preserve frmArr(frmCount)
    7.  Load frmArr(frmCount) = New myFrm
    8.  frmArr(frmCount).Show
    9. End Sub
    You can handle events in myFrm(check indices)

  6. #6
    Fanatic Member 007shahid's Avatar
    Join Date
    Feb 2001
    Posts
    562
    He doesn't want to design a form.
    I don't want to design the form.
    I want to create it from scratch.
    THE TIME/WEATHER IS
    Don't know how to use APIs or have problem with them,
    Download API-Guide & API-Viewer from http://www.allapi.net

  7. #7
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011
    What would it mean? Creating from scratch? Does he want to add controls dynamically??
    He cannot add any form except the way i mentioned...once the form is added he can always use Form.Controls and Form.Lisences Collection to add any controls to the form..

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    vb does not have the ability to create forms from scratch...

    but if you REALLY REALLY want to then

    VB Code:
    1. Option Explicit
    2.  
    3. Type WNDCLASS
    4.     style As Long
    5.     lpfnwndproc As Long
    6.     cbClsextra As Long
    7.     cbWndExtra2 As Long
    8.     hInstance As Long
    9.     hIcon As Long
    10.     hCursor As Long
    11.     hbrBackground As Long
    12.     lpszMenuName As String
    13.     lpszClassName As String
    14. End Type
    15. Type POINTAPI
    16.     x As Long
    17.     y As Long
    18. End Type
    19. Type Msg
    20.     hWnd As Long
    21.     message As Long
    22.     wParam As Long
    23.     lParam As Long
    24.     time As Long
    25.     pt As POINTAPI
    26. End Type
    27.  
    28. Declare Function RegisterClass Lib "user32" Alias "RegisterClassA" (Class As WNDCLASS) As Long
    29. Declare Function UnregisterClass Lib "user32" Alias "UnregisterClassA" (ByVal lpClassName As String, ByVal hInstance As Long) As Long
    30. 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
    31. 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
    32. Declare Sub PostQuitMessage Lib "user32" (ByVal nExitCode As Long)
    33. Declare Function GetMessage Lib "user32" Alias "GetMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long) As Long
    34. Declare Function TranslateMessage Lib "user32" (lpMsg As Msg) As Long
    35. Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageA" (lpMsg As Msg) As Long
    36. Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
    37. Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    38. Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    39. 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
    40. Declare Function LoadCursor Lib "user32" Alias "LoadCursorA" (ByVal hInstance As Long, ByVal lpCursorName As Any) As Long
    41.  
    42.  
    43. Public Const COLOR_WINDOW = 5
    44. Public Const BM_CLICK = 245
    45. Public Const CS_VREDRAW = &H1
    46. Public Const CS_HREDRAW = &H2
    47. Public Const CS_KEYCVTWINDOW = &H4
    48. Public Const WS_OVERLAPPED = &H0&
    49. Public Const WS_POPUP = &H80000000
    50. Public Const WS_CHILD = &H40000000
    51. Public Const WS_MINIMIZE = &H20000000
    52. Public Const WS_VISIBLE = &H10000000
    53. Public Const WS_DISABLED = &H8000000
    54. Public Const WS_CLIPSIBLINGS = &H4000000
    55. Public Const WS_CLIPCHILDREN = &H2000000
    56. Public Const WS_MAXIMIZE = &H1000000
    57. Public Const WS_CAPTION = &HC00000 ' WS_BORDER Or WS_DLGFRAME
    58. Public Const WS_BORDER = &H800000
    59. Public Const WS_DLGFRAME = &H400000
    60. Public Const WS_VSCROLL = &H200000
    61. Public Const WS_HSCROLL = &H100000
    62. Public Const WS_SYSMENU = &H80000
    63. Public Const WS_THICKFRAME = &H40000
    64. Public Const WS_GROUP = &H20000
    65. Public Const WS_TABSTOP = &H10000
    66. Public Const WS_MINIMIZEBOX = &H20000
    67. Public Const WS_MAXIMIZEBOX = &H10000
    68. Public Const WS_TILED = WS_OVERLAPPED
    69. Public Const WS_ICONIC = WS_MINIMIZE
    70. Public Const WS_SIZEBOX = WS_THICKFRAME
    71. Public Const WS_OVERLAPPEDWINDOW = (WS_OVERLAPPED Or WS_CAPTION Or WS_SYSMENU Or WS_THICKFRAME Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX)
    72. Public Const WS_TILEDWINDOW = WS_OVERLAPPEDWINDOW
    73. Public Const WS_POPUPWINDOW = (WS_POPUP Or WS_BORDER Or WS_SYSMENU)
    74. Public Const WS_CHILDWINDOW = (WS_CHILD)
    75. Public Const WS_EX_CLIENTEDGE = 512
    76. Public Const WM_DESTROY = &H2
    77. Public Const WM_MOVE = &H3
    78. Public Const WM_SIZE = &H5
    79. Public Const SW_HIDE = 0
    80. Public Const SW_SHOWNORMAL = 1
    81. Public Const SW_NORMAL = 1
    82. Public Const SW_SHOW = 5
    83. Public Const SW_MINIMIZE = 6
    84. Public Const IDC_ARROW = 32512&
    85. Public Const GWL_WNDPROC = -4
    86.  
    87. Public lngHwndButton As Long
    88. Public lngHwndLabel2 As Long
    89. Public lngHwndLabel1 As Long
    90. Public lngHwndTextbox As Long
    91. Public hWndForm As Long
    92.  
    93. Public Sub Main()
    94.     Dim lngStyle As Long
    95.  
    96.     'Register Class
    97.     Call RegisterClassProc
    98.     'Create a FORM
    99.     lngStyle = WS_OVERLAPPED Or WS_SYSMENU Or WS_CLIPCHILDREN Or WS_CLIPSIBLINGS
    100.     hWndForm = CreateWindowEx(0, "MyCoolClass", "Dynamic Form using CreateEx API from Serge", lngStyle, 0, 0, 400, 300, 0, 0, App.hInstance, ByVal 0&)
    101.     'Create controls on the form
    102.     lngHwndLabel1 = CreateWindowEx(0, "Static", "New Label1", WS_CHILD, 50, 25, 100, 25, hWndForm, 0, App.hInstance, ByVal 0&)
    103.     lngHwndLabel2 = CreateWindowEx(0, "Static", "New Label2", WS_CHILD, 50, 55, 100, 25, hWndForm, 0, App.hInstance, ByVal 0&)
    104.     lngHwndButton = CreateWindowEx(0, "Button", "New Button", WS_CHILD, 50, 90, 100, 25, hWndForm, 0, App.hInstance, ByVal 0&)
    105.     lngHwndTextbox = CreateWindowEx(WS_EX_CLIENTEDGE, "edit", "Sample Text", WS_CHILD, 50, 125, 100, 25, hWndForm, 0, App.hInstance, ByVal 0&)
    106.     If hWndForm <> 0 Then ShowWindow hWndForm, SW_SHOWNORMAL
    107.     'Show controls on the new form
    108.     ShowWindow lngHwndTextbox, SW_SHOWNORMAL
    109.     ShowWindow lngHwndButton, SW_SHOWNORMAL
    110.     ShowWindow lngHwndLabel1, SW_SHOWNORMAL
    111.     ShowWindow lngHwndLabel2, SW_SHOWNORMAL
    112.     MessageProc
    113.    
    114. End Sub
    115. Private Sub MessageProc()
    116.     Dim ms As Msg
    117.    
    118.     Do While GetMessage(ms, 0, 0, 0)
    119.         TranslateMessage ms
    120.         DispatchMessage ms
    121.     Loop
    122. End Sub
    123.  
    124. Function ProcessWndProc(ByVal lWndProc As Long) As Long
    125.     ProcessWndProc = lWndProc
    126. End Function
    127.  
    128. Public Sub RegisterClassProc()
    129.     Dim ws As WNDCLASS
    130.    
    131.     'Prepare Class to be registered
    132.     ws.style = CS_HREDRAW + CS_VREDRAW
    133.     ws.lpfnwndproc = ProcessWndProc(AddressOf WindowProc)
    134.     ws.cbClsextra = 0
    135.     ws.cbWndExtra2 = 0
    136.     ws.hInstance = App.hInstance
    137.     ws.hIcon = 0
    138.     ws.hCursor = LoadCursor(0, IDC_ARROW)
    139.     ws.hbrBackground = COLOR_WINDOW
    140.     ws.lpszMenuName = 0
    141.     ws.lpszClassName = "MyCoolClass"
    142.     'Register Class
    143.     RegisterClass ws
    144. End Sub
    145.  
    146. Private Function WindowProc(ByVal hWnd As Long, ByVal message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    147.     Select Case message
    148.         Case WM_DESTROY
    149.             PostQuitMessage (0)
    150.             UnregisterClass "MyCoolClass", App.hInstance
    151.     End Select
    152.     WindowProc = DefWindowProc(hWnd, message, wParam, lParam)
    153. End Function

    code originally posted by Serge..

  9. #9
    Fanatic Member 007shahid's Avatar
    Join Date
    Feb 2001
    Posts
    562
    He could just use VC++ instead of doing the above code. But I still feel he should just add a blank form and add controls using Licenses.Add(). Infact it was moin khan who showed me how to add controls like that in the above link
    THE TIME/WEATHER IS
    Don't know how to use APIs or have problem with them,
    Download API-Guide & API-Viewer from http://www.allapi.net

  10. #10

    Thread Starter
    Member
    Join Date
    Jun 2000
    Location
    Slovenia, Europe
    Posts
    58
    Thanks everyone for your input.
    I think I'll use VC++ for making my class and VB to make only the UI.

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