Results 1 to 11 of 11

Thread: Problem with combo box with its Style property set to "0 - Dropdown combo"

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    566

    Problem with combo box with its Style property set to "0 - Dropdown combo"

    I have a combo box with its Style property set to "0 - Dropdown combo".
    Storing its value to the database poses a strange problem.

    Let's say I have a combo box with its Style property set to "0 - Dropdown combo"
    And I populate it like this:
    Code:
    Private Sub Form_Load()
       ......
       cbo1.Clear
       cbo1.AddItem "One"
       cbo1.AddItem "Two"
       cbo1.AddItem "Three"
       ......
    End Sub
    Then let's say for a specific record, I read its value from the database and show it like this (for simplicity I am hard-coding the value instead of actually reading it from database):
    Code:
       cbo1.Text = "Two"
    Then I click on this button:
    Code:
    Private Sub cmdProcList1_Click()
       Dim s                        As String
       
       s = ""
       s = s & "cbo1.ListCount = " & cbo1.ListCount & vbCrLf
       s = s & "cbo1.ListIndex = " & cbo1.ListIndex & vbCrLf
       s = s & "cbo1.Text = " & cbo1.Text & vbCrLf
       s = s & "cbo1.List(cbo1.ListIndex) = " & cbo1.List(cbo1.ListIndex) & vbCrLf
       
       s = s & ""
       
       Text3.Text = s
    End Sub
    Here is the result:
    cbo1.ListCount = 3
    cbo1.ListIndex = -1
    cbo1.Text = Two
    cbo1.List(cbo1.ListIndex) =
    As you see, the ListIndex is -1.
    But, I expect it to be 1

    What is wrong in here?
    And how can I fix it?

    Thanks

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,520

    Re: Problem with combo box with its Style property set to "0 - Dropdown combo"

    When style is 0, simply typing an item into the cbo text box (or setting text via code) does not change its ListIndex value. On LostFocus or Validate, you might want to see if the new text exists in one of the list items. You can use CB_FINDITEMEXACT with SendMessage API. Obviously if you select one of the items via mouse (or arrow down/up?), then the ListIndex will change.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    6,509

    Re: Problem with combo box with its Style property set to "0 - Dropdown combo"

    Ilia,

    Are you possibly wanting it to be a "2 - Dropdown list"? This limits you to the items in the list, which also guarantees that ListIndex reflects the item selected from the list, even when the keyboard is used (or code is used) to select the item.

    I guess, when using the "0 - Dropdown combo" option, the underlying code just doesn't check to see if the item you typed (or set with code) matches any of the items in the list.

    Take Care,
    Elroy

    EDIT: By the way, just as an FYI aside, I developed a User Control that allows you to change that combo "Style" property at runtime. I've got a situation where some of my users want to type in "custom" values, but other users want to be restricted to the dropdown list. To accomplish this, I designed a custom User Control that has both style ComboBoxes on it, and only shows the one according to the program's per-user setup. If that User Control would help you, ask and I'll post it.
    Last edited by Elroy; Sep 18th, 2020 at 10:35 AM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,520

    Re: Problem with combo box with its Style property set to "0 - Dropdown combo"

    Here's a simple auto-fill type solution that will update the listindex as typing occurs. Not bullet proof, needs a bit of work. For example, try backspacing after typing the letters "ti"

    .. Combo1 is style 0 (default). Start typing the word "timing"
    Code:
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
    Private Const CB_FINDSTRING As Long = &H14C
    
    Private Sub Combo1_Change()
        Dim lIndex As Long, lPos As Long
        lPos = Len(Combo1.Text)
        If lPos <> 0 Then
            lIndex = SendMessage(Combo1.hWnd, CB_FINDSTRING, -1, ByVal Combo1.Text)
            If lIndex <> -1 Then
                Combo1.ListIndex = lIndex
                Combo1.SelStart = lPos
                Combo1.SelLength = Len(Combo1.Text) - lPos
            End If
        End If
        Debug.Print "listindex "; Combo1.ListIndex
    End Sub
    
    Private Sub Form_Load()
        
        Combo1.AddItem "Time"
        Combo1.AddItem "timing"
        
    End Sub
    Pros: Updates ListIndex & auto-fills
    Cons: Doesn't allow changing character case if a match exists with any list items. Triggers combo's Click event if that is not wanted.

    But if all you want to know is if user typed something that matches an existing list item, then on LostFocus or Validate event:
    Code:
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
    Private Const CB_FINDSTRINGEXACT As Long = &H158
    
    ...
      Dim lIndex As Long
      If Combo1.ListIndex = -1 Then 
          lIndex = SendMessage(Combo1.hWnd, CB_FINDSTRINGEXACT, -1, ByVal Combo1.Text)
          If lIndex <> -1 Then Combo1.ListIndex = lIndex
      End If
    Last edited by LaVolpe; Sep 18th, 2020 at 11:28 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    566

    Re: Problem with combo box with its Style property set to "0 - Dropdown combo"

    Quote Originally Posted by Elroy View Post
    Ilia,

    Are you possibly wanting it to be a "2 - Dropdown list"? This limits you to the items in the list, which also guarantees that ListIndex reflects the item selected from the list, even when the keyboard is used (or code is used) to select the item.
    ......
    Thanks.
    No, I definitely need to set my Combobox's style property to "0 - Dropdown combo", because I need to allow the user to either select an existing item from the Combobox or type in a totally different text in there.

    Looks like, I have to put in place some additional code to make sure if the typed in value matches the existing value then set the ListIndex

    Thanks.
    Ilia

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    566

    Re: Problem with combo box with its Style property set to "0 - Dropdown combo"

    Quote Originally Posted by LaVolpe View Post
    Here's a simple auto-fill type solution that will update the listindex as typing occurs. Not bullet proof, needs a bit of work. For example, try backspacing after typing the letters "ti"
    ......
    Thanks for your help and the code.

    Just one issue is still outstanding:
    And that issue is: What to store in and retrieve from the database.

    When the Combobox's Style is set to "2 - Dropdown List", this issue is completely simple and trivial:
    We store cbo1.ListIndex into the database and when the next time we load the screen, we read the value from the database and assign that value to cbo1.ListIndex
    In other words, it is the cbo1.ListIndex that is stored in and retrieved from the database.

    But when the Combobox's Style is set to "0 - Dropdown Combo", this issue is not clear to me.

    Is it the combobox's Text property (only the Text property) that I should store in and retrieve from the database?
    Or should I store and retrieve both the Text and the ListIndex properties?
    A bit confusing.

    How do you save and retrieve this style of combobox in the database?
    Thanks.

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    6,509

    Re: Problem with combo box with its Style property set to "0 - Dropdown combo"

    Quote Originally Posted by IliaPreston View Post
    Just one issue is still outstanding:
    And that issue is: What to store in and retrieve from the database.
    Personally, it would never dawn on me to store the cbo1.ListIndex in the database. I'd always store the cbo1.Text in the database.

    There are at least two reasons for that: 1) If you add more items to the cbo1, your original cbo1.ListIndex values (you've stored in the DB) will no longer agree with the cbo1.ListIndex in the cbo1 with the additional items; 2) When using "0 - Dropdown Combo", you've got no cbo1.ListIndex value for when the user typed in a value that doesn't correspond to any of the items in the list.

    Personally, I'd never bother with storing cbo1.ListIndex in the database, even if I also stored cbo1.Text. In my mind, I'm not storing "the combobox" in the database. Rather, I'm storing data I've collected using a combobox in the database. I could have used a TextBox, or possibly another method to get that data. But it's the data, not the combobox that I'm storing.

    -------------

    EDIT: Just as a further note on this, if you're using "2 - Dropdown List" (which is not what you say you're doing, but I often do), you can run into the problem of having something in your DB that's no longer in your ComboBox list. For example, in my situation, I have a ComboBox where therapists are listed for attaching to a patient's encounter. However, the user can maintain the "master therapist list", and therapists sometimes come and go. So, when a therapist is removed from the "master list", they're no longer in the ComboBox. But, if that patient's encounter is pulled up, the loading of that ComboBox will throw an error. So, that's an error trapping situation, with a warning to the user, whereby they can not load that encounter, or possibly change the therapist for the encounter.

    Now, just to be complete, over in the "therapist list maintenance area", I have an option for "inactivating" a therapist (without deleting them). And, I also have a custom ComboBox whereby I can "gray out" certain items in the list, with all inactivated therapists grayed out. The user can still delete therapists, but they're cautioned to not do this for precisely this reason.
    Last edited by Elroy; Sep 19th, 2020 at 09:38 AM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    21,566

    Re: Problem with combo box with its Style property set to "0 - Dropdown combo"

    Sounds like a job for database normalization and a DataCombo. Or two if it really makes sense to let some users add crap to the lookup table (shudder).

    Silly example, using a pair of fabricated Recordsets instead of a database:

    Name:  sshot.png
