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!

BindingList bound to a listbox, ResetItem problem.

Status
Not open for further replies.

robUK2

Programmer
Mar 10, 2008
57
TH
Hello,

VS 2008

I am using a bindingList that is bound to a listbox.

Howerver, everytime I modify an item in the bindingList and call the resetItem to update the list box. It fires the selectedIndexChange property. This then goes into an ever ending loop.

I have been looping through with the debugger, but can't understand why it loops.

I have tried to set the RaisedListChangedEvent to false. However, that doesn't allow the listbox to be updated. I have tried the phoneLineItem.ResetItem(currentLine)in other parts of the code. And it doesn't call the selectedIndexChanged event. This only happens in the SetRingingState sub.

I think it might difficult to predict what is happening with this code, as you will have to see it running. However, any pointers on how the right direction could lead to me solving this problem. I can't think of any reason why this should happen.

Many thanks for any suggestions,

Binding the list to the list box.
Code:
Private phoneLineItem As New BindingList(Of PhoneLine)()
Private Sub PopulatePhoneLines()
    For i As Integer = 0 To 6
        'Create new object for each line - initalize each line
        line(i) = New PhoneLine(i, String.Empty, "Free", False, Me)
        'Add object to the bindingList
        phoneLineItem.Add(line(i))
    Next
    
    'Databind the list box with the BindingList
    'Ensure that the datasource has been set before selecting a line from the list box.
    Me.isDataSourceSet = False
    Me.lstPhoneLines.DataSource = phoneLineItem
    Me.lstPhoneLines.ValueMember = "CurrentLineStatus"
    Me.lstPhoneLines.DisplayMember = "CurrentLineStatus"
    
    'Set line 0 as the initial selection on startup
    Me.lstPhoneLines.SelectedIndex = 0
    Me.isDataSourceSet = True
End Sub

Selection Index changed event. This gets called everytime the phoneLineItem.ResetItem(currentLine) is called. That starts the loop of. ProcessCurrentState is a function in another class that calls the SetRingingState().
Code:
'Change the button setting based on the line object current state.
Private Sub lstPhoneLines_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    ' MessageBox.Show(sender.GetType().ToString());
    Debug.WriteLine("Selected Index Changed Event Fired")
    'phoneLineItem.RaiseListChangedEvents = false;
    If isDataSourceSet Then
        currentLine = Me.lstPhoneLines.SelectedIndex
        If currentLine > -1 Then
		//Calls function in another class that calls: SetRingingState()
            Me.phoneLineItem(currentLine).fsm().processCurrentState()         
	  End If
    End If
End Sub

//The SetRingingState will go back to the selectedIndexChanged Event when the phoneLineItem.ResetItem(currentLine) is called. So it never leaves this sub.
Code:
Public Sub SetRingingState()
    Me.btnCallAnswer.Text = "Answer"
    Me.btnEndCallReject.Text = "Reject"
    
    Me.btnCallAnswer.Enabled = True
    Me.btnEndCallReject.Enabled = True
    Me.btnRedial.Enabled = False
    Me.btnHoldUnhold.Enabled = False
    
    phoneLineItem(currentLine).LineStatus = "Ringing"
    phoneLineItem(currentLine).Holding = False
    
    phoneLineItem.ResetItem(currentLine)
End Sub
 
Use an exteral flag for whether or not to run the selection changed code, When you load the listbox flip it to true, and when you run the selection changed code the first time, flip it back to false.

If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this [red]"there was an error crap"[/red]
 
Hello,

I have tried setting a flag called bRun. However, as the program never comes out of the ProcessCurrentState() it will never reach the bRun = not bRun.

Many thanks for any suggestions.

Code:
Private Sub lstPhoneLines_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    ' MessageBox.Show(sender.GetType().ToString());
    static bRun as boolean = true
    Debug.WriteLine("Selected Index Changed Event Fired")
    'phoneLineItem.RaiseListChangedEvents = false;
    If isDataSourceSet Then
        currentLine = Me.lstPhoneLines.SelectedIndex
        If currentLine > -1 Then
//Calls function in another class that calls: SetRingingState()
             if bRun then
                     Me.phoneLineItem(currentLine).fsm().processCurrentState()
            end if
            bRun = not bRun       
        End If
    End If
End Sub

Here is the code for processCurrentState. It is in a class called fsm.

Code:
'Process different states that the object can be in, based on the event that have been fired.
		Public Function process(ByVal currentEvent As [Event]) As Boolean
			Select Case currentEvent
				Case Event.INCOMING_CALL_EVENT
					If _currentState = State.IDLE_STATE Then
						' do something when you leave IDLE_STATE
						' do something during transtion
						Me._currentState = State.RINGING_STATE
						Me._frmCATDialer.SetRingingState()

						Return True
					End If
					'Report error here.

					'TODO: If the user is making a call, the current state would be idle. Another event could be
					'added called connecting that would also set the state to busy once accepted.
				Case Event.ACCEPT_CALL_EVENT
					If _currentState = State.RINGING_STATE OrElse _currentState = State.IDLE_STATE Then
						Me._currentState = State.BUSY_STATE
						Me._frmCATDialer.SetBusyState()
						Return True
					End If
					'Report error here.

				Case Event.REJECT_CALL_EVENT
					If _currentState = State.RINGING_STATE Then
						Me._currentState = State.DISCONNECTED_STATE
						Me._frmCATDialer.SetIdleState()
						Return True
					End If
					'Report error here.

				Case Event.HOLD_CALL_EVENT
					If _currentState = State.BUSY_STATE Then
						Me._currentState = State.HOLDING_STATE

						'Change state of button to hold
						Me._frmCATDialer.SetHoldingState()

						'btn.Text = "UnHold";
						Return True
					End If
					'Report error here.

				Case Event.RELEASE_HOLD_EVENT
					If _currentState = State.HOLDING_STATE Then
						Me._currentState = State.BUSY_STATE

						'Change state of button to unhold
						Me._frmCATDialer.SetReleaseHoldState()

						Return True
					End If
					'Report error here.

				Case Event.DISCONNECTED_EVENT
					If _currentState = State.BUSY_STATE OrElse _currentState = State.HOLDING_STATE OrElse _currentState = State.RINGING_STATE Then
						Me._currentState = State.IDLE_STATE
						Me._frmCATDialer.SetIdleState()
						Return True
					End If
					'Report error here.

				Case Else

			End Select
			Return False
		End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top