'findstring' in listbox

G

Guest

Hello.
Can someone tell me what I may be doing wrong here?

I'm using the code (lboxRP is a listbox):

Dim newRPindex As Integer
newRPindex = Me.lboxRP.FindString(RP)
Me.lboxRP.SetSelected(newRPindex, True)

When the last line executes, I get an error message:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred
in system.windows.forms.dll

Additional information: Specified argument was out of the range of valid
values.

If I 'mouse-over' my code, the value RP is the string it should be, and it
does exist as an item in the listbox...

Help!!

TIA
Amber
 
G

Guest

Put a break on the line:
Me.lboxRP.SetSelected(newRPindex, True)
Now what's the value of newRPindex when you mouseover it?

It will need to be 0 or greater to use the SetSelected method.
 
G

Guest

The value is -1.
That's the problem - I'm trying to set it to equal the index that the
findstring returns.
TIA
Amber
 
G

Guest

Try setting the initial search location to -1,
newRPindex = Me.lboxRP.FindString(RP, -1)

You've checked that there's RP is not prefixed or suffixed with spaces
(chr$(32))?
 
G

Guest

Okay, I tried that - still doesn't work.
It seems so straightforward...I'm confused as to why it won't work.

when I use the line :

dim newRPindex as integer = me.lboxRP.FindString(RP, -1)

the value 'RP' comes from the listbox...
All I'm trying to accomplish is this:
I have a listbox with multiple items. When the user makes changes to the
form, and saves, it updates everything, and repopulates the listbox.
I'm trying to save the current item that is selected, then reselect it after
the update.

Any other suggestions??
TIA.
Amber
 
G

Guest

You've narrowed the problem down to the FindString function not finding the
string representation of the object RP. How are you dimensioning and
assigning RP?

Try this to see if you can return a value:
dim newRPindex as integer = me.lboxRP.FindString("Your List Item Text")
 
G

Guest

Thanks for all you help!
It's still not working for me.
I'll post my complete code.

The sub that uses findString is:

Private Sub refreshRoadPermit(Optional ByVal deleteRecord As Boolean =
False, Optional ByVal RP As String = "")
If deleteRecord = True Then
 
G

Guest

Close... but not the exact code I was looking for.

How do you pass the RP string to the refreshRoadPermit function?

Post the calling line if you can
 
G

Guest

I'm not sure what happened there...I was typing away, looked up, and it was
gone. Hopefully I didn't accidentally send an incomplete message...

Okay, here is the code in my sub that uses the FindString method:

Private Sub refreshRoadPermit(Optional ByVal deleteRecord as Boolean =
False, Optional ByVal RP As String = "")

RoadPermitIsDirty = False
If deleteRecord = True Then
'refresh and select 1st Road Permit
getRoadPermits()
Else
getRoadPermits()
'reselect previously selected Road Permit
Dim newRPindex as Integer
newRpindex = me.lboxRP.FindString(RP, -1)
Me.lboxRP.SetSelected(newRPindex, True)
End If
End Sub

I assign RP it's value in another sub

public Sub UpdateRoadPermit()
dim drv as DataRowView = Me.lboxRP.SelectedItem
dim cRP as New RoadPermit
...lots of other code
cRP.RoadPermitName = Me.txtRP.Text
refreshRoadPermit(False,cRP.RoadPermitName)
End Sub

Now I can see that RoadPermitName is correct - and there doesn't appear to
be any spaces where there shouldn't be.
I'm baffled.

After reading this lengthy post...any new suggestions?
:)
TIA
Amber
 
G

Guest

I would venture a guess that line 5 is not assigning a value and you are
potentially passing a String.Empty. Unless you are reassigning the value of
Me.lboxRP.SelectedItem.Text to Me.txtRP.Text, you are simply passing the
contents of what could be an empty textbox to your UpdateRoadPermit sub.

Put a break at line 6. Once the code breaks, mouseover cRP.RoadPermitName
and see what the value is.

1. > public Sub UpdateRoadPermit()
2. > dim drv as DataRowView = Me.lboxRP.SelectedItem
3. > dim cRP as New RoadPermit
4. > ...lots of other code
5. > cRP.RoadPermitName = Me.txtRP.Text
6. > refreshRoadPermit(False,cRP.RoadPermitName)
7. > End Sub

Here's some sample code to test the ListBox functionality
Dim i As Integer
ListBox1.Items.Add("String 1")
ListBox1.Items.Add("String 2")
ListBox1.Items.Add("String 3")
i = ListBox1.FindString("String 2")
ListBox1.SetSelected(i, True)
 
G

Guest

