simondopickup
asked on
How to rename items within a combobox
Experts,
I have a text box and a combobox. When i type a 'fault' into the textbox - the 'fault' is added to a combobox. If i want to delete a 'fault' then i can select the item from the combobox - hit delete and then the item is removed from the combobox and my worksheet data store.
However i want to add some more functionality to this part of my userform. I want the user to be able to select from the current list of faults and then rename it. Preferably using a combobox to select the fault again (as in the delete fault function) - however i am not sure how i would structure the userform such that the user could change the name of the fault selected.....
Could someone offer some tips - perhaps some code to bash at?
Thanks!!
Simon
I have a text box and a combobox. When i type a 'fault' into the textbox - the 'fault' is added to a combobox. If i want to delete a 'fault' then i can select the item from the combobox - hit delete and then the item is removed from the combobox and my worksheet data store.
However i want to add some more functionality to this part of my userform. I want the user to be able to select from the current list of faults and then rename it. Preferably using a combobox to select the fault again (as in the delete fault function) - however i am not sure how i would structure the userform such that the user could change the name of the fault selected.....
Could someone offer some tips - perhaps some code to bash at?
Thanks!!
Simon
Here's another way to be able to perform this. You can just select your item in the combobox,
then right-click on your combobox, select "Rename", then enter the new value:
'Put this in the Enter event of your ComboBox:
Private Sub ComboBox1_Enter(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ComboBox1.Enter
Dim item As New MenuItem("Rename")
AddHandler item.Click, AddressOf UpdateItem
Dim cmnu As New ContextMenu(New MenuItem() {item})
Me.ComboBox1.ContextMenu = cmnu
End Sub
'This sub performs the update:
Public Sub UpdateItem(ByVal sender As Object, ByVal e As EventArgs)
Dim update As String = _
InputBox("Please enter a new name", "Change name")
Me.ComboBox1.Items.Remove( Me.ComboBo x1.Selecte dItem)
Me.ComboBox1.Items.Add(upd ate)
Me.ComboBox1.SelectedItem = update
End Sub
then right-click on your combobox, select "Rename", then enter the new value:
'Put this in the Enter event of your ComboBox:
Private Sub ComboBox1_Enter(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ComboBox1.Enter
Dim item As New MenuItem("Rename")
AddHandler item.Click, AddressOf UpdateItem
Dim cmnu As New ContextMenu(New MenuItem() {item})
Me.ComboBox1.ContextMenu = cmnu
End Sub
'This sub performs the update:
Public Sub UpdateItem(ByVal sender As Object, ByVal e As EventArgs)
Dim update As String = _
InputBox("Please enter a new name", "Change name")
Me.ComboBox1.Items.Remove(
Me.ComboBox1.Items.Add(upd
Me.ComboBox1.SelectedItem = update
End Sub
You'll notice in the UpdateItem sub above, it's removing the old item, and adding the renamed item.
If you actually want to just change the name of the selected item, you can use this:
Public Sub UpdateItem(ByVal sender As Object, ByVal e As EventArgs)
Dim update As String = _
InputBox("Please enter a new name", "Change name")
Me.ComboBox1.Items().Item( Me.ComboBo x1.Items.I ndexOf( _
Me.ComboBox1.SelectedItem) ) = update
End Sub
If you actually want to just change the name of the selected item, you can use this:
Public Sub UpdateItem(ByVal sender As Object, ByVal e As EventArgs)
Dim update As String = _
InputBox("Please enter a new name", "Change name")
Me.ComboBox1.Items().Item(
Me.ComboBox1.SelectedItem)
End Sub
ASKER
hmmm i am just looking at both solutions - right now. bare with me..
ASKER
jeverist,
i am getting an error caused by the line:
ComboBox1.List = Application.Transpose(Rang e("FAULT_R ANGE"))
when i initialize my code. The following error is shown:
ERROR - could not set property - invalid property array index.
Any ideas why it would be doing this?
i am getting an error caused by the line:
ComboBox1.List = Application.Transpose(Rang
when i initialize my code. The following error is shown:
ERROR - could not set property - invalid property array index.
Any ideas why it would be doing this?
ASKER
I am working on VB thats behind excel - what version is that? VBRocks i have tried to implement your solution but i'm getting syntax problems before i even start...I we on the same page?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks jeverist - its working now
Assuming that you want to rename the ComboBox selection using the TextBox and initiate the process with a ComandButton, here are some routines for your userform that use the ComboBox1, TextBox1 and CommandButton2 controls to rename the selected fault from the FAULT_RANGE range:
Private Sub UserForm_Initialize()
ComboBox1.List = Application.Transpose(Rang
End Sub
Private Sub ComboBox1_Change()
TextBox1.Value = ComboBox1.Value
End Sub
Private Sub CommandButton2_Click()
RenameFault
End Sub
Sub RenameFault()
With ComboBox1
.List(.ListIndex) = TextBox1.Value
Range("FAULT_RANGE").Cells
.Value = ""
End With
TextBox1.Value = ""
End Sub
Jim