rjef
asked on
Delete item from combo box
How can i delete an item from the drop down list of a combo box? vb6
combobox.removeitem(index)
don't forget that the indexes start at 0. let me know if you need more help
ASKER
I would like for the user to right click on the item he wants to remove and then click delete . The i would execute the combobox.removeitem(index) . How do i do that?
Hi rjef,
You can't right click combobox and get a menu. So here is a workaround...
Let the user select an item from the combobox, then click a button named "Delete" or a menu.
Private Sub cmdDelete_Click()
On Error Resume Next
Combo1.RemoveItem Combo1.ListIndex
End Sub
Bye
---
Harish
You can't right click combobox and get a menu. So here is a workaround...
Let the user select an item from the combobox, then click a button named "Delete" or a menu.
Private Sub cmdDelete_Click()
On Error Resume Next
Combo1.RemoveItem Combo1.ListIndex
End Sub
Bye
---
Harish
ASKER
that appears to work but i really need to be able to right click on the item and delete it. Any one else have any ideas?
You can't do it. There's no right click event. Unless there are hooks or something -- I know nothing about those!
Sorry, I meant to say there's no mousedown event which is where you trap right and left clicks
rjef, Combobox doesn't have MouseDown event. So it is not possible with standard combobox. But, you can do it if you can get third party controls..
In that case, create a menu ...
mnuCombo Visible = False
...mnuComboDelete Caption = &Delete
Private Sub Combo_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
PopUpMenu mnuCombo
End Sub
Private Sub mnuComboDelete_Click()
On Error Resume Next
Combo1.RemoveItem Combo1.ListIndex
End Sub
In that case, create a menu ...
mnuCombo Visible = False
...mnuComboDelete Caption = &Delete
Private Sub Combo_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
PopUpMenu mnuCombo
End Sub
Private Sub mnuComboDelete_Click()
On Error Resume Next
Combo1.RemoveItem Combo1.ListIndex
End Sub
ASKER
what is the name of the 3rd party control?
There are many controls available. For example, Sheridan controls
Before that, try this...
Add Microsoft Forms 2.0 Object Library from Components.
Then use that combobox
Before that, try this...
Add Microsoft Forms 2.0 Object Library from Components.
Then use that combobox
ASKER
How come this doesn't work?
Private Sub ComboBox1_KeyDown(KeyCode As MSForms.ReturnInteger, Shift As Integer)
If KeyCode = 13 Then
ComboBox1.Text = UCase(ComboBox1.Text)
For X = 0 To ComboBox1.ListCount - 1
If Trim(ComboBox1.Text) = ComboBox1.List(X) Then
Exit Sub
End If
Next X
ComboBox1.AddItem ComboBox1.Text
ComboBox1.Text = ""
End If
End Sub
Private Sub ComboBox1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then
resp2 = MsgBox("Delete", vbYesNo, "Open")
If resp2 = vbNo Then
Exit Sub
End If
On Error Resume Next
MsgBox ComboBox1.List
On Error Resume Next
ComboBox1.RemoveItem ComboBox1.ListIndex
End If
End Sub
Private Sub ComboBox1_KeyDown(KeyCode As MSForms.ReturnInteger, Shift As Integer)
If KeyCode = 13 Then
ComboBox1.Text = UCase(ComboBox1.Text)
For X = 0 To ComboBox1.ListCount - 1
If Trim(ComboBox1.Text) = ComboBox1.List(X) Then
Exit Sub
End If
Next X
ComboBox1.AddItem ComboBox1.Text
ComboBox1.Text = ""
End If
End Sub
Private Sub ComboBox1_MouseDown(Button
If Button = vbRightButton Then
resp2 = MsgBox("Delete", vbYesNo, "Open")
If resp2 = vbNo Then
Exit Sub
End If
On Error Resume Next
MsgBox ComboBox1.List
On Error Resume Next
ComboBox1.RemoveItem ComboBox1.ListIndex
End If
End Sub
Is it giving error or not working ?
ASKER
it does not remove the items from the list
Which one ? mousedown or keydown ?
ASKER
mouse down
If you are just right-clicking without selecting first then listindex = -1 and your on error resume next is covering up that problem.
Just a thought ...
Just a thought ...
ASKER
when i select then the drop down box disappears
You should select an item before one can delete that.. for that change the style property
Private Sub Form_Load()
ComboBox1.Style = fmStyleDropDownList
ComboBox1.AddItem "A"
ComboBox1.AddItem "B"
ComboBox1.AddItem "C"
ComboBox1.AddItem "D"
End Sub
Private Sub ComboBox1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
On Error Resume Next
If (Button = vbRightButton) Then
ComboBox1.RemoveItem ComboBox1.ListIndex
End If
End Sub
Private Sub Form_Load()
ComboBox1.Style = fmStyleDropDownList
ComboBox1.AddItem "A"
ComboBox1.AddItem "B"
ComboBox1.AddItem "C"
ComboBox1.AddItem "D"
End Sub
Private Sub ComboBox1_MouseDown(Button
On Error Resume Next
If (Button = vbRightButton) Then
ComboBox1.RemoveItem ComboBox1.ListIndex
End If
End Sub
ASKER
so how can the user add items to the list?
As shown in the form_load event. If you want that, put a text box and an add button.
Private Sub cmdAdd_Click()
ComboBox1.AddItem Text1.Text
End Sub
Private Sub cmdAdd_Click()
ComboBox1.AddItem Text1.Text
End Sub
ASKER
so instead of having a button to delete items in the list i have to have a button to add items to the list?
Do you have any other idea ??
ASKER
i heard something about sub classing to do this. Does anybody know about that?
I think that is in .NET not in VB6
ASKER
Well, a new feature for me too :) Look at this PAQ: https://www.experts-exchange.com/questions/10092917/SubClassing.html
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.