Results 1 to 5 of 5

Thread: combobox click event suggestion wanted

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Hi,

    I have a combo box with 4 items. I have a Select Case event behind the combobox_click to run code based on the item selected..

    However, what I need to do is only run the code if the item selected is different from the already selected item.. For example, the combo box currently shows Item1. If the user clicks the down arrow and selects Item1, I don't want the code to be executed.. But, if the user selects a different item, the code should be run..

    Thanks,

    Dan

  2. #2
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Use a Static variable

    In your sub, declare a variable thus:

    Code:
      Static lastSelected as Integer
    Then, check the value of lastSelected before submitting to
    the Select Case. At the end of the Sub, set lastSelected
    to the value of the currently selected index.

    The idea here is that the Static keyword will let your sub
    keep the same value for a variable as it did the last time
    the sub was executed.

    Have fun...
    Paul Lewis

  3. #3
    Member
    Join Date
    May 2000
    Location
    Canada
    Posts
    52
    How about this. Use the tag property of the combo box to store the original text in. Then in the click event check to see if the tag is the same as the selected item. I use this often and it works quite well.
    Using the tag property you do not need to define an additional variable.


    [Edited by d.paulson on 09-29-2000 at 12:22 AM]

  4. #4
    Addicted Member jcouture100's Avatar
    Join Date
    Aug 1999
    Posts
    141
    Save the original combobox text to a variable and then
    save the newly clicked text to another variable. Then
    compare the text in the click event. If the two are the
    same then skip the processing, if they are different, then
    process the click event.

    Example:
    Code:
    Option Explicit
    Dim strOld As String
    
    Private Sub Combo1_Click()
        Dim strNew As String
        strNew = Combo1.Text
        If strNew = strOld Then
            Exit Sub
        Else
            strOld = Combo1.Text
            MsgBox "Processing Stuff"
        End If
    End Sub
    
    Private Sub Form_Load()
        Dim intCnt As Integer
        'Load the combobox with some stuff for testing
        For intCnt = 1 To 10
            Combo1.AddItem "Item " & CStr(intCnt)
        Next
        'Will probably want a "loading info" flag here for
        'testing so you don't process a click event on
        'form load.
        Combo1.ListIndex = 0
        strOld = Combo1.Text
    End Sub
    Hope this helps.
    JC

  5. #5
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    Howdy howdy. Here is a quick little fix that I put together, this might work for you (I did a simple project)

    Code:
    Dim strPrevious As String
    
    Private Sub Combo1_Click()
        If Combo1.List(Combo1.ListIndex) = strPrevious Then Exit Sub
        Select Case Combo1.ListIndex
            Case 0
                MsgBox Combo1.List(Combo1.ListIndex)
                strPrevious = Combo1.List(Combo1.ListIndex)
            Case 1
                MsgBox Combo1.List(Combo1.ListIndex)
                strPrevious = Combo1.List(Combo1.ListIndex)
            Case 2
                MsgBox Combo1.List(Combo1.ListIndex)
                strPrevious = Combo1.List(Combo1.ListIndex)
            Case 3
                MsgBox Combo1.List(Combo1.ListIndex)
                strPrevious = Combo1.List(Combo1.ListIndex)
        End Select
    End Sub
    
    Private Sub Form_Load()
        Combo1.AddItem "Item1"
        Combo1.AddItem "Item2"
        Combo1.AddItem "Item3"
        Combo1.AddItem "Item4"
        Combo1.ListIndex = 0
    
    End Sub
    Hope that helps.
    -Excalibur

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