Link to home
Start Free TrialLog in
Avatar of RIAS
RIASFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Select value from Listbox

Hello,
The listbox contains items like
REG 1NN
when I try to select or find the item in the listbox like 'REG1NN' it returns none as there is a space between G AND 1.
      .lstVehRegDailyRun.SelectedItem = Str_Car


Open in new window

Any suggestion on how can i search ignoring the space?
Thanks
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Try using:

   .lstVehRegDailyRun.SelectedItem.Replace(" ", "") = Str_Car

Open in new window

Avatar of RIAS

ASKER

Thanks Gustav Brock,
But, it didn't work. Any more suggestions?
Spec: 
The item in listbox is with space.
The item in the textbox.text is without space. 
Avatar of RIAS

ASKER

Tried this as well
  Dim index As Integer = .lstVehRegDailyRun.FindString(Str_Car)
                            ' If the item was not found in ListBox 2 display a message box, otherwise select it in ListBox2.
                            If index = -1 Then
                                ClMain.FormatMessage(21, "Please check the Car's Reg  as its not in the system. Please add the Car to the system.")
                                Exit Sub
                            Else
                                .lstVehRegDailyRun.SetSelected(index, True)
                            End If


Open in new window

We only had your single code line.

If you wish to match a string with the same string including a space, either the first must have a space inserted or the space must be removed from the last; or all spaces removed from both.
Avatar of Arana (G.P.)
Arana (G.P.)

how are you getting  Str_Car  ?
This does work:
ListBox1.SelectedItem = "REG 1NN"

can you show the contents of str_car before trying to use it in the selectedvalue(msgbox or debug print)
Avatar of RIAS

ASKER

str_car   is a string.
str_car  =  "REG1NN"

And the listbox items are
REG 1NN
REG 789

So it does exist in the listbox but, by the code ListBox1.SelectedItem  it does not return true. 
Also are you sure the "space" in "REG 1NN" is actually a space (chr(32))   , or some other non printable character?
Avatar of RIAS

ASKER

These values are coming from database.
it does not exist in the listbox, REG1NN is not the same as REG 1NN, it will never find it this way

so you can try to search only for a part of the string like
.lstVehRegDailyRun.SelectedItem = left(Str_Car,3)
it will then give youthe first ocurrence of REG ignoring the rest
Avatar of RIAS

ASKER

Could have done that but, its Car reg so finding just 1st three will not help
ASKER CERTIFIED SOLUTION
Avatar of Arana (G.P.)
Arana (G.P.)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RIAS

ASKER

Thanks Arana (G.P.) 
Trying and be right back
beware that this is not optimized for performance, if you have a lot of items in the list, this will not be fast (talking about hundreds of items)
Avatar of RIAS

ASKER

Thanks Arana (G.P.) it worked!