This is a migrated thread and some comments may be shown as answers.

Stuck on Selected Item Bug?

11 Answers 412 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Nick Jones
Top achievements
Rank 1
Nick Jones asked on 14 Mar 2011, 03:53 PM
HI I'm using the dropdownlist control.  I've populated the control and everything works fine.  However if I select an item, and the form closes, whenever I subsequently open the form the control shows no text initially, and once the drop down arrow has been pressed you can see the current selection is stuck on the previous choice.

So On Load
  cbComp.DataSource = DataSet.Company
        cbComp.ValueMember = "id"
        cbComp.DisplayMember = "CName"

Anyone know of a work around this?

11 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 14 Mar 2011, 03:58 PM
Hello Nick,

When you open your form, you need to supply the selected value or selected index for the drop down. When this has been changed, you can save this value in a manner of your choice so you can set the value again the next time the form is opened.

I hope this helps, but if you have any questions, please let me know
Thanks
Richard
0
Nick Jones
Top achievements
Rank 1
answered on 14 Mar 2011, 04:05 PM
Hi Richard,

So after the above code I should add cbComp.SelectedIndex = 1 or cbComp.SelectedInex = Nothing
I've tried this.  I've tried setting the datasource  = nothing before rebinding to the datatable to try and clear out the old selection but nothing seems to work.

Every subsequent time I open the form the dropdownlist shows the selection I initially made and won't change.
0
Richard Slade
Top achievements
Rank 2
answered on 14 Mar 2011, 04:15 PM
Hello Nick,

This is an oversimplified exmaple. At some point you need to save the value somewhere when the value or index is changed.

E.g

Public Class Form1
  
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.RadDropDownList1.Items.Add("Item 1")
        Me.RadDropDownList1.Items.Add("Item 2")
        Me.RadDropDownList1.Items.Add("Item 3")
        Me.RadDropDownList1.Items.Add("Item 4")
  
        Me.RadDropDownList1.SelectedIndex = My.Settings.SelectedIndex ' just text held in My.Settings (initial value was -1)
    End Sub
  
    Private Sub RadDropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.Data.PositionChangedEventArgs) Handles RadDropDownList1.SelectedIndexChanged
        My.Settings.SelectedIndex = Me.RadDropDownList1.SelectedIndex
        My.Settings.Save()
    End Sub
End Class

Hope that helps
Richard
0
Nick Jones
Top achievements
Rank 1
answered on 14 Mar 2011, 04:39 PM
Hi Richard,

I appreciate your time in writing that out for me, but I think you are missing the point of my post.  Getting the selected value is not an issue.  It works the first time round.  Unfortunately the control is playing up when you try the form again.  

Private Sub MyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
        'Data table is populated......
 
        cbComp.DataSource = DataSet.Company
        cbComp.ValueMember = "id"
        cbComp.DisplayMember = "CName"
    End Sub
 
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
 
            'Save New Department
            Dim intComp As Integer
            intComp = cbComp.SelectedValue
            Main._AddDepartment(txtDName.Text, intComp)
            Me.Close()
        End If
    End Sub


