Error due dynamically change RadComboBox items

2 posts, 0 answers
  1. Peter Bogoyavlensky
    Peter Bogoyavlensky avatar
    20 posts
    Member since:
    Feb 2009

    Posted 18 Nov 2009 Link to this post

    I try to change RadComboBoxItems collection through casting SelectedIndexChanged event (for localization reasons). And it works fine in version 2009.2.729. But in current version 2009.3.1103 it produced exception ("Object reference not set to an instance of an object") if I change item in combobox. If I press button - all works fine.

    What's wrong with that code?

    Public Class RadForm1 
        Private m_cmbCurLanguage As Boolean = False 
        Private Sub RadForm1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load 
            m_cmbCurLanguage = True 
        End Sub 
        Private Sub AssignCurLanguage(ByVal lng As Integer
            Dim lng1 As String = "", lng2 As String = "", lng3 As String = "" 
            Select Case lng 
                Case 0 
                    lng1 = "Eng1" : lng2 = "Ger1" : lng3 = "Fra1" 
                Case 1 
                    lng1 = "Eng2" : lng2 = "Ger2" : lng3 = "Fra2" 
                Case 2 
                    lng1 = "Eng3" : lng2 = "Ger3" : lng3 = "Fra3" 
            End Select 
            m_cmbCurLanguage = False 
            cmbCurLanguage.Items.Add(New Telerik.WinControls.UI.RadComboBoxItem(lng1, 0)) 
            cmbCurLanguage.Items.Add(New Telerik.WinControls.UI.RadComboBoxItem(lng2, 1)) 
            cmbCurLanguage.Items.Add(New Telerik.WinControls.UI.RadComboBoxItem(lng3, 2)) 
            cmbCurLanguage.SelectedIndex = lng 
        End Sub 
        Private Sub cmbCurLanguage_SelectedIndexChanged(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles cmbCurLanguage.SelectedIndexChanged 
            If m_cmbCurLanguage Then 
                m_cmbCurLanguage = True 
            End If 
        End Sub 
        Private Sub RadButton1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles RadButton1.Click 
            m_cmbCurLanguage = True 
        End Sub 
    End Class 
  2. Victor
    Victor avatar
    1349 posts

    Posted 18 Nov 2009 Link to this post

    Hi Peter Bogoyavlensky,

    Thank you for writing.
    Your code worked before because the selection events were not working correctly until Q3 2009. You are changing the collection in the SelectedIndexChanged event handler which could potentially cause more SelectedIndexChanged events. Doing this is undefined and you may experience other issues beside this one, for example a StackOverflowException.
    You should not clear the collection and re-add the items. In your case simply changing the Text property of the existing RadComboBoxItems should suffice.
    Please write again if you need further assistance.

    Sincerely yours,
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. DevCraft R3 2016 release webinar banner
Back to Top