• Welcome to Valhalla Legends Archive.
 

VB6 Creating Text boxes and buttons out of thin air

Started by Tontow, September 17, 2004, 10:17 PM

Previous topic - Next topic

Tontow

How do I automate the creation of an unknown number buttons and text boxs?

Grok

Quote from: Tontow on September 17, 2004, 10:17 PM
How do I automate the creation of an unknown number buttons and text boxs?

These instructions are for a button, but work equally well for a text box.

We are going to create a 'control array'.

1.  Put a button on your form.  In the properties box, set the value of 'Index' to 0.  Set the value of 'Hidden' to True.  Name this button, for this example, btnFlood.

2.  In your code, for each new button you need, instantiate it with a new index.
Load btnFlood(1)     'creates a new button, btnFlood(1)
btnFlood(1).Move  newX, newY, newWidth, newHeight     'position and size it where you want it.
btnFlood(1).Caption = "Click Me 1"      'set button caption as desired
btnFlood(1).Hidden = False        'make it visible


3.  On the form, double click the button to open it's click event.

Private Sub btnFlood_Click(ByVal Index As Integer)
   'add code depending on value of Index, for example:
   Select Case Index
   Case 1    'clicked first button
   Case 2    'clicked second button
   'etc
   End Select
End Sub

Puzzle

Another alternative would be to draw one using the API.
Public Sub CreateButton(frm As Form, Caption As String, Left As Long, Top As Long, Height As Long, Width As Long, OnClick As Long, ByRef OldhWndProc As Long)
   Dim hWnd As Long

   hWnd = CreateWindowEx(0&, "BUTTON", Caption, WS_VISIBLE Or WS_CHILD Or WS_THICKFRAME, Left, Top, Height, Width, frm.hWnd, 0&, App.hInstance, 0&)
   If hWnd And OnClick <> 0 Then
       'Set the new WindowProc to manage the button events
       OldhWndProc = SetWindowLong(hWnd, GWL_WNDPROC, OnClick)
   End If
End Sub

Public Function MyWindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, lParam As Long) As Long
   'OnLeftButtonUp
   If uMsg = WM_LBUTTONUP Then
       'Make a sound
       Beep
   End If
   'Call the old WindowProc for the button
   MyWindowProc = CallWindowProc(myOldhWndProc, hWnd, uMsg, wParam, lParam)
End Function

Private Sub Command1_Click()
   CreateButton Me, "Button", 5, 5, 300, 100, AddressOf Module1.MyWindowProc, myOldhWndProc
End Sub


Of course, you would have to add the declarations.

Tontow