Code order

Jiale Xue - MSFT 46,876 信誉分 Microsoft 供应商
2024-05-22T06:17:34.2233333+00:00

我有一个带有文本框、标签、组合框等的表单。很长一段时间以来,我在打开要显示的表单时遇到了问题,并且组合框无法显示正确的项目。这花了一些时间,但我发现两行代码的顺序一直是问题所在。在这一点上,我知道哪里错了,但我无法理解为什么它是错误的。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

Windows 窗体
Windows 窗体
一组用于开发图形用户界面的 .NET Framework 托管库。
114 个问题
VB
VB
Microsoft 开发的一种面向对象的编程语言,其在 .NET Framework 上实现。 以前称为 Visual Basic .NET。
73 个问题
0 个注释 无注释
{count} 票

接受的答案
  1. Hui Liu-MSFT 48,576 信誉分 Microsoft 供应商
    2024-05-22T08:46:31.34+00:00

    首先,您需要注意 ComboBox 不提供正确绑定到 SelectedItem 所需的 SelectedItemChanged 事件。 只有 ComboBox、CheckedListBox 和 ListBox 控件的 SelectedValue 属性是简单绑定的。 更多详细信息,您可以参考此文档。 对于组合框绑定数据,需要设置 ComboBox 的两个 Member 属性(ValueMember,DisplayMember )并加载 dataSource:

    comboBox.DataSource = dataSource;  
    comboBox.ValueMember = valueMember;  
    comboBox.DisplayMember = displayMember;  
    

    否则,它将阻止 DataSource 的正确重新绑定。 然后将所选值绑定到列。

    comboBox.DataBindings.Add("SelectedValue", boundDataSource, boundDataMember);  
    

    您也可以尝试重置 ComboBox 的 DataSource(设置为 null)并重新绑定它。 所以你需要调用 SetState(“View”) 方法,然后调用 BindControls()。 这是一个相关的主题,希望对您有所帮助。


    如果回复有帮助,请点击“接受答案”并点赞。

    注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。

    1 个人认为此答案很有帮助。
    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助

你的答案

问题作者可以将答案标记为“接受的答案”,这有助于用户了解已解决作者问题的答案。