Code order

Glenn Walker 251 Reputation points
2021-03-28T23:35:49.073+00:00

I have a form with textboxes, labels, comboboxes, etc. I have had a problem for quite some time with opening the form to display and the Comboboxes do not display the correct item. It took some time but I found that the order of two lines of code were the problem the entire time. At this point I know what is wrong, but I am unable to understand why it is wrong. SetState("View") BindControls() SetState() is a sub that sets all of the controls on the form, based on the parameters and goes something like this: 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 I am having no problem with either of the methods/subs. However, after hours and hours and hours of watching this crap run, I found that the problem is related to the order of the two lines of code (above) is reversed. I have noticed that if the order is changed, all of the table data on the form is displayed correctly, except the data for the comboboxes. Which is what blows my mind. Why is it that only the combobox data is sensitive to the order of the two lines? Whooosh!!! There it went again. Over my head!

Developer technologies | Windows Forms
{count} votes

Answer accepted by question author
  1. Daniel Zhang-MSFT 9,661 Reputation points
    2021-04-06T06:44:43.327+00:00

    Hi GlennWalker-1236,
    First, you need to note that ComboBox does not provide SelectedItemChanged event which is required for correctly binding to SelectedItem.
    Only the SelectedValue property of the ComboBox, CheckedListBox, and ListBox control is simple bound.
    More details you can refer this document.
    For combobox binding data, you need to set the two Member properties(ValueMember,DisplayMember )of ComboBox and load your dataSource:

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

    Otherwise it will prevent the correct rebinding of the DataSource.
    Then bind the selected value to your column.

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

    You can also try to reset the DataSource of the ComboBox(set it to null) and rebind it.
    So you need to call the SetState("View") method then BindControls().
    Here is a related thread and hope helpful for you.
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentationto enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Glenn Walker 251 Reputation points
    2021-04-03T15:32:25.477+00:00

    Actually I did that. Finger pinky swear. It just came out the way it did. Your interface sometimes leaves much to be desired.

    Anyway, here I go again and it looks better.

    Form Event:

    Private Sub DocRecord_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
            If MyUser = "DocMaster" Then
                GetDocument(MasterID)
                SetState("View")
                BindControls()
            Else
                Select Case DocState
                    Case "Add"
                        UpRevision = ""
                        MasterID = GetMasterBaseID(MasterID).ToString 'Create MasterBase ID
                        lblDocID.Text = MasterID
                        lblFilePath.Text = GetDirectory(MyDirectory).ToString
                        lblRev.Text = IncrementAARevision(UpRevision).ToString
                        rdoActive.Checked = False
                        SetState("View")
                    Case "View", "Edit", "Revise"
                        GetDocument(MasterID)
                        If DocState = "Revise" Then
                            SetState("Edit")
                            BindControls()
                            UpRevision = lblRev.Text
                            lblDocID.Text = MasterID
                            lblRev.Text = IncrementAARevision(UpRevision).ToString()
                            lblFilePath.Text = GetDirectory(MyDirectory).ToString
                            rdoActive.Checked = False
                            cboDoc.Text = ""
                            cboOwner.Text = ""
                            cboWhere.Text = ""
                        Else
                            SetState("View")
                            BindControls()
                        End If
                End Select
            End If
        End Sub
    

    So, basically, the two lines:

                SetState("View")
                BindControls()
    

    must be in this order, else any combo box on the form will not execute correctly.

    Set State:

        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():

    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
    

    This order, for those two lines, only matter where comboboxes are concerned. I cannot, for the life of me, understand why this is so.

    0 comments No comments

  2. Glenn Walker 251 Reputation points
    2021-04-08T16:57:11.327+00:00

    Thanks, that helps a lot.

    0 comments No comments

  3. Bonnie DeWitt 811 Reputation points
    2021-04-10T05:05:25.16+00:00

    Just an FYI about setting the ComboBox's DataSource: always set the DataSource after you set the DisplayMember and ValueMember, not before (otherwise the events will fire twice, once when you set DisplayMember and again when you set ValueMember). The events will only fire once if you set those two before you set the DataSource.

    If you're interested, see my blog post: https://geek-goddess-bonnie.blogspot.com/2018/08/combobox-gotcha-2.html


    ~~Bonnie DeWitt [MVP since 2003]
    [http://geek-goddess-bonnie.blogspot.com]2


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.