我有一个带有文本框、标签、组合框等的表单。很长一段时间以来,我在打开要显示的表单时遇到了问题,并且组合框无法显示正确的项目。这花了一些时间,但我发现两行代码的顺序一直是问题所在。在这一点上,我知道哪里错了,但我无法理解为什么它是错误的。SetState(“View”) BindControls() SetState() 是一个子,它根据参数设置窗体上的所有控件,如下所示:
Private Sub SetState(ByVal AppState As String) #Region "General Form Properties"
Me.Text = "Document Record"
#End Region Select Case AppState Case "Add"
#Region "Button Properties"
btnObsolete.Enabled = False btnEdit.Enabled = False btnCancel.Enabled = True btnSave.Enabled = True btnScan.Enabled = False btnItem.Enabled = False btnClose.Enabled = False btnHelp.Enabled = True #End Region #Region "Control Properties"
pnlDocRecord.Enabled = True lblDocID.Visible = True txtName.ReadOnly = False txtDescription.ReadOnly = False lblFilePath.ForeColor = Color.Blue cboDoc.Enabled = False cboDoc.TabIndex = 2 cboDoc.TabStop = False cboDoc.ValueMember = "DocType"
cboDoc.DisplayMember = "DocType"
cboDoc.SelectedIndex = -1 cboOwner.Enabled = False cboOwner.TabIndex = 2 cboOwner.TabStop = False cboOwner.ValueMember = "Department"
cboOwner.DisplayMember = "Department"
cboOwner.SelectedIndex = -1 cboWhere.Enabled = False cboWhere.TabIndex = 2 cboWhere.TabStop = False cboWhere.ValueMember = "Where"
cboWhere.DisplayMember = "Where"
cboWhere.SelectedIndex = -1 rdoActive.Visible = True rdoActive.Enabled = False rdoObsolete.Visible = True rdoObsolete.Enabled = False txtName.Focus() #End Region Case "Edit", "Revise"
#Region "Button Properties"
btnObsolete.Enabled = False btnEdit.Enabled = False btnCancel.Enabled = True btnSave.Enabled = True btnScan.Enabled = False btnItem.Enabled = False btnClose.Enabled = False btnHelp.Enabled = True #End Region #Region "Control Properties"
pnlDocRecord.Enabled = True lblDocID.Visible = True txtName.ReadOnly = False txtName.TabIndex = 0 txtName.TabStop = True txtDescription.ReadOnly = False txtDescription.TabIndex = 1 txtDescription.TabStop = True lblFilePath.ForeColor = Color.Blue cboDoc.Enabled = True cboDoc.TabIndex = 2 cboDoc.TabStop = True cboDoc.DataSource = DocBindingSource cboDoc.ValueMember = "DocType"
cboDoc.DisplayMember = "DocType"
cboOwner.Enabled = True cboOwner.TabIndex = 3 cboOwner.TabStop = True cboOwner.DataSource = OwnerBindingSource cboOwner.ValueMember = "Department"
cboOwner.DisplayMember = "Department"
cboWhere.Enabled = True cboWhere.TabIndex = 4 cboWhere.TabStop = True cboWhere.DataSource = WhereBindingSource cboWhere.ValueMember = "Where"
cboWhere.DisplayMember = "Where"
rdoActive.Visible = True rdoActive.Enabled = False rdoObsolete.Visible = True rdoObsolete.Enabled = False txtName.Focus() #End Region Case "View"
#Region "Button Properties"
btnObsolete.Enabled = False If MyUser = "DocMaster"
Then btnEdit.Enabled = False Else btnEdit.Enabled = True btnCancel.Enabled = False btnSave.Enabled = False btnScan.Enabled = False btnItem.Enabled = False btnClose.Enabled = True btnHelp.Enabled = True #End Region #Region "Control Properties"
pnlDocRecord.Enabled = True lblDocID.Visible = True txtName.ReadOnly = True txtName.TabIndex = 0 txtName.TabStop = False txtDescription.ReadOnly = True txtDescription.TabIndex = 1 txtDescription.TabStop = False lblFilePath.ForeColor = Color.Blue cboDoc.Enabled = False cboDoc.TabIndex = 2 cboDoc.TabStop = False cboDoc.DataSource = DocBindingSource cboDoc.ValueMember = "DocType"
cboDoc.DisplayMember = "DocType"
cboDoc.SelectedIndex = -1 cboOwner.Enabled = False cboOwner.TabIndex = 3 cboOwner.TabStop = False cboOwner.DataSource = OwnerBindingSource cboOwner.ValueMember = "Department"
cboOwner.DisplayMember = "Department"
cboOwner.SelectedIndex = -1 cboWhere.Enabled = False cboWhere.TabIndex = 4 cboWhere.TabStop = False cboWhere.DataSource = WhereBindingSource cboWhere.ValueMember = "Where"
cboWhere.DisplayMember = "Where"
cboWhere.SelectedIndex = -1 rdoActive.Visible = True rdoActive.Enabled = False rdoObsolete.Visible = True rdoObsolete.Enabled = False txtName.Focus() #End Region End Select End Sub BindControls() does exactly as named.It is to bind the controls with table values.Private Sub BindControls() Dim MasterBaseBindingSource As New BindingSource MasterBaseBindingSource.DataSource = MasterBase lblDocID.DataBindings.Add("Text", MasterBase.ListTable, "MasterBaseID") lblRev.DataBindings.Add("Text", MasterBase.ListTable, "Revision") txtName.DataBindings.Add("Text", MasterBase.ListTable, "Title") txtDescription.DataBindings.Add("Text", MasterBase.ListTable, "Description") lblFilePath.DataBindings.Add("Text", MasterBase.ListTable, "FilePath") cboDoc.DataBindings.Add("Text", MasterBase.ListTable, "DocType") cboWhere.DataBindings.Add("Text", MasterBase.ListTable, "WhereUsed") cboOwner.DataBindings.Add("Text", MasterBase.ListTable, "Owner") rdoActive.DataBindings.Add("Checked", MasterBase.ListTable, "Effective", True, DataSourceUpdateMode.OnValidation, CheckState.Indeterminate) rdoObsolete.DataBindings.Add("Checked", MasterBase.ListTable, "Obsolete", True, DataSourceUpdateMode.OnValidation, CheckState.Indeterminate) End Sub
我对这两种方法/子方法都没有问题。 然而,经过几个小时、几个小时、几个小时的观看这个废话的运行后,我发现问题与两行代码(上面)的顺序颠倒有关。 我注意到,如果更改顺序,表单上的所有表格数据都会正确显示,但组合框的数据除外。 这让我很震惊。 为什么只有组合框数据对两行的顺序敏感? 嗬嗬!!! 又这样了。 在我头上!
Note:此问题总结整理于: Code order