Views: 57
Size:  3.9 KB

    Two rows added, now we'll add a third using one of the new months we've defined
    Attached Files Attached Files
    Last edited by dilettante; Sep 19th, 2020 at 06:29 PM.

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    6,509

    Re: Problem with combo box with its Style property set to "0 - Dropdown combo"

    Quote Originally Posted by dilettante View Post
    Sounds like a job for database normalization and a DataCombo. Or two if it really makes sense to let some users add crap to the lookup table (shudder).
    Dil, is that addressed to me or Ilia? I'm not quite sure.

    If it's addressed to me, you might be confounding two separate concepts I discussed above:
    1. An option (and a custom UC) that allows users to decide if they want "0 - Dropdown combo" or "2 - Dropdown List" at runtime.
    2. The ability of users to maintain what's in the list of a "2 - Dropdown List" at runtime, and the potential problems that a changing list (with database storage) can cause.

    And personally, I'm quite happy with the way I've addressed both of those issues. Just as an FYI, I don't bind anything. Early on, I learned to detest binding, and the corresponding limitations it brings.

    I suppose I'm willing to listen to suggestions on how to improve the way I've done things, but that's probably another thread, as I don't want to hijack Ilia's thread in ways in which he's not interested.

    Take Care,
    Elroy
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  10. #10
    PowerPoster
    Join Date
    Feb 2006
    Posts
    21,566

    Re: Problem with combo box with its Style property set to "0 - Dropdown combo"

    Quote Originally Posted by IliaPreston View Post
    I have a combo box with its Style property set to "0 - Dropdown combo".
    Storing its value to the database poses a strange problem.
    Quote Originally Posted by IliaPreston View Post
    And that issue is: What to store in and retrieve from the database.

    When the Combobox's Style is set to "2 - Dropdown List", this issue is completely simple and trivial:
    We store cbo1.ListIndex into the database and when the next time we load the screen, we read the value from the database and assign that value to cbo1.ListIndex
    In other words, it is the cbo1.ListIndex that is stored in and retrieved from the database.

    But when the Combobox's Style is set to "0 - Dropdown Combo", this issue is not clear to me.

    Is it the combobox's Text property (only the Text property) that I should store in and retrieve from the database?
    Or should I store and retrieve both the Text and the ListIndex properties?
    A bit confusing.

    How do you save and retrieve this style of combobox in the database?
    I think I answered these questions in a straightforward manner, and mostly via sample code.


    I even simplified the code. Switching formats takes just:

    Code:
    Private Sub chkNewMonths_Click()
        If chkNewMonths.Value = vbChecked Then
            DataCombo1(0).Visible = False
            DataCombo1(1).Visible = True
        Else
            DataCombo1(1).Visible = False
            DataCombo1(0).Visible = True
        End If
    End Sub
    No need for a UserControl to wrap the pair of controls.
    Last edited by dilettante; Sep 19th, 2020 at 06:16 PM.

  11. #11
    PowerPoster
    Join Date
    Feb 2006
    Posts
    21,566

    Re: Problem with combo box with its Style property set to "0 - Dropdown combo"

    Since I seem to be making heads explode here's another version using an actual MDB instead of simulating two database tables.

    There is even less procedural logic used, and a lot of that is just UI management.

    It's Visual Basic. Not QBasic, or VBScript, or even the VBA subset found in MS Access.


    But I know there is rampant superstition around data binding here. We can probably even find a few who don't trust multiplication and write loops of addition instead.

    Name:  Fear Multiplication.png
Views: 46
Size:  3.9 KB

    But seriously, write it however you want. I was just pointing out the power tool VB6 provides for the problem described by the OP. If you don't want to use it that's fine.
    Attached Files Attached Files

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