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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

.SelectedItem = Nothing Problem

Status
Not open for further replies.

morelab

Programmer
Dec 3, 2004
6
0
0
US
When using a data_reader I have no problem using the _Leave method to set .SelectedItem = Nothing to eliminate the highlighted blue.

However, when using a data_adapter I am having a terribly difficult time getting rid of the highlight. I "fixed" the problem using:

monitorListBox.SelectedIndex = -1
monitorListBox.SelectedItem = Nothing

but then when I do other things I get an ugly reset since there is technically a _SelectedIndexChanged event. Additionally, I have also tried _LostFocus - no luck there either.

Anybody know how to rid the highlight from a listbox using a data_adapter binding?
 
I'm having the same issue -- and have close to the same workarounds...I use the .SelectedIndex = -1
and I also remove and add the listboxes when I need to workaround this -- but there must be a better way.

I need to use the dataadapter so I can get both displaymember and a valuemember. Does anyone know a way to solve this or a better way to go about this?
 
This is the part where Chiph and I grumble about binding data to GUI objects...

But for a quick fix that might work, try setting the listbox.visible = false when you switch records if the index is -1. then after the move is complete, set it back to true.

-Rick

----------------------
 
ThatRickGuy -

Good idea, but when I reset it to visible the blue selected box still shows.

Are you aware of the ability use .DisplayMember & .ValueMember without using a dataAdapter? I've played around and the IDE gives me the ability, but I cannot get it to work.

Bill
 
Hey -- I came across some code here:

I took the code and modified it for my needs. The .Displaymember is working correctly, the issue is that the valuemember is returning the text "ValueName" -- instead of the actual value I am assigning to it. Can someone take a look and see if there is anything obvious I am missing:

Code:
... 
Dim mtrLbl As String
Dim mtrID As Integer
Do While db_reader.Read
    mtrLbl = db_reader.Item(0).ToString
    mtrID = db_reader.Item(1).ToString
    MsgBox(mtrID) 'This will be the valumember and reads correctly
    arrNumber.Add(New ListAssigner(mtrID, mtrLbl))
 Loop
   objConn.Close()

 listBoxMonitors.DataSource = arrNumber
 listBoxMonitors.DisplayMember = "DisplayName"
 listBoxMonitors.ValueMember = "ValueName"
 listBoxMonitors.Refresh()

The it hits a class (ListAssigner):

Code:
Public Class ListAssigner

    Private gstrValueName As String
    Private gstrDisplayName As String

    Public Sub New(ByVal strValueName As String, ByVal strDisplayName As String)
        MyBase.New()
        Me.gstrValueName = strValueName
        Me.gstrDisplayName = strDisplayName
    End Sub

    Public ReadOnly Property ValueName() As String
        Get
            Return gstrValueName
        End Get
    End Property

    Public ReadOnly Property DisplayName() As String
        Get
            Return gstrDisplayName
        End Get
    End Property

End Class

I thought maybe it wouldn't work because my valuemember is an integer instead of the string in the example -- but that doesn't seem to be it....I'll keep working on this -- but hopefully someone with better skills can scan this and point me in the right direction.

Thanks.
 
Nope.

I got it -- but is still selected the top row by default....

The code works -- it was my messagebox to see the value that was incorrect:

use this to pull the selected value from the listbox:
mtrID = Me.listBoxMonitors.SelectedValue

Still no solution yet....


 
For the record, I finally got this (and posted the result to another thread but wanted to save the solution here for future searches:

Unfortunately, I forgot to keep the original authors info to give him/her credit....


I built a class called itemclass:

Code:
Public Class itemclass
    Public name As String
    Public id As Integer
    Public Sub New(ByVal inName As String, ByVal inid As Integer)
        name = inName
        id = inid
    End Sub
    Public Overrides Function tostring() As String
        Return name
    End Function
End Class

Then just added the class to the listbox through the datareader:
Code:
Private Sub listBoxRepFill(ByVal unit As String)
    
        listBoxRep.Items.Clear()
        objConn.Open()
        'execute the query
        Dim cmd As New OleDb.OleDbCommand("SELECT displayitem,value from table",objConn)
        Dim db_reader As OleDb.OleDbDataReader = cmd.ExecuteReader(CommandBehavior.Default)
        Do While db_reader.Read
            With listBoxRep.Items
                .Add(New itemclass(db_reader.Item(0).ToString, db_reader.Item(1).ToString))
            End With
        Loop
        db_reader.Close()
        objConn.Close()
    End Sub

Then I return the selected value (the integer associated with the string displayed value) through:
Code:
Dim mtrID
Dim selitem As itemclass = CType(listBoxMonitors.SelectedItem, itemclass)
mtrID = selitem.id
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top