Results 1 to 14 of 14

Thread: Clear combo box selection

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    8

    Question Clear combo box selection

    I have a form built to enter certain text into a Word document based upon values in a combo box drop-down list. My problem is that I can't figure out how to clear my selection if I inadvertently select a value I shouldn't have. For example:

    Combo box has choices of Left and Right. I select left when in fact I shouldn't have selected anything at all. Now I have no way to get rid of it. What I'd like to happen is if I press the Delete key it clears out the selection I just made. Is that possible? If so, how do I do it?

    Thanks,
    Mario64

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Clear combo box selection

    Welcome to the Forums.

    Are you using a UserForm or automating Word from VB?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    8

    Re: Clear combo box selection

    Quote Originally Posted by RobDog888
    Welcome to the Forums.

    Are you using a UserForm or automating Word from VB?
    UserForm

    Mario64

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Clear combo box selection

    Set the Style property of the combo to 0 - fmStyleDropDownCombo so you can delete the text that is typed in. Or Add
    an item that is an empty string. Then if they select "blank" it can blank out or do what ever you code it to do.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    8

    Re: Clear combo box selection

    Quote Originally Posted by RobDog888
    Set the Style property of the combo to 0 - fmStyleDropDownCombo so you can delete the text that is typed in. Or Add
    an item that is an empty string. Then if they select "blank" it can blank out or do what ever you code it to do.
    I actually chose not to use that style because I want to force a selection from the drop-down list only. So I'm using style 2.

    Is there another way to force selection from the combo box while allowing Delete to work?

    Thanks,
    Mario64

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Clear combo box selection

    Yes, you can set the .ListIndex to -1 or no selection. In your clear button click event you can do this.
    VB Code:
    1. ComboBox1.ListIndex = -1
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    8

    Re: Clear combo box selection

    Quote Originally Posted by RobDog888
    Yes, you can set the .ListIndex to -1 or no selection. In your clear button click event you can do this.
    VB Code:
    1. ComboBox1.ListIndex = -1
    Hmm, let me try that and I'll report back. Tx.

    Mario64

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    8

    Re: Clear combo box selection

    Can't seem to get it to work. I changed the style back to 0 then added the following:

    Private Sub UserForm_Initialize()
    ' In this section of our code, we will set up the values
    ' for all of the combo boxes.
    ' We need to set up the data for our combo boxes

    ' Choices for Side Indicator
    cmbSideInd.ListIndex = -1
    cmbSideInd.AddItem ("L Left")
    cmbSideInd.AddItem ("R Right")
    cmbSideInd.AddItem ("B Both")

    It still allows me to "free text" an entry into the box. What am I doing wrong?

    Mario64

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Clear combo box selection

    Keep the Style set to 0.
    Then after all items are added, then try the -1.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    8

    Re: Clear combo box selection

    Quote Originally Posted by RobDog888
    Keep the Style set to 0.
    Then after all items are added, then try the -1.
    Nope. Still didn't work unfortunately. They can type anything they want into the box and it will accept it.

    Mario64

  11. #11

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    8

    Re: Clear combo box selection

    Ok. I'm getting close. Using this code

    Private Sub cmbSideInd_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 46 Then cmbSideInd.Clear
    End Sub

    does clear the box but then if you click the drop down again the whole thing is blank. I want to clear out the user's selection NOT the drop down list itself. Any ideas on how to accomplish that?

    Thanks,
    Mario64

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Clear combo box selection

    .Clear will actually remove all list entrires from the combo box.
    In the properties window for the combo box set the Style to 2. then add this code as a test but name your obejcts as the same so the events fire.

    VB Code:
    1. Private Sub CommandButton2_Click()
    2.     ComboBox1.ListIndex = -1
    3. End Sub
    4.  
    5. Private Sub UserForm_Initialize()
    6.     ComboBox1.AddItem "Test1"
    7.     ComboBox1.AddItem "Test2"
    8. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    8

    Re: Clear combo box selection

    Yeah, that did it. Thanks!

    One last question, what's the difference between the Click event and the KeyDown event? I'm a keyboard person and won't actually be using the mouse to select from the combo boxes. Will Click still work? Should I take into account both scenarios?

    Thanks,
    Mario64

  14. #14
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Clear combo box selection

    Quote Originally Posted by mario64
    Yeah, that did it. Thanks!

    One last question, what's the difference between the Click event and the KeyDown event? I'm a keyboard person and won't actually be using the mouse to select from the combo boxes. Will Click still work? Should I take into account both scenarios?

    Thanks,
    Mario64
    Yes...in fact you should always give your users two or three different ways of doing the same thing. They like that.

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