Link to home
Start Free TrialLog in
Avatar of Discofish
Discofish

asked on

VB .NET ListBox - ToString() called, but redraws with original values

This should be easy, looks like I just need to call the function to force VB to update its internal values and repaint after that.  It is repainting the form however, but it is displaying the wrong values.  All the values are correct in the ListBox object, they are just showing up wrong.  Visual Basic does call the ToString function for the object, and it does return the correct value.  Any ideas what might be wrong?

I included the code for the areas in question.  Hope it helps.

Public Class HashedItem
    Protected m_Hash As Hashtable
    Protected m_Keys() As String
    Protected m_strDelimeter As String

    Public Function GetEnum() As System.Collections.IDictionaryEnumerator
        Return m_Hash.GetEnumerator
    End Function
    Public Sub New(ByVal strDisplay As String, ByVal strDelimeter As String)
        m_Hash = New Hashtable
        m_Keys = Split(strDisplay, ",")
        m_strDelimeter = strDelimeter
    End Sub

    Public Sub AddItem(ByVal strKey As String, ByVal strValue As Object)
        m_Hash.Add(strKey, strValue)
    End Sub
    Public Sub SetItem(ByVal strKey As String, ByVal strValue As Object)
        m_Hash(strKey) = strValue
    End Sub

    Public Function GetValue(ByVal strKey As String) As Object
        Return m_Hash(strKey)
    End Function

    Public Property Delimter()
        Get
            Return m_strDelimeter
        End Get
        Set(ByVal Value)

        End Set
    End Property

    Public Property Count()
        Get
            Return m_Hash.Count
        End Get
        Set(ByVal Value)

        End Set
    End Property
    Public Function GetString(ByVal strKey As String) As String

        Dim strString As String
        strString = CStr(m_Hash(strKey))
        Return strString
    End Function

    Overrides Function ToString() As String
        Dim i As Integer
        Dim str As String

        str = m_Hash(m_Keys(i))
        For i = 1 To UBound(m_Keys)
            str = str & m_strDelimeter & m_Hash(m_Keys(i))
        Next

        'ToString = str
        Return str
    End Function
End Class

Editing the items:

 Public Sub EditHasheditem(ByRef Control As Object)
        Dim i As Integer
        Dim e As System.Collections.IDictionaryEnumerator
        Dim Label As Label
        Dim TextBox As TextBox


        m_Controls = New ControlList
        Dim HashedItem As HashedItem

        HashedItem = Control.SelectedItem


        e = HashedItem.GetEnum
        e.MoveNext()

        Dim startHeight As Int16
        startHeight = 0
        Dim strLabel, strText As String

        For i = 0 To HashedItem.Count - 1
            strText = "txt" & e.Key
            strLabel = "lbl" & e.Key

            Label = New Label
            Label.Text = e.Key
            Label.Left = 15
            Label.Width = 100
            Label.Top = startHeight
            Label.Name = strLabel

            TextBox = New TextBox
            TextBox.Text = e.Value
            TextBox.Left = 120
            TextBox.Width = 100
            TextBox.Top = startHeight
            TextBox.Name = e.Key

            Controls.Add(TextBox)
            Controls.Add(Label)
            m_Controls.AddTail(TextBox)

            startHeight = startHeight + 25
            e.MoveNext()

        Next
        cmdCancel.Top = startHeight
        cmdUpdate.Top = startHeight

        Width = TextBox.Left + TextBox.Width + 10
        Height = startHeight + 50


        m_HashedItem = HashedItem
        m_ParentControl = Control
        m_iTargetIndex = Control.SelectedIndex


    End Sub


    Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
        Dim i As Integer

        m_Controls.Start()
        m_Controls.NextNode()

        m_ParentControl.SelectedIndex = m_iTargetIndex
        For i = 0 To m_HashedItem.Count - 1
            m_ParentControl.SelectedItem.SetItem(m_Controls.Value.Name, m_Controls.Value.Text)
            m_Controls.NextNode()

        Next

        m_ParentControl.ResetText()  'trying them all
        m_ParentControl.UPdate() 'see if it works
        m_ParentControl.visible = True

        MsgBox(m_ParentControl.SelectedItem.ToString())

        '  = m_HashedItem
        Me.Close()
        Me.Dispose()

    End Sub
Avatar of NetPointer
NetPointer

did not understood yr question :( what does it mean by showing wrong? u mean order? then try for sorting property of listbox...

NetPointer
Avatar of Discofish

ASKER

I mean, I have a list of points

1,5
2,5
6,0

and I update 1,5 to be 20,300.
The ItemData object gets 20,300.  The list still says 1,5.  The ToString() method of the object I used in the list is getting called.  VB is not drawing them on the list, however.
I didn't mean ItemData...I was refering to "SelectedItem", which is what I am modifying.  But like I said, internally the listbox has the correct values after I call the code to update them.  They still contain the values from the time when I first inserted them in the list though.
Problem solved.

I deleted the item, created a new item,
then added that item in the same index position.

I look at this as a hack, as it should be able to be done the way I was doing it (manipulating the values already in memory).  Since no one was able to help, I would like my points back. Thanks.

Discofish
Avatar of Bob Learned
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

PAQ with points refunded

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheLearnedOne
EE Cleanup Volunteer
ASKER CERTIFIED SOLUTION
Avatar of SpazMODic
SpazMODic

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial