By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 1,912 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

'findstring' in listbox

P: n/a
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
Jul 21 '05 #1
Share this Question
Share on Google+
17 Replies


P: n/a
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.

"amber" wrote:
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

Jul 21 '05 #2

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

Jul 21 '05 #3

P: n/a
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))?

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

Jul 21 '05 #4

P: n/a
Also, you can use
newRPindex = Me.lboxRP.FindStringExact(RP)
"amber" wrote:
The value is -1.
That's the problem - I'm trying to set it to equal the index that the
findstring returns.
TIA
Amber

Jul 21 '05 #5

P: n/a
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
"Mike S." wrote:
Also, you can use
newRPindex = Me.lboxRP.FindStringExact(RP)
"amber" wrote:
The value is -1.
That's the problem - I'm trying to set it to equal the index that the
findstring returns.
TIA
Amber

Jul 22 '05 #6

P: n/a
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")

"amber" wrote:
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
"Mike S." wrote:
Also, you can use
newRPindex = Me.lboxRP.FindStringExact(RP)
"amber" wrote:
The value is -1.
That's the problem - I'm trying to set it to equal the index that the
findstring returns.
TIA
Amber

Jul 22 '05 #7

P: n/a
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
"Mike S." wrote:
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")

"amber" wrote:
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
"Mike S." wrote:
Also, you can use
newRPindex = Me.lboxRP.FindStringExact(RP)
"amber" wrote:

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

Jul 22 '05 #8

P: n/a
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

"amber" wrote:
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


Jul 22 '05 #9

P: n/a
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

Jul 22 '05 #10

P: n/a
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)

Jul 22 '05 #11

P: n/a
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

Jul 22 '05 #12

P: n/a
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.

"amber" wrote:
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

Jul 22 '05 #13

P: n/a
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
"Mike S." wrote:
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.

"amber" wrote:
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

Jul 22 '05 #14

P: n/a
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*

"Mike S." wrote:
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.

"amber" wrote:
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

Jul 22 '05 #15

P: n/a
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.

"amber" wrote:
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
"Mike S." wrote:
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.

"amber" wrote:
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

Jul 22 '05 #16

P: n/a
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

Jul 22 '05 #17

P: n/a
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.
"amber" wrote:
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

Jul 22 '05 #18

This discussion thread is closed

Replies have been disabled for this discussion.