controles generados dinamicamente
Hola gente, les comento mi problema, necesito hacer una matriz de
elementos, en este caso TextBox, y agregarle eventos, mas
precisamente el click, pero me da error y se me cuelga, creo que es
por se una matriz, ya que este codigo pero para elementos idividuales
no tiene problemas, si alguien sabe que puede ser y si lo quieren
probar por favor envienmen sus sugerencias para modificar esto para
que realmente funcione. por favor al que pueda colaborar que me envie
un mail a [email protected]
gracias
'form
Public Sub CrearMatrizEtiquetas1(NroComponentes As Integer)
Dim Atributo() As TextBox
Dim i As Integer
ReDim Atributo(NroComponentes) As TextBox
For i = 0 To NroComponentes - 1
Set Atributo(i) = Me.Controls.Add("VB.TextBox", "Atributo" &
i, Me)
With Atributo(i)
.Visible = True
.Width = 6000
.Height = 500
.Left = Me.tvwArticulos.Width + Me.tvwArticulos.Left + 100
.Top = (i + 1) * 1000
oldWndProc = SetWindowLong(.hWnd, GWL_WNDPROC, AddressOf
newWndProc)
End With
Next
End Sub
'Modulo.bas
Option Explicit
'apis para crear eventos y manejarlos
Public Declare Function SetWindowLong Lib "user32.dll"
Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nindex As Long,
ByVal dwnewlong As Long) As Long
Public Declare Function CallWindowProc Lib "user32.dll"
Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As
Long, ByVal Msg As Long, ByVal wParam As Long, lParam As Long) As Long
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202
Public Const WM_ACTIVATE = &H6
Public Const WM_CLOSE = &H10
Public Const WM_HSCROLL = &H114
Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101
Public Const WM_LBUTTONDBLCLK = &H203
Public Const GWL_WNDPROC = (-4)
Global oldWndProc As Long
Public Function newWndProc(ByVal hWnd As Long, ByVal uMsg As Long,
ByVal wParam As Long, lParam As Long) As Long
Select Case uMsg
Case WM_LBUTTONDOWN: MsgBox "left button down"
Case WM_LBUTTONUP: MsgBox "left button up"
Case WM_LBUTTONDBLCLK: MsgBox "left button doble click"
'se pueden agregar mas eventos, ver la api
Case Else: 'Exit Sub
End Select
newWndProc = CallWindowProc(oldWndProc, hWnd, uMsg, wParam,
lParam)
End Function
elementos, en este caso TextBox, y agregarle eventos, mas
precisamente el click, pero me da error y se me cuelga, creo que es
por se una matriz, ya que este codigo pero para elementos idividuales
no tiene problemas, si alguien sabe que puede ser y si lo quieren
probar por favor envienmen sus sugerencias para modificar esto para
que realmente funcione. por favor al que pueda colaborar que me envie
un mail a [email protected]
gracias
'form
Public Sub CrearMatrizEtiquetas1(NroComponentes As Integer)
Dim Atributo() As TextBox
Dim i As Integer
ReDim Atributo(NroComponentes) As TextBox
For i = 0 To NroComponentes - 1
Set Atributo(i) = Me.Controls.Add("VB.TextBox", "Atributo" &
i, Me)
With Atributo(i)
.Visible = True
.Width = 6000
.Height = 500
.Left = Me.tvwArticulos.Width + Me.tvwArticulos.Left + 100
.Top = (i + 1) * 1000
oldWndProc = SetWindowLong(.hWnd, GWL_WNDPROC, AddressOf
newWndProc)
End With
Next
End Sub
'Modulo.bas
Option Explicit
'apis para crear eventos y manejarlos
Public Declare Function SetWindowLong Lib "user32.dll"
Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nindex As Long,
ByVal dwnewlong As Long) As Long
Public Declare Function CallWindowProc Lib "user32.dll"
Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As
Long, ByVal Msg As Long, ByVal wParam As Long, lParam As Long) As Long
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202
Public Const WM_ACTIVATE = &H6
Public Const WM_CLOSE = &H10
Public Const WM_HSCROLL = &H114
Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101
Public Const WM_LBUTTONDBLCLK = &H203
Public Const GWL_WNDPROC = (-4)
Global oldWndProc As Long
Public Function newWndProc(ByVal hWnd As Long, ByVal uMsg As Long,
ByVal wParam As Long, lParam As Long) As Long
Select Case uMsg
Case WM_LBUTTONDOWN: MsgBox "left button down"
Case WM_LBUTTONUP: MsgBox "left button up"
Case WM_LBUTTONDBLCLK: MsgBox "left button doble click"
'se pueden agregar mas eventos, ver la api
Case Else: 'Exit Sub
End Select
newWndProc = CallWindowProc(oldWndProc, hWnd, uMsg, wParam,
lParam)
End Function
La manera más sencilla y segura de generar controles dinámicos, es contar con un elemento inicial con Index = 0, a modo de elemento inicial de array de controles.
Luego, con Load vas creando instancias
Dim Ind as Integer
Ind = Text1.Ubound + 1
Load Text1(Ind)
Text1(Ind).Left = -...
Text1(Ind).Top = ....
Text1(Ind).Visible = true
Luego, todos los eventos tendrán un parámetro extra, que es el index. Ese parámetro es el que te va a permitir saber sobre cual elemento se generó el evento.
Saludos
Luego, con Load vas creando instancias
Dim Ind as Integer
Ind = Text1.Ubound + 1
Load Text1(Ind)
Text1(Ind).Left = -...
Text1(Ind).Top = ....
Text1(Ind).Visible = true
Luego, todos los eventos tendrán un parámetro extra, que es el index. Ese parámetro es el que te va a permitir saber sobre cual elemento se generó el evento.
Saludos
