Strange Problem with selectedIndexChanged

FuZion

Well-known member
Joined
Jan 28, 2007
Messages
47
Programming Experience
Beginner
I have a strange problem with a form I'm writing... Basically it's a list box with New Question 1, 2, 3 and 4, a points text box, and an add button.

This is the code I run when the add button is clicked:
VB.NET:
 Private Sub btnAdditionalQuestionAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdditionalQuestionAdd.Click
        Dim prevQuestion As String = listAdditionalQuestions.SelectedItem.ToString
        Dim index As Integer = listAdditionalQuestions.SelectedIndex
 
        If listAdditionalQuestions.SelectedItem.ToString.StartsWith("New") Then
            Dim x As New clsAnswer(listOtsQuestions.SelectedValue, txtAdditionalPoints.Text, "v", txtAdditionalQuestion.Text)
            x.backgroundAddQ.RunWorkerAsync()
            answers.Add(x)
 
            listAdditionalQuestions.Items.Add(txtAdditionalQuestion.Text)
            listAdditionalQuestions.SelectedItem = txtAdditionalQuestion.Text
            listAdditionalQuestions.Items.Remove(prevQuestion)
        Else
            Dim ans As clsAnswer = answers.ItemByText(listAdditionalQuestions.SelectedItem.ToString)
            ans.txt = txtAdditionalQuestion.Text
            ans.pts = txtAdditionalPoints.Text
            ans.backgroundUpdateQ.RunWorkerAsync()
 
            listAdditionalQuestions.Items.Add(txtAdditionalQuestion.Text)
            listAdditionalQuestions.SelectedItem = txtAdditionalQuestion.Text
            listAdditionalQuestions.Items.Remove(prevQuestion)
 
        End If
    End Sub

Now, if there is already a question in there (ie the user has changed it from new question 1 to an actual question) and the user changes just the question text, the update works fine. However, if the user changes the text and the points when the listAdditionalQuestions.remove function is called it removes all of the items, not just one. However, this works fine when the points are not changed.

This is the selectedIndexChanged function that I use to update the question text box and the points text box so the user can make corrections as needed.

VB.NET:
 Private Sub listAdditionalQuestions_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles listAdditionalQuestions.SelectedIndexChanged
        If listAdditionalQuestions.Items.Count > 0 Then
 
            If Not listAdditionalQuestions.SelectedItem.ToString.StartsWith("New") Then
                Dim ans As clsAnswer = answers.ItemByText(listAdditionalQuestions.SelectedItem.ToString)
 
                txtAdditionalQuestion.Text = ans.txt
                txtAdditionalPoints.Text = ans.pts
            Else
                txtAdditionalQuestion.Text = ""
                txtAdditionalPoints.Text = ""
            End If
        End If
    End Sub

If this is confusing please let me know I'm not sure how well I explained it, if anyone knows what's going on please let me know!

Thanks in advance,

FuZion
 
Back
Top