Results 1 to 14 of 14

Thread: Efficient coding??

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    74
    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

  2. #2
    Guest
    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.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    74

    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

  4. #4
    Guest
    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.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    74

    Talking Whoopsie

    Sorry Matthew for being so dang confusing,
    I guess the late nights are catching on to me too

    I'll explain by example, cuz it's the best and easiest way:

    so I have 3 combo boxes, let's name 'em combo1, combo2, etc.

    Now in the click event of these combo boxes I run Do_Something

    I was wondering instead of having Do_something for the click events of combo 1-3, is there some way to have a procedure where on the event any combo box is clicked I run Do_Something.

    So for any combo box I click I Do_something

    But I don't want to make the 3 combo boxes into a control array. Is this possible?

    Hope I made sense this time, if not there's always tomorrow
    sorry for all the confusion


  6. #6
    Guest
    I'm afraid you'll have to add the code into each Combobox.
    Unless...of course, you have a control array.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    74

    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]

  8. #8
    Addicted Member
    Join Date
    Oct 2000
    Location
    Vienna/Austria
    Posts
    132
    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

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    74

    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!

  10. #10
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363
    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.
    Wade

  11. #11
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    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.

  12. #12
    Fanatic Member
    Join Date
    Jan 1999
    Location
    UK
    Posts
    554

    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
    {;->

  13. #13
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    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}

  14. #14
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363
    An ActiveX control would be nice though...wouldn't have to replicate the call each time...hee hee
    Wade

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width