Hi Mike,
If I do what you suggested, I can see that "RP" contains the string as it
should.
I tried your sample code, adding the strings, and when I use
FindString("String 2") it works fine.
I can't figure out what to do next...as far as I can see, RP is the correct
string, so does this mean my listbox doesn't contain the values it appears to?
WHen I look at them, they look correct...
Any more suggestions? or have I exhausted you :)
Amber
 
G

Guest

Ah, that's what makes it fun. If it was all easy it would get boring pretty
quickly.

Let's enumerate the values in the ListBox to see why RP isn't matching any
of them. In the code below, the asterisks will help delineate any spaces that
are in the data.

In the refreshRoadPermit Sub, right before the line:
newRpindex = me.lboxRP.FindString(RP, -1)

add this code:

Dim s As String
For Each s In lboxRP.Items
Debug.WriteLine("ListBox *" & s & "*")
Next
Debug.WriteLine("RP Value *" & RP & "*")

Then check the debug window and post the results if you can.
 
G

Guest

Ok...when I added your code, when I get to the line:

For Each s In lboxRP.Items

I get the following error:

An unhandled exception of type 'System.InvalidCastException' occurred in
microsoft.visualbasic.dll

Additional information: Cast from type 'DataRowView' to type 'String' is not
valid.

Is this a clue? :)
Amber
 
G

Guest

OOohhhh...but if I add .toString to that line, I get this in my debug window:

ListBox *S*
ListBox *y*
ListBox *s*
ListBox *t*
ListBox *e*
ListBox *m*
ListBox *.*
ListBox *W*
ListBox *i*
ListBox *n*
ListBox *d*
ListBox *o*
ListBox *w*
ListBox *s*
ListBox *.*
ListBox *F*
ListBox *o*
ListBox *r*
ListBox *m*
ListBox *s*
ListBox *.*
ListBox *L*
ListBox *i*
ListBox *s*
ListBox *t*
ListBox *B*
ListBox *o*
ListBox *x*
ListBox *+*
ListBox *O*
ListBox *b*
ListBox *j*
ListBox *e*
ListBox *c*
ListBox *t*
ListBox *C*
ListBox *o*
ListBox *l*
ListBox *l*
ListBox *e*
ListBox *c*
ListBox *t*
ListBox *i*
ListBox *o*
ListBox *n*
RP Value *R07272*
 
G

Guest

I think we're missing a few pieces to the puzzle here but we'll get it to work.
The pieces that are missing are probably in the UpdateRoadPermit Sub under
the "...lots of other code" section.

In the UpdateRoadPermit Sub, note the line:
cRP.RoadPermitName = Me.txtRP.Text

Me.txtRP.Text needs to be assigned correctly in the
....lots of other code" section. Your code is assigning the string
representation of a "DataRowView" to the Me.txtRP control.

Whereever you have the code that probably looks like this:
me.txtRP.Text = drv
you may want to try changing to this:
me.txtRP.Text = drv.toString()
or better yet, change this line
dim drv as DataRowView = Me.lboxRP.SelectedItem
to
dim sMyString as String = Me.lboxRP.SelectedItem.ToString

If you use the ListBox1.Items.Add method, the ListBox.SelectedItem object
will be a string. Since you are using a bound control the
ListBox.SelectedItem object is a type of DataRowView. You just need to
extract the required string out of the DataRowView object.

Hopefully this makes sense.
 
G

Guest

Thanks Mike!

Okay, here's how things are populated:

First I fill my textbox, in sub populateRoadPermitData() with the following
code:

Dim drv As DataRowView
drv = lboxRP.SelectedItem

Me.txtRP.Text = drv("STR_ROAD_PERMIT").ToString

Then in my UpdateRoadPermit() sub, I use the following code:
cRP.RoadPermitName = Me.txtRP.Text

to assign my road permit name, then pass it to refreshRoadPermit sub:

refreshRoadPermit(False, cRP.RoadPermitName)

This is how it should be isn't it??
Cheers,
Amber
 
G

Guest

This line:
Me.txtRP.Text = drv("STR_ROAD_PERMIT").ToString
is putting the string representation of the System.Windows.Forms.Listbox
Object Collection into the textbox. Figure out how to put the actual string
you want in there and you've got it.

You're going to have to put some breakpoints in your code and find out where
your code is breaking down. If this line:
drv("STR_ROAD_PERMIT").ToString
is putting the value:
"System.Windows.Forms.Listbox Object Collection"
into your text box then you need to keep backing up in your code until you
can extract the value you need. Try
Me.txtRP.Text = lboxRP.SelectedItem.Text instead of going through the
creation of a DataRowView. Also, if you can get away with it, assign the
property value directly to the RoadPermitName property from the Listbox like
this:
cRP.RoadPermitName = lboxRP.SelectedItem.Text
then assign the txtRP.Text value from cRP.RoadPermitName. It may make it
easier to troubleshoot in the future.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top