For Each obj In Me.Controls If TypeOf obj Is TextBox Then ......'' 用obj.Text取得文本框中的文本 End If If TypeOf obj Is ListBox Then ......'' 对ListBox进行相应操作 End If Next
Private Sub Form_Load() Dim myObj As TextBoxFor Each myObj In Form1.Controls myObj.Text = "abc" Next End Sub
dim ctl as control For Each ctl In Me.Controls If TypeOf ctl Is TextBox Then ......... End If If TypeOf ctl Is ListBox Then ...... End If Next
Dim Ctrl As ControlFor Each Ctrl In Controls Select Case TypeName(Ctrl) Case "TextBox" '... Case "CommandButton" '... Case "Label" '... Case Else End Select Next
说来说去就是一个for循环,至于怎么实现,方法应该很多也很容易呀,楼主到底要实现什么东西这么难吗?
Option ExplicitPrivate Sub Command1_Click() EnumChildWindows Form2.hwnd, AddressOf enumchildwindow, ByVal 0& End SubPrivate Sub Form_Load() Form2.Show End Sub-------------------------------------------------- Option Explicit Public Declare Function SendMessageByStr& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Public Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As LongPublic Const WM_GETTEXT = &HDPublic Function vbGetWindowText(ahWnd As Long) As String Dim c As Integer Dim t As String If ahWnd = 0 Then Exit Function t = String(65536, " ") c = SendMessage(ahWnd, 14, 0&, 0&) SendMessageByStr ahWnd, 13, c + 1, t If InStr(t, Chr$(0)) Then t = Left(t, InStr(t, Chr$(0)) - 1) End If
If t = "" Then c = GetWindowText(ahWnd, t, 65536) vbGetWindowText = t End FunctionPublic Function remove0(ByVal src As String) As String Dim i As Integer Dim s As String For i = 1 To Len(src) If Mid(src, i, 1) <> Chr(0) Then s = s & Mid(src, i, 1) Else Exit For End If Next i remove0 = s End FunctionPublic Function enumchildwindow(ByVal hwnd As Long, ByVal lParam As Long) As Long Dim s As String * 255 Dim ss As String GetClassName hwnd, s, 255 ss = remove0(s) If ss = "ThunderTextBox" Or ss = "Edit" Then Debug.Print vbGetWindowText(hwnd) End If enumchildwindow = 1 End Function
dim ctl as control1、在同一程序中获得 For Each ctl In Me.Controls If TypeOf ctl Is TextBox Then ......... End If If TypeOf ctl Is ListBox Then ...... End If Next 2、在不同程序中获得 用消息函数
For Each obj In Me.Controls
If TypeOf obj Is TextBox Then
......'' 用obj.Text取得文本框中的文本
End If
If TypeOf obj Is ListBox Then
......'' 对ListBox进行相应操作
End If
Next
Dim myObj As TextBoxFor Each myObj In Form1.Controls
myObj.Text = "abc"
Next
End Sub
If TypeOf ctl Is TextBox Then
.........
End If
If TypeOf ctl Is ListBox Then
......
End If
Next
Select Case TypeName(Ctrl)
Case "TextBox"
'...
Case "CommandButton"
'...
Case "Label"
'...
Case Else
End Select
Next
EnumChildWindows Form2.hwnd, AddressOf enumchildwindow, ByVal 0&
End SubPrivate Sub Form_Load()
Form2.Show
End Sub--------------------------------------------------
Option Explicit
Public Declare Function SendMessageByStr& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String)
Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As LongPublic Const WM_GETTEXT = &HDPublic Function vbGetWindowText(ahWnd As Long) As String
Dim c As Integer
Dim t As String
If ahWnd = 0 Then Exit Function
t = String(65536, " ")
c = SendMessage(ahWnd, 14, 0&, 0&)
SendMessageByStr ahWnd, 13, c + 1, t
If InStr(t, Chr$(0)) Then
t = Left(t, InStr(t, Chr$(0)) - 1)
End If
If t = "" Then c = GetWindowText(ahWnd, t, 65536)
vbGetWindowText = t
End FunctionPublic Function remove0(ByVal src As String) As String
Dim i As Integer
Dim s As String
For i = 1 To Len(src)
If Mid(src, i, 1) <> Chr(0) Then
s = s & Mid(src, i, 1)
Else
Exit For
End If
Next i
remove0 = s
End FunctionPublic Function enumchildwindow(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim s As String * 255
Dim ss As String
GetClassName hwnd, s, 255
ss = remove0(s)
If ss = "ThunderTextBox" Or ss = "Edit" Then
Debug.Print vbGetWindowText(hwnd)
End If
enumchildwindow = 1
End Function
但FINDWINDOW不查找子窗体
具体方法可以去搜索一下csdn的帖子,这方面的东西很多的
For Each ctl In Me.Controls
If TypeOf ctl Is TextBox Then
.........
End If
If TypeOf ctl Is ListBox Then
......
End If
Next
2、在不同程序中获得
用消息函数