C
clive
A person in my team had a funny experience yesterday.
He was using a ComboBox, and giving it a DataSource of a string array.
Unfortunately, in the design view, he set Sorted = true.
in essence the two conflicting lines were
comboBox1.Sorted = True
comboBox1.DataSource = new string() {"hey", "yo"}
The result of this was that it allowed the line to go forward, but it
actually set it to nothing.
If we listened to the SelectedIndexChanged, then it would trigger on
this command, and the comboBox1.Items.Count was equal to 2
So it was getting set, but then, once that line completed, the
datasource was to nothing again.
When you do this the other way around, it will throw an exception.
But this way around, it just ignores the DataSource call it seems.
Here's a small program to highlight what I'm talking about.
----
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
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.
Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
Friend WithEvents SortDataSource As System.Windows.Forms.Button
Friend WithEvents DataSourceSort As System.Windows.Forms.Button
Friend WithEvents ClearButton As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.ComboBox1 = New System.Windows.Forms.ComboBox
Me.SortDataSource = New System.Windows.Forms.Button
Me.DataSourceSort = New System.Windows.Forms.Button
Me.ClearButton = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'ComboBox1
'
Me.ComboBox1.Location = New System.Drawing.Point(32, 16)
Me.ComboBox1.Name = "ComboBox1"
Me.ComboBox1.Size = New System.Drawing.Size(121, 21)
Me.ComboBox1.TabIndex = 0
Me.ComboBox1.Text = "ComboBox1"
'
'SortDataSource
'
Me.SortDataSource.Location = New System.Drawing.Point(176, 8)
Me.SortDataSource.Name = "SortDataSource"
Me.SortDataSource.Size = New System.Drawing.Size(192, 48)
Me.SortDataSource.TabIndex = 1
Me.SortDataSource.Text = "comboBox1.Sorted = True ->
comboBox1.DataSource = new String(){""hey"", ""yo""}"
'
'DataSourceSort
'
Me.DataSourceSort.Location = New System.Drawing.Point(176, 64)
Me.DataSourceSort.Name = "DataSourceSort"
Me.DataSourceSort.Size = New System.Drawing.Size(192, 48)
Me.DataSourceSort.TabIndex = 1
Me.DataSourceSort.Text = "comboBox1.DataSource = new
String(){""hey"", ""yo""} -> comboBox1.Sorted = True"
'
'ClearButton
'
Me.ClearButton.Location = New System.Drawing.Point(384, 8)
Me.ClearButton.Name = "ClearButton"
Me.ClearButton.Size = New System.Drawing.Size(192, 48)
Me.ClearButton.TabIndex = 1
Me.ClearButton.Text = "Clear"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(24, 136)
Me.TextBox1.Multiline = True
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both
Me.TextBox1.Size = New System.Drawing.Size(584, 160)
Me.TextBox1.TabIndex = 2
Me.TextBox1.Text = ""
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(624, 309)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.SortDataSource)
Me.Controls.Add(Me.ComboBox1)
Me.Controls.Add(Me.DataSourceSort)
Me.Controls.Add(Me.ClearButton)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub SortDataSource_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles SortDataSource.Click
Try
Clear()
Me.setSorted()
Me.setDataSource()
Catch ex As Exception
Log(vbCrLf & "*** I caught the Sorted then DataSource
exception " & ex.Message & vbCrLf)
End Try
TextBox1.Text &= vbCrLf
End Sub
Private Sub DataSourceSort_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles DataSourceSort.Click
Try
Clear()
Me.setDataSource()
Me.setSorted()
Catch ex As Exception
Log(vbCrLf & "*** I caught the DataSource then Sorted
exception " & ex.Message & vbCrLf)
End Try
End Sub
Sub setSorted()
Log("Before Setting Sorted")
Me.ComboBox1.Sorted = True
Me.ComboBox1.Items.Add("yo")
Me.ComboBox1.Items.Add("hey")
Me.ComboBox1.Items.Add("goodies")
Log("After Setting Sorted")
End Sub
Sub setDataSource()
Log("Before Setting Datasource")
Me.ComboBox1.DataSource = New String() {"yo", "hey"}
Log("After Setting Datasource")
End Sub
Sub Clear()
Log("Before Clear")
Me.ComboBox1.Sorted = False
Me.ComboBox1.DataSource = Nothing
Me.ComboBox1.Items.Clear()
Log("After Clear")
End Sub
Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles ClearButton.Click
Me.Clear()
TextBox1.Text &= vbCrLf
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Log("ComboBox1_SelectedIndexChanged")
End Sub
Sub Log(ByVal fun As String)
TextBox1.Text &= "While in function " & fun & " the number of
items are " & ComboBox1.Items.Count & " currently selected index " &
ComboBox1.SelectedIndex & vbCrLf
End Sub
End Class
He was using a ComboBox, and giving it a DataSource of a string array.
Unfortunately, in the design view, he set Sorted = true.
in essence the two conflicting lines were
comboBox1.Sorted = True
comboBox1.DataSource = new string() {"hey", "yo"}
The result of this was that it allowed the line to go forward, but it
actually set it to nothing.
If we listened to the SelectedIndexChanged, then it would trigger on
this command, and the comboBox1.Items.Count was equal to 2
So it was getting set, but then, once that line completed, the
datasource was to nothing again.
When you do this the other way around, it will throw an exception.
But this way around, it just ignores the DataSource call it seems.
Here's a small program to highlight what I'm talking about.
----
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
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.
Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
Friend WithEvents SortDataSource As System.Windows.Forms.Button
Friend WithEvents DataSourceSort As System.Windows.Forms.Button
Friend WithEvents ClearButton As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.ComboBox1 = New System.Windows.Forms.ComboBox
Me.SortDataSource = New System.Windows.Forms.Button
Me.DataSourceSort = New System.Windows.Forms.Button
Me.ClearButton = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'ComboBox1
'
Me.ComboBox1.Location = New System.Drawing.Point(32, 16)
Me.ComboBox1.Name = "ComboBox1"
Me.ComboBox1.Size = New System.Drawing.Size(121, 21)
Me.ComboBox1.TabIndex = 0
Me.ComboBox1.Text = "ComboBox1"
'
'SortDataSource
'
Me.SortDataSource.Location = New System.Drawing.Point(176, 8)
Me.SortDataSource.Name = "SortDataSource"
Me.SortDataSource.Size = New System.Drawing.Size(192, 48)
Me.SortDataSource.TabIndex = 1
Me.SortDataSource.Text = "comboBox1.Sorted = True ->
comboBox1.DataSource = new String(){""hey"", ""yo""}"
'
'DataSourceSort
'
Me.DataSourceSort.Location = New System.Drawing.Point(176, 64)
Me.DataSourceSort.Name = "DataSourceSort"
Me.DataSourceSort.Size = New System.Drawing.Size(192, 48)
Me.DataSourceSort.TabIndex = 1
Me.DataSourceSort.Text = "comboBox1.DataSource = new
String(){""hey"", ""yo""} -> comboBox1.Sorted = True"
'
'ClearButton
'
Me.ClearButton.Location = New System.Drawing.Point(384, 8)
Me.ClearButton.Name = "ClearButton"
Me.ClearButton.Size = New System.Drawing.Size(192, 48)
Me.ClearButton.TabIndex = 1
Me.ClearButton.Text = "Clear"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(24, 136)
Me.TextBox1.Multiline = True
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both
Me.TextBox1.Size = New System.Drawing.Size(584, 160)
Me.TextBox1.TabIndex = 2
Me.TextBox1.Text = ""
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(624, 309)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.SortDataSource)
Me.Controls.Add(Me.ComboBox1)
Me.Controls.Add(Me.DataSourceSort)
Me.Controls.Add(Me.ClearButton)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub SortDataSource_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles SortDataSource.Click
Try
Clear()
Me.setSorted()
Me.setDataSource()
Catch ex As Exception
Log(vbCrLf & "*** I caught the Sorted then DataSource
exception " & ex.Message & vbCrLf)
End Try
TextBox1.Text &= vbCrLf
End Sub
Private Sub DataSourceSort_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles DataSourceSort.Click
Try
Clear()
Me.setDataSource()
Me.setSorted()
Catch ex As Exception
Log(vbCrLf & "*** I caught the DataSource then Sorted
exception " & ex.Message & vbCrLf)
End Try
End Sub
Sub setSorted()
Log("Before Setting Sorted")
Me.ComboBox1.Sorted = True
Me.ComboBox1.Items.Add("yo")
Me.ComboBox1.Items.Add("hey")
Me.ComboBox1.Items.Add("goodies")
Log("After Setting Sorted")
End Sub
Sub setDataSource()
Log("Before Setting Datasource")
Me.ComboBox1.DataSource = New String() {"yo", "hey"}
Log("After Setting Datasource")
End Sub
Sub Clear()
Log("Before Clear")
Me.ComboBox1.Sorted = False
Me.ComboBox1.DataSource = Nothing
Me.ComboBox1.Items.Clear()
Log("After Clear")
End Sub
Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles ClearButton.Click
Me.Clear()
TextBox1.Text &= vbCrLf
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Log("ComboBox1_SelectedIndexChanged")
End Sub
Sub Log(ByVal fun As String)
TextBox1.Text &= "While in function " & fun & " the number of
items are " & ComboBox1.Items.Count & " currently selected index " &
ComboBox1.SelectedIndex & vbCrLf
End Sub
End Class