-
Oct 20th, 2000, 11:36 PM
#1
Thread Starter
Lively Member
Well I'm trying to cut down my code, tyring to make it more efficient. I'm hoping you VB experts can clarify a few things for me:
1) I noticed that most of the combo boxes and text boxes call the same function. Is there a way to make a general event procedure for all combo boxes? Like for all combo boxes that undergo the click event I execeute so and so. Is this possible? I know control arrays can achieve this, but I don't want to create a control array, I need my objects to be unique.
2) There are times when I need to edit a certain property for all text boxes (or any other objects), Is there a way to change all of them without calling each object? Can I somehow use a loop?
3) This has nothing to do with efficient coding, it just popped in my mind , How do I make those line seperators in the menu? I want to partition my menu options just like how vb and other application do it
Thanks again for all the help! Hope to learn somethign new again from you guys
-
Oct 21st, 2000, 12:03 AM
#2
Q1 & 2:
Code:
'Textboxes
For Each MyControl In Me.Controls
If TypeOf MyControl Is TextBox Then MyControl.Text = ""
Next
'Comboboxes
For Each MyControl In Me.Controls
If TypeOf MyControl Is Combobox Then MyControl.ListIndex = 0
Next
Q3:
Put a dash (-) as the caption of the menu.
-
Oct 21st, 2000, 12:26 AM
#3
Thread Starter
Lively Member
Thanks Matthew
I had a feeling I could somehow use the loop, thanks for showing me the way.
do I have to declare MyControl or is it some constant?
I don't see though how the code can help me with Q1. Unless I don't see it yet. Forgive me I can be slow at times
What I want to do is instead of putting the same code over and over in all the click events of a certain type of object (like a combo box), I have one general one for all my combo boxes. Is that possible?
Thanks again Matthew, I learned a great deal
-
Oct 21st, 2000, 12:36 AM
#4
You can use the code above. It will help you with Q1. MyControl is as is. You said you wanted to change all the comboboxes:
Code:
Private Sub Combo1_Change()
For Each MyControl In Me.Controls
If TypeOf MyControl Is ComboBox Then MyControl.Text = Combo1.Text
Next
End Sub
Private Sub Combo1_Click()
For Each MyControl In Me.Controls
If TypeOf MyControl Is ComboBox Then MyControl.Text = "Comboboxes"
Next
End Sub
Used just like a control array.
If this is not what you want, just explain a bit more and I'll see what I can do.
Because that's what it looks like you are askin'..how to change all the text in comboboxes or something.
Than again, it's late, I'm tired, and my eyes are struggling to stay open.
-
Oct 21st, 2000, 12:53 AM
#5
Thread Starter
Lively Member
-
Oct 21st, 2000, 01:04 AM
#6
I'm afraid you'll have to add the code into each Combobox.
Unless...of course, you have a control array.
-
Oct 21st, 2000, 01:59 AM
#7
Thread Starter
Lively Member
Let's just hope in the next VB
HEy thanks Matthew, I needed an expert to clarify what I had in mind
I guess we can just hope to be able to do something like that in the next VB
but again, thank you, I learned alot
[Edited by gin on 10-21-2000 at 03:01 AM]
-
Oct 21st, 2000, 03:56 AM
#8
Addicted Member
Hi gin !!!
Try the following found on a Excel page.
1) Create a class e.g. clsTextBox:
in the class declaration:
'class to get from specified textboxes the events
'
Public WithEvents txtTextBox As MSForms.TextBox
place your code in the event's wich you need.
Note: not all events can be catched !!!
2) create a form
In the Form declaration:
Dim TxtBox() As New clsTextBox
in the load or activated event of the form:
' set the textboxes that the class clstxtbox can
' intersect the event
'
ReDim TxtBox(4)
Set TxtBox(0).txtTextBox = Me.Controls("myTextbox1")
Set TxtBox(1).txtTextBox = Me.Controls("mytextbox2")
Set TxtBox(2).txtTextBox = Me.Controls("myTextbox3")
Set TxtBox(3).txtTextBox = Me.Controls("myTextbox4")
Set TxtBox(4).txtTextBox = Me.Controls("myTextbox5")
now all Textboxes which you have referenced with the array
have the same event routines.
-cu TheOnly
-
Oct 21st, 2000, 09:27 AM
#9
Thread Starter
Lively Member
Thanks
Thanks TheOnly!
I'll give it a try, although I don't know much about creating classes, I'll try to get some background in the topic so I can fully understand the code. I'm sure I'll figure it out
Thanks for all the help!
-
Dec 21st, 2000, 12:16 PM
#10
Hyperactive Member
If the procedure can stand alone (i.e., if it uses no global variables or unpassed form/control references), you can make your own ActiveX control with the procedure code in the event.
In your project, you can drop in the ActiveX control and the embedded code will already be with the event (though not visible).
Good luck.
-
Dec 21st, 2000, 12:49 PM
#11
Frenzied Member
hmmm it may not be the best solution, but you can subclass each item and then check from wich handle the event was fired.
In module:
Code:
Option Explicit
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Const WM_KEYDOWN = &H100
Public Const GWL_WNDPROC = (-4)
Dim PrevProc As Long
Public Sub SubClass(hWnd As Long)
PrevProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Public Sub unSubClass(hWnd As Long)
SetWindowLong hWnd, GWL_WNDPROC, PrevProc
End Sub
Public Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
WindowProc = CallWindowProc(PrevProc, hWnd, uMsg, wParam, lParam)
'Here we'll cath all the event
If uMsg = WM_KEYDOWN Then
MsgBox "A key was pressed in the listbox with hWnd: " & hWnd
End If
End Function
In a form
Code:
'add 2 listboxes
'Press a key in one of the 2 listboxes (list1 and list2) and you'll get a message that you've pressed a key in the listbox (you'll get the hwnd)
Private Sub Form_Load()
SubClass List1.hWnd
SubClass List2.hWnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
unSubClass List1.hWnd
unSubClass List2.hWnd
End Sub
I know it's a bit weak but play around with the messages and it may be effective sometime
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Dec 21st, 2000, 09:46 PM
#12
Fanatic Member
Efficiency!
From the standpoint of efficiency - in that you wish to make your code more efficient...
I would go with Mathew Gates option
Its more or less what your require,
kinda, all the common code which is executed regardless of which combo you clicked on.
Kinda like calling a seperate procedure altogether
Code:
Sub Combo1_Click()
CommonComboProcedures
End Sub
Sub Combo2_Click()
CommonComboProcedures
End Sub
Sub Combo3_Click()
CommonComboProcedures
End Sub
Sub CommonComboProcedures()
'--> Common Code
'here you can place the code which is common to all the combo's or text boxes
'--> Mathews code
'Textboxes
For Each MyControl In Me.Controls
If TypeOf MyControl Is TextBox Then
'place your code here - ie reset textbox to ""
MyControl.Text = ""
End If
Next
'Comboboxes
For Each MyControl In Me.Controls
If TypeOf MyControl Is Combobox Then
'place your code here - ie reset listindex to 0
MyControl.ListIndex = 0
End IF
Next
End Sub
DocZaf
{;->
-
Dec 22nd, 2000, 06:23 AM
#13
Fanatic Member
Originally posted by Matthew Gates
MyControl is as is.
Actually the code Matthew posted ...
Code:
Private Sub Combo1_Change()
For Each MyControl In Me.Controls
If TypeOf MyControl Is ComboBox Then MyControl.Text = Combo1.Text
Next
End Sub
won't work if you are using the Option Explicit keyword, becuase the MyControl variable is never declared. If you are using Option Explicit you have to declare the MyControl variable as a Control or Object. For example...
Code:
Option Explicit
Private Sub Combo1_Change()
Dim MyControl as Control
'Dim MyControl as Object - This also works
For Each MyControl In Me.Controls
If TypeOf MyControl Is ComboBox Then MyControl.Text = Combo1.Text
Next
End Sub
Sorry to be so anal retentive just thought I should clarify that. :P
{Insert random techno-babble here}
{Insert quote from some long gone mofo here}
-
Dec 22nd, 2000, 09:34 AM
#14
Hyperactive Member
An ActiveX control would be nice though...wouldn't have to replicate the call each time...hee hee
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
|