473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

'findstring' in listbox

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
17 3076
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Rene Aguirre | last post by:
Hello everybody, I had this problem using the FindString funcion of a wxChoice object on wxPython: * I have Windows 98 * I read a text file edited on a Japanese computer, but only uses...
1
by: Marcin Floryan | last post by:
Hello! I have recently come across two very nice methods in ListBox control: ..FindString and .FindStringExact it is very usefull for me, and I would like to use it in some other situations. ...
2
by: Jorge | last post by:
Hello I have a little problem i have a checkedlistbox with several repeated items. The code below only selects the first ocurrence While reader.Read For i = 0 To...
17
by: amber | last post by:
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)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.