Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to rename items within a combobox

Posted on 2007-11-16
8
1,588 Views
Last Modified: 2013-12-26
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
0
Comment
Question by:simondopickup
  • 4
  • 2
  • 2
8 Comments
 
LVL 38

Expert Comment

by:jeverist
ID: 20297936
Hi Simon,

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(Range("FAULT_RANGE"))

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(.ListIndex + 1, 1) = TextBox1.Value
    .Value = ""
End With

TextBox1.Value = ""

End Sub

Jim
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 20299085
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.ComboBox1.SelectedItem)
        Me.ComboBox1.Items.Add(update)
        Me.ComboBox1.SelectedItem = update

    End Sub

0
 
LVL 27

Expert Comment

by:VBRocks
ID: 20299122
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.ComboBox1.Items.IndexOf( _
            Me.ComboBox1.SelectedItem)) = update

    End Sub

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:simondopickup
ID: 20299915
hmmm i am just looking at both solutions - right now. bare with me..
0
 

Author Comment

by:simondopickup
ID: 20304790
jeverist,

i am getting an error caused by the line:
ComboBox1.List = Application.Transpose(Range("FAULT_RANGE"))
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?
0
 

Author Comment

by:simondopickup
ID: 20304860
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?
0
 
LVL 38

Accepted Solution

by:
jeverist earned 500 total points
ID: 20305632
simondopickup,

The routines assume a one dimsional array for FAULT_RANGE.  We also need to check if anything is selected in the ComBox so we need to change the 'RenameFault()' routine to this:



Jim
Sub RenameFault() 
If ComboBox1.ListIndex < 0 Then Exit Sub 
With ComboBox1
    .List(.ListIndex) = TextBox1.Value
    Range("FAULT_RANGE").Cells(.ListIndex + 1) = TextBox1.Value
    .Value = ""
End With 
TextBox1.Value = "" 
End Sub

Open in new window

0
 

Author Closing Comment

by:simondopickup
ID: 31409527
Thanks jeverist - its working now
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question