|
-
August 27th, 2003, 11:08 PM
#1
creating controls
Here is my code:
Dim WithEvents kk As TextBox
Private Sub Form_Load()
For i = 0 To 5
Set kk = Controls.Add("vb.textbox", "Doug" & i)
kk.Width = 1200
kk.Top = 2500 + i * 300
kk.Left = 600
kk.Height = 285
kk.Text = kk.Name '--> doug
kk.Visible = True
Next i
End Sub
Private Sub kk_click()
MsgBox "Thanks for the help", , "hmm"
End Sub
ok; what i can do is create controlls and then modify their contents(text,position etc) after they have been created. What i can not do is raiseEvents with the newly created controls. what i would like to do is on _click (or change etc) have that control execute code. Which would be the same as having an array of text1(0)..text1(5) then on
text1_click(index as integer)
'do whatever
end sub
if you were to copt my code into a from you would see that when you click on the last text box you get a messassage but the other textboxes are 'inactive' --> i want to change this so they will all execuate the same code.
Someone told me that i need to add each control i created to a class and then do some stuff, but i don't have any knowledge of this.
If this is unclear in any way let me know.
- i know i posted this before - but i'm in dire need of this to work pls forgive.
Thanks
-Doug
-
August 28th, 2003, 12:13 AM
#2
Well, why not to create an array of textbox control?.. All you have to do during design-time is to copy and then paste an instance of the control, you'll be prompted if you want to create a control array and then choose yes, then delete the control and retain the one which has index is 0..
Code:
Private Sub Form_Load()
For i = 0 To 5
If i <> 0 Then
Load Text1(i)
End If
With Text1(i)
.Width = 1200
.Top = 2500 + i * 300
.Left = 600
.Height = 285
.Text = .Name & "(" & i & ")"
.Visible = True
End With
Next i
End Sub
Private Sub Text1_Click(Index As Integer)
MsgBox "You clicked " & Text1(0).Name & "(" & Index & ")"
End Sub
Busy 
-
August 28th, 2003, 03:30 AM
#3
I thought about doing that but i will be using this code as a module (so inessence many forms) and don't want to create a control on each form.
thanks though
-Doug
-
August 28th, 2003, 11:46 AM
#4
I've worked on a hook class for textbox before, I trimmed it down to the basic. It allow you to set events for any existing textbox.
here is the class code :
Code:
Option Explicit
' ClsHook
' Using this class, you will not have to code the same
' validation over and over.
'
' Note that the original events of the textbox are triggered
' before thoses of the hooks.
' If the hook make a modif to the value of the textbox,
' the _Change event is triggered again
' (watch for _change loops)
'
' Author : Francois Gelinas aka Heulsay @ hotmail.com
' Date : September 2002 - April 2003
Public WithEvents HookTxt As TextBox
Private Declare Function GetParent Lib "user32" (ByVal Hwnd As Long) As Long
' ====================================
' Function HookTxt
' Receive TextBoxHwnd wich is the handle of the textbox we want to hook.
' Return True if the hook is successfull (we found the control) or false in other case.
' ====================================
Public Function SetHookTxt(ByRef TextBoxHwnd As Long) As Boolean
Dim ParentHwnd As Long
Dim ThisForm As Form
Dim ThisControl As Control
' Get the parent first, then search this container for the
' textbox, this make the search for the textbox much
' faster in a big project.
ParentHwnd = GetParent(TextBoxHwnd)
For Each ThisForm In Forms
If (ThisForm.Hwnd = ParentHwnd) Then
For Each ThisControl In ThisForm.Controls
If (ThisControl.Hwnd = TextBoxHwnd) Then
Set HookTxt = ThisControl
SetHookTxt = True
Exit Function
End If
Next ThisControl
End If
Next ThisForm
SetHookTxt = False
End Function
' Now let's hook the events for this textbox
Private Sub HookTxt_Click()
MsgBox "Yeah baby"
End Sub
' ====================================
' Class Terminate
' Unset the control
' ====================================
Private Sub Class_Terminate()
Set HookTxt = Nothing
End Sub
and here is a simple application fo the class, used on a simple form, 2 command buttons and 3 textbox
Code:
Option Explicit
Private cHook() As ClsHookTxt
Private Sub Command1_Click()
' Add 2 hook , the first 2 textboxes
ReDim cHook(2)
Set cHook(1) = New ClsHookTxt
Set cHook(2) = New ClsHookTxt
cHook(1).SetHookTxt Text1.Hwnd
cHook(2).SetHookTxt Text2.Hwnd
End Sub
Private Sub Command2_Click()
' Add the third textbox to the hook list
ReDim Preserve cHook(3)
Set cHook(3) = New ClsHookTxt
cHook(3).SetHookTxt Text3.Hwnd
End Sub
Hope this will help you out.
Also, if you plan to use the _Change event, watch out for infinite loops
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|