J
Joshua Anderson
Hi,
Ok what I am trying to do is create a combobox at runtime, populate it with
data, add it to my tab control, and sets its selectedindex to -1, so nothing
is selected.
So that all works, when the page is displayed, the combobox is there with
nothing selected, but when I click on another tab, then click back, the
first item is selected in the combobox!!!!!!!!!!
Now that really sucks, as I cant have that for my application.
If you add the items without populating from a dataset it it seems to work
fine, (.items.add("etc")), but when the data is binded it does not work
properly.
Any ideas?
Source code below and attached:
----------------------------------------------------------------------------
Imports System.Data.SqlClient
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 TabControl1 As System.Windows.Forms.TabControl
Friend WithEvents TabPage1 As System.Windows.Forms.TabPage
Friend WithEvents TabPage2 As System.Windows.Forms.TabPage
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.TabControl1 = New System.Windows.Forms.TabControl
Me.TabPage1 = New System.Windows.Forms.TabPage
Me.TabPage2 = New System.Windows.Forms.TabPage
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.TabControl1.SuspendLayout()
Me.TabPage1.SuspendLayout()
Me.TabPage2.SuspendLayout()
Me.SuspendLayout()
'
'TabControl1
'
Me.TabControl1.Controls.Add(Me.TabPage1)
Me.TabControl1.Controls.Add(Me.TabPage2)
Me.TabControl1.Location = New System.Drawing.Point(8, 8)
Me.TabControl1.Name = "TabControl1"
Me.TabControl1.SelectedIndex = 0
Me.TabControl1.Size = New System.Drawing.Size(352, 216)
Me.TabControl1.TabIndex = 0
'
'TabPage1
'
Me.TabPage1.Controls.Add(Me.Label2)
Me.TabPage1.Location = New System.Drawing.Point(4, 22)
Me.TabPage1.Name = "TabPage1"
Me.TabPage1.Size = New System.Drawing.Size(344, 190)
Me.TabPage1.TabIndex = 0
Me.TabPage1.Text = "TabPage1"
'
'TabPage2
'
Me.TabPage2.Controls.Add(Me.Label1)
Me.TabPage2.Location = New System.Drawing.Point(4, 22)
Me.TabPage2.Name = "TabPage2"
Me.TabPage2.Size = New System.Drawing.Size(344, 190)
Me.TabPage2.TabIndex = 1
Me.TabPage2.Text = "TabPage2"
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif",
15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
Me.Label1.Location = New System.Drawing.Point(24, 32)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(160, 32)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Now click Back"
'
'Label2
'
Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif",
15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
Me.Label2.Location = New System.Drawing.Point(24, 136)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(264, 32)
Me.Label2.TabIndex = 1
Me.Label2.Text = "Click On The Other Tab"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(360, 229)
Me.Controls.Add(Me.TabControl1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.TabControl1.ResumeLayout(False)
Me.TabPage1.ResumeLayout(False)
Me.TabPage2.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Set Connection String
Dim gDBConn As New SqlConnection
gDBConn.ConnectionString = "user id=user; password=user; initial
catalog=database; server=theserver;"
gDBConn.Open()
'Create Combobox and set defaults
Dim tempComboBox As ComboBox = New ComboBox
tempComboBox.Sorted = True
tempComboBox.Left = 50
tempComboBox.Top = 50
tempComboBox.Width = 150
tempComboBox.DropDownStyle = ComboBoxStyle.DropDownList
Dim tempDataSet As New DataSet
Dim tempSQL As String = "select
controlProducerID,controlProducerName from cms_controlProducer"
'Get Adapter and fill dataset
Dim adapter As New SqlDataAdapter
adapter.SelectCommand = New SqlCommand(tempSQL, gDBConn)
adapter.Fill(tempDataSet, "myMapping")
adapter.Dispose()
'Populate ComboBox with Dataset
tempComboBox.DataSource = tempDataSet.Tables(0)
tempComboBox.ValueMember = "controlProducerID"
tempComboBox.DisplayMember = "controlProducerName"
'Dispose Of Dataset
tempDataSet.Dispose()
'Add Control to Tab Page1
TabPage1.Controls.Add(tempComboBox)
'Sets selected Index to -1 so nothing is displayed.
tempComboBox.SelectedIndex = -1
End Sub
End Class
Ok what I am trying to do is create a combobox at runtime, populate it with
data, add it to my tab control, and sets its selectedindex to -1, so nothing
is selected.
So that all works, when the page is displayed, the combobox is there with
nothing selected, but when I click on another tab, then click back, the
first item is selected in the combobox!!!!!!!!!!
Now that really sucks, as I cant have that for my application.
If you add the items without populating from a dataset it it seems to work
fine, (.items.add("etc")), but when the data is binded it does not work
properly.
Any ideas?
Source code below and attached:
----------------------------------------------------------------------------
Imports System.Data.SqlClient
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 TabControl1 As System.Windows.Forms.TabControl
Friend WithEvents TabPage1 As System.Windows.Forms.TabPage
Friend WithEvents TabPage2 As System.Windows.Forms.TabPage
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.TabControl1 = New System.Windows.Forms.TabControl
Me.TabPage1 = New System.Windows.Forms.TabPage
Me.TabPage2 = New System.Windows.Forms.TabPage
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.TabControl1.SuspendLayout()
Me.TabPage1.SuspendLayout()
Me.TabPage2.SuspendLayout()
Me.SuspendLayout()
'
'TabControl1
'
Me.TabControl1.Controls.Add(Me.TabPage1)
Me.TabControl1.Controls.Add(Me.TabPage2)
Me.TabControl1.Location = New System.Drawing.Point(8, 8)
Me.TabControl1.Name = "TabControl1"
Me.TabControl1.SelectedIndex = 0
Me.TabControl1.Size = New System.Drawing.Size(352, 216)
Me.TabControl1.TabIndex = 0
'
'TabPage1
'
Me.TabPage1.Controls.Add(Me.Label2)
Me.TabPage1.Location = New System.Drawing.Point(4, 22)
Me.TabPage1.Name = "TabPage1"
Me.TabPage1.Size = New System.Drawing.Size(344, 190)
Me.TabPage1.TabIndex = 0
Me.TabPage1.Text = "TabPage1"
'
'TabPage2
'
Me.TabPage2.Controls.Add(Me.Label1)
Me.TabPage2.Location = New System.Drawing.Point(4, 22)
Me.TabPage2.Name = "TabPage2"
Me.TabPage2.Size = New System.Drawing.Size(344, 190)
Me.TabPage2.TabIndex = 1
Me.TabPage2.Text = "TabPage2"
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif",
15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
Me.Label1.Location = New System.Drawing.Point(24, 32)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(160, 32)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Now click Back"
'
'Label2
'
Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif",
15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
Me.Label2.Location = New System.Drawing.Point(24, 136)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(264, 32)
Me.Label2.TabIndex = 1
Me.Label2.Text = "Click On The Other Tab"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(360, 229)
Me.Controls.Add(Me.TabControl1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.TabControl1.ResumeLayout(False)
Me.TabPage1.ResumeLayout(False)
Me.TabPage2.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Set Connection String
Dim gDBConn As New SqlConnection
gDBConn.ConnectionString = "user id=user; password=user; initial
catalog=database; server=theserver;"
gDBConn.Open()
'Create Combobox and set defaults
Dim tempComboBox As ComboBox = New ComboBox
tempComboBox.Sorted = True
tempComboBox.Left = 50
tempComboBox.Top = 50
tempComboBox.Width = 150
tempComboBox.DropDownStyle = ComboBoxStyle.DropDownList
Dim tempDataSet As New DataSet
Dim tempSQL As String = "select
controlProducerID,controlProducerName from cms_controlProducer"
'Get Adapter and fill dataset
Dim adapter As New SqlDataAdapter
adapter.SelectCommand = New SqlCommand(tempSQL, gDBConn)
adapter.Fill(tempDataSet, "myMapping")
adapter.Dispose()
'Populate ComboBox with Dataset
tempComboBox.DataSource = tempDataSet.Tables(0)
tempComboBox.ValueMember = "controlProducerID"
tempComboBox.DisplayMember = "controlProducerName"
'Dispose Of Dataset
tempDataSet.Dispose()
'Add Control to Tab Page1
TabPage1.Controls.Add(tempComboBox)
'Sets selected Index to -1 so nothing is displayed.
tempComboBox.SelectedIndex = -1
End Sub
End Class