Valhalla Legends Forums Archive | Visual Basic Programming | VB6 Creating Text boxes and buttons out of thin air

AuthorMessageTime
Tontow
How do I automate the creation of an unknown number buttons and text boxs?
September 18, 2004, 3:17 AM
Grok
[quote author=Tontow link=board=31;threadid=8723;start=0#msg80648 date=1095477439]
How do I automate the creation of an unknown number buttons and text boxs?

[/quote]
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.
[code]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[/code]

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

[code]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[/code]
September 18, 2004, 4:23 AM
Puzzle
Another alternative would be to draw one using the API.
[code] 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[/code]

Of course, you would have to add the declarations.
September 21, 2004, 9:57 AM
Tontow
and Unload would remove it, correct?
September 22, 2004, 11:57 PM

Search