Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listbox Items Iteration Dilemmaaa!!!

Status
Not open for further replies.

Balarao

MIS
Jun 9, 2003
25
PH
Hello All,

I'm new in VB.NET and can't seem to figure out how listbox iteration works.

Here's the code:

1 Private Sub ResetPlacement()
2 Dim strSelectedSubject As String
3 Dim dr As DataRow
4 Dim i As Int32
5
6 lbxSubjects.ClearSelected()
7
8 For i = 0 To lbxSubjects.Items.Count - 1
9 strSelectedSubject = lbxSubjects.Items(i).ToString
10
11 For Each dr In dtCurriculumSubject.Rows
12 If strSelectedSubject.Trim = _
13 dr("SubjectName").ToString.Trim Then
14 dr("Placement") = i + 1
15 Exit For
16 End If
17 Next
18 Next
19 End Sub

Everytime I iterate throught the FOR loop in line 8, I get a different 'strSelectedSubject' result in line 9. These are the results:

i Value Result Remarks
------- ------ -------
0 "Latin 1" This is correct
1 "System.Data.DataRowView" ???
2 "System.Data.DataRowView" ???

I tried using the following syntax with same result:
lbxSubjects.GetItemText(lbxSubjects.items(i))

I also tried the following but it gives an error on "late-Binding" with is not permitted when OPTION STRICT is ON:
lbxSubjects.items(i).Text
lbxSubjects.items(i).Value

Please enlighten me!!! :)

God bless,
Balarao

 
I use this function when working with list and combo boxes. it helps avoid that issue. There was a great discussion about this a week or two ago that we went into the whys and hows.



Add this:

Code:
Private Function getValue(ByVal sender As Object) As String
  If sender.GetType Is New String("").GetType Then
    Return sender
  Else
    Return CType(sender, Data.DataRowView).Item(0)
  End If
End Sub

then change this line:
Code:
strSelectedSubject = lbxSubjects.Items(i).ToString

to

Code:
strSelectedSubject = getValue(lbxSubjects.Items(i))

-Rick



----------------------
 
Thank you for your prompt response.

Three errors occured:

1 Private Function getValue(ByVal sender As Object) As String
2 If sender.GetType Is New String("").GetType Then
3 Return sender
4 Else
5 Return CType(sender, Data.DataRowView).Item(0)
6 End If
7 End Sub

line 2, String(""): Option Strict On disallows implicit conversion from "String" to "I-dimensional array of Char"

line 3, sender and line 5, CType(sender, Data.DataRowView).Item(0): Option Strict On disallows implicit conversion from "System.Object" to "String"

Does this mean that I have no choice but to turn Option Strict Off?

Is there any workaround for this?

Still hanging...

Balarao
 
change:
CType(sender, Data.DataRowView).Item(0)
to
CType(sender, Data.DataRowView).Item(0).tostring

change
If sender.GetType Is New String("").GetType Then
to
If sender.GetType Is New String("").ToString.GetType Then


-Rick

----------------------
 
Thanks Rick.

Only one error left.

the quotes "" in
If sender.GetType Is New String("").ToString.GetType Then

still generates a "late binding" error.

Balarao
 
I tried the new "cool string" but the late binding error is still there!

Option Strict On disallows implicit conversion from "String" to "I-dimensional array of Char"

I'm getting nuts over this one!

How hard is it to use a ListBox?

Desperately yours,

Balarao
 
ehh, this isn't a list box issue. this is a type and strict issue. My shop doesn't use Option Strict On (not my decision), so I can get away with kludges like
Code:
sender.GetType Is New String("").GetType

Since I haven't had to deal with the "nice" way of comparing types, I've never dug into it. so if someone knows a better way to compair types, I'd love to see it.

in the mean time try:
Code:
dim str as string
'...
If sender.GetType Is str.GetType then
'...

-Rick

----------------------
 
What if the listbox contains neither string objects or datarowviews?
 
Ya see, this is what I get for posting klude code. :p

Here is the "Improved" klude code. It still doesn't handle invading aliens, but it should avoid the strict option issues and the "What if..." that RG Mentioned ;)

Code:
Private Function getValue(ByVal sender As Object) As String
 If sender.GetType.ToString.IndexOf("DataRowView") >= 0 Then
  Return CType(sender, Data.DataRowView).Item(0).ToString
 Else
  Return sender.ToString
 End If
End Function

-Rick

----------------------
 
What if you want to get the value member instead of the display member?
 
Geeebus!

Throw this in your enums lib:
Code:
  Public Enum enmListColumn
    DisplayMember = 0
    ValueMember = 1
  End Enum

add an import to the class with the getValue and the class(es) calling getValue to the enum lib w/ the enmListColumn in it. Then use this for the getValue code:

Code:
Private Function getValue(ByVal sender As Object, ByVal Column As enmListColumn) As String
 dim ReturnVal As String
try
 If sender.GetType.ToString.IndexOf("DataRowView") >= 0 Then
  ReturnVal = CType(sender, Data.DataRowView).Item(Column).ToString
 Else
  ReturnVal = sender.ToString
 End If
catch
 ReturnVal = ""
end try
return ReturnVal
End Function

Your calling code would then look like this:
Code:
getValue(cboRegion.SelectedValue, DisplayMember)
or
Code:
getValue(cboRegion.SelectedValue, ValueMember)

There, happy? Now it can defend against alien invasions and cars driving the wrong way down a one way street.

-Rick

----------------------
 
BTW, Thanks RG, every now and then it's good to have someone poke at you saying 'Make it better' ;)

Better code makes a better app. And a better app makes a better paycheck.

-Rick

----------------------
 
Using reflection, such as this page shows:

You could make a function to return the value of a string, DataRowView, or custom class
Code:
     Public Function GetValue(ByVal sender As Object, ByVal Member As String) As Object
        Dim dr As DataRowView
        If sender.GetType Is Type.GetType("DataRowView") Then
            dr = CType(sender, DataRowView)
            Return dr.Item(Member)
        ElseIf TypeOf sender Is String Then
            Return CType(sender, String)
        Else
            Dim Mypropertyinfoa As System.Reflection.PropertyInfo = sender.GetType.GetProperty(Member)
            Return Mypropertyinfoa.GetValue(sender, Nothing)
        End If
    End Function
 
That definately goes the extra mile. I've just started messing arround with some of the more entertaining reflection functionality recently. I'm still learning as I go in that domain.

Just out of curiocity though, won't MyPropertyInfoA by nothing if Member isn't a valid property name, thus causing a null reference error on the return MyPropertyInfoA.getvalue line?

-Rick

----------------------
 
I'm sure there would be an exception, which would need to be taken care of. The way I implemented it was to pass in either .ValueMember of .DisplayMember property for the listbox object.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top