Now when the form first loads, MyForm.ShowDialog() you get img1. (Fine, I'll choose the third option "Test Company")
The next time you add a department, you get img2 (Selected item 1 not there!, no problem but not as expected)
and if you hit the dropdown, 'img3' you see that item 3 (the item i selected the first time round) is already highlighted and it doesn't matter what I select the control will always pass the selected value of this item.
0
Richard Slade
Top achievements
Rank 2
answered on 14 Mar 2011, 04:51 PM
Hello,

Yes, I beleive understand your issue. I'm not sure what your Main._AddDepartment is doing, but I am unable to replicate your issue. Here is a sample project. It just assumes that you add a My.Settings setting of SelectedValue as an integer as somewhere to save the value.

Designer File
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form
  
    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub
  
    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
  
    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.RadDropDownList1 = New Telerik.WinControls.UI.RadDropDownList()
        Me.RadButton1 = New Telerik.WinControls.UI.RadButton()
        CType(Me.RadDropDownList1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.RadButton1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'RadDropDownList1
        '
        Me.RadDropDownList1.Location = New System.Drawing.Point(23, 43)
        Me.RadDropDownList1.Name = "RadDropDownList1"
        Me.RadDropDownList1.ShowImageInEditorArea = True
        Me.RadDropDownList1.Size = New System.Drawing.Size(190, 21)
        Me.RadDropDownList1.TabIndex = 0
        Me.RadDropDownList1.Text = "RadDropDownList1"
        '
        'RadButton1
        '
        Me.RadButton1.Location = New System.Drawing.Point(83, 84)
        Me.RadButton1.Name = "RadButton1"
        Me.RadButton1.Size = New System.Drawing.Size(130, 24)
        Me.RadButton1.TabIndex = 1
        Me.RadButton1.Text = "Save"
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(284, 144)
        Me.Controls.Add(Me.RadButton1)
        Me.Controls.Add(Me.RadDropDownList1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.RadDropDownList1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.RadButton1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()
  
    End Sub
    Friend WithEvents RadDropDownList1 As Telerik.WinControls.UI.RadDropDownList
    Friend WithEvents RadButton1 As Telerik.WinControls.UI.RadButton
  
End Class

Form1.vb
Public Class Form1
  
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim list As New System.Collections.Generic.List(Of Person)()
        list.Add(New Person(1, "Richard"))
        list.Add(New Person(2, "Bob"))
        list.Add(New Person(3, "Fred"))
        list.Add(New Person(4, "Peter"))
  
        Me.RadDropDownList1.DataSource = list
        Me.RadDropDownList1.DisplayMember = "Name"
        Me.RadDropDownList1.ValueMember = "Id"
        Me.RadDropDownList1.SelectedValue = My.Settings.SelectedValue
  
        Me.RadDropDownList1.SelectedValue = My.Settings.SelectedValue ' just text held in My.Settings (initial value was 1)
    End Sub
  
  
    Private Sub RadButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton1.Click
        My.Settings.SelectedValue = CInt(Me.RadDropDownList1.SelectedValue)
        MessageBox.Show("Saved - stop debugging and restart to see changes saved")
    End Sub
End Class
  
Public Class Person
    Public Property Id As Integer
    Public Property Name As String
  
    Public Sub New(ByVal id As Integer, ByVal name As String)
        Me.Name = name
        Me.Id = id
    End Sub
End Class

hope this helps. If you are still having issues, please could you provide a full sample that replicates your issue and I'll be happy to take a look at it.
thanks
Richard
0
Richard Slade
Top achievements
Rank 2
answered on 14 Mar 2011, 04:55 PM
Nick,

One thing I haven't asked you is what version of the RadControls you are using. Please can you confirm
Thanks
Richard
0
Nick Jones
Top achievements
Rank 1
answered on 14 Mar 2011, 05:09 PM
Hi Richard,

I'm using the 2010_3_10_1215.  All that the _AddDepartment is doing is saving to the DB.  However I get the same issue if I just hit the cancel button which just calls me.close().

I've tried with a new form and get the exact same issue.  This time all I have is:
Private Sub RadButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton2.Click
    Me.Close()
 
End Sub
 
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.CompanyTableAdapter.Fill(Me.Company._Company)
    RadDropDownList1.DataSource = Company._Company
    RadDropDownList1.ValueMember = "id"
    RadDropDownList1.DisplayMember = "CName"
End Sub


I also tried this is a normal form and not a radform.  Thoughts?
0
Nick Jones
Top achievements
Rank 1
answered on 14 Mar 2011, 05:12 PM
Got it.  Ok to recreate this issue you have to use showdialog().  myForm.show() and the dropdownlist works fine.  myForm.Showdialog() and the form doesn't behave as it should.  Give it a go and see what you get.
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 14 Mar 2011, 05:21 PM
Hello Nick,

No, I'm afraid I cannot replicate this. I have added a new form as the main form, and launched the other form with the drop down list from it. I can save and close the form and show a new form again with the value populated correctly.

Main.designer.vb
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Main
    Inherits System.Windows.Forms.Form
  
    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub
  
    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
  
    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.RadButton1 = New Telerik.WinControls.UI.RadButton()
        CType(Me.RadButton1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'RadButton1
        '
        Me.RadButton1.Location = New System.Drawing.Point(42, 101)
        Me.RadButton1.Name = "RadButton1"
        Me.RadButton1.Size = New System.Drawing.Size(130, 24)
        Me.RadButton1.TabIndex = 0
        Me.RadButton1.Text = "Launch Form1"
        '
        'Main
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(284, 262)
        Me.Controls.Add(Me.RadButton1)
        Me.Name = "Main"
        Me.Text = "Main"
        CType(Me.RadButton1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
  
    End Sub
    Friend WithEvents RadButton1 As Telerik.WinControls.UI.RadButton
End Class

Main.vb
Public Class Main
  
    Private Sub RadButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton1.Click
        Dim form As New Form1()
        form.ShowDialog()
    End Sub
End Class


Form1.designer.vb
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form
  
    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub
  
    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
  
    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.RadDropDownList1 = New Telerik.WinControls.UI.RadDropDownList()
        Me.RadButton1 = New Telerik.WinControls.UI.RadButton()
        CType(Me.RadDropDownList1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.RadButton1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'RadDropDownList1
        '
        Me.RadDropDownList1.Location = New System.Drawing.Point(23, 43)
        Me.RadDropDownList1.Name = "RadDropDownList1"
        Me.RadDropDownList1.ShowImageInEditorArea = True
        Me.RadDropDownList1.Size = New System.Drawing.Size(190, 21)
        Me.RadDropDownList1.TabIndex = 0
        Me.RadDropDownList1.Text = "RadDropDownList1"
        '
        'RadButton1
        '
        Me.RadButton1.Location = New System.Drawing.Point(83, 84)
        Me.RadButton1.Name = "RadButton1"
        Me.RadButton1.Size = New System.Drawing.Size(130, 24)
        Me.RadButton1.TabIndex = 1
        Me.RadButton1.Text = "Save and Close"
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(284, 144)
        Me.Controls.Add(Me.RadButton1)
        Me.Controls.Add(Me.RadDropDownList1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.RadDropDownList1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.RadButton1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()
  
    End Sub
    Friend WithEvents RadDropDownList1 As Telerik.WinControls.UI.RadDropDownList
    Friend WithEvents RadButton1 As Telerik.WinControls.UI.RadButton
  
End Class

Form1.vb
Public Class Form1
  
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim list As New System.Collections.Generic.List(Of Person)()
        list.Add(New Person(1, "Richard"))
        list.Add(New Person(2, "Bob"))
        list.Add(New Person(3, "Fred"))
        list.Add(New Person(4, "Peter"))
  
        Me.RadDropDownList1.DataSource = list
        Me.RadDropDownList1.DisplayMember = "Name"
        Me.RadDropDownList1.ValueMember = "Id"
        Me.RadDropDownList1.SelectedValue = My.Settings.SelectedValue
  
        Me.RadDropDownList1.SelectedValue = My.Settings.SelectedValue ' just text held in My.Settings (initial value was 1)
    End Sub
  
  
    Private Sub RadButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton1.Click
        My.Settings.SelectedValue = CInt(Me.RadDropDownList1.SelectedValue)
        Me.Close()
    End Sub
End Class
  
Public Class Person
    Public Property Id As Integer
    Public Property Name As String
  
    Public Sub New(ByVal id As Integer, ByVal name As String)
        Me.Name = name
        Me.Id = id
    End Sub
End Class

If you can alter my sample to re-produce this, i'll see if I can also replicate it
Richard
0
Nick Jones
Top achievements
Rank 1
answered on 14 Mar 2011, 05:56 PM
Hi Richard,

I tried out your code and didn't have an issue.  I added the following to see if it was something to do with the datatable:
Dim dt As New DataTable
        Dim col1 As New DataColumn
        Dim col2 As New DataColumn
        col1.ColumnName = "Id"
        col2.ColumnName = "Name"
        dt.Columns.Add(col1)
        dt.Columns.Add(col2)
 
        For Each guy In list
            Dim newrow As DataRow = dt.NewRow()
            newrow.Item(0) = guy.Id
            newrow.Item(1) = guy.Name
            dt.Rows.Add(newrow)
        Next
 
        Me.RadDropDownList1.DataSource = dt
        Me.RadDropDownList1.DisplayMember = "Name"
        Me.RadDropDownList1.ValueMember = "Id"
but that didn't do it either.  The only difference I can see is that you've put,
Dim form As New Form1()

I've taken this out, but it still works, however when I have added this to my project and I no longer get the issue.  So problem solved but not sure why I can't recreate.  Maybe it has something to do with the dataadapter.  Anyway, thank you for your help, it has been appreciated.
0
Richard Slade
Top achievements
Rank 2
answered on 14 Mar 2011, 06:00 PM
Hi Nick,

I'm glad that you have your issue sorted.
If any of the answers have been helpful, then please remember to mark as answer and if you need anything further then do let me know
Thanks
Richard
Tags
DropDownList
Asked by
Nick Jones
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Nick Jones
Top achievements
Rank 1
Share this question
or