Link to home
Start Free TrialLog in
Avatar of chlade
chlade

asked on

BindingNavigator disable problem

I am using a BindingNavigator to allow navigation through data on my form.

Depending on the situation, I either want to enable or disable the Delete button on the navigation bar (the red "X").

However, I am having problems getting this to work.  I have narrowed it down to a situation where it appears certain things reset the enabled property.

For instance, if I have the form open and the delete button is disabled, if I do an AcceptChanges on the dataset, the BindingNavigator delete button resets to be enabled.  If I add a line immediately after the ds.AcceptChanges to reset the button to be disabled, it works fine.

My question is why does this happen?  What would be the best way to handle it?  Certainly I could reset the value after each ds.AcceptChanges, but this doesn't seem real elegant.

Also, are there any other situations I should be aware of that would reset the enabled property?

Any information regarding this situation is much appreciated.

Thanks,
Chris
Avatar of chlade
chlade

ASKER

Here is a sample program that demonstrates my problem:

Option Strict On
Option Explicit On

Public Class frmInvoiceEntry
    Inherits System.Windows.Forms.Form

    Friend WithEvents ds As System.Data.DataSet
    Friend WithEvents dtServiceHeader As System.Data.DataTable

    Friend WithEvents colSHClient As System.Data.DataColumn
    Friend WithEvents colSHClientName As System.Data.DataColumn

    Friend WithEvents txtClientName As System.Windows.Forms.TextBox
    Friend WithEvents txtClient As System.Windows.Forms.TextBox

    Friend WithEvents btnDisableThenAccept As System.Windows.Forms.Button
    Friend WithEvents btnAcceptThenDisable As System.Windows.Forms.Button

    Friend WithEvents masterBindingSource As System.Windows.Forms.BindingSource
    Friend WithEvents BindingNavigator1 As System.Windows.Forms.BindingNavigator
    Friend WithEvents BindingNavigatorAddNewItem As System.Windows.Forms.ToolStripButton
    Friend WithEvents BindingNavigatorCountItem As System.Windows.Forms.ToolStripLabel
    Friend WithEvents BindingNavigatorDeleteItem As System.Windows.Forms.ToolStripButton
    Friend WithEvents BindingNavigatorMoveFirstItem As System.Windows.Forms.ToolStripButton
    Friend WithEvents BindingNavigatorMovePreviousItem As System.Windows.Forms.ToolStripButton
    Friend WithEvents BindingNavigatorSeparator As System.Windows.Forms.ToolStripSeparator
    Friend WithEvents BindingNavigatorPositionItem As System.Windows.Forms.ToolStripTextBox
    Friend WithEvents BindingNavigatorSeparator1 As System.Windows.Forms.ToolStripSeparator
    Friend WithEvents BindingNavigatorMoveNextItem As System.Windows.Forms.ToolStripButton
    Friend WithEvents BindingNavigatorMoveLastItem As System.Windows.Forms.ToolStripButton
    Friend WithEvents BindingNavigatorSeparator2 As System.Windows.Forms.ToolStripSeparator

#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.

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(frmInvoiceEntry))
        Me.masterBindingSource = New System.Windows.Forms.BindingSource(Me.components)
        Me.ds = New System.Data.DataSet
        Me.dtServiceHeader = New System.Data.DataTable
        Me.colSHClient = New System.Data.DataColumn
        Me.colSHClientName = New System.Data.DataColumn
        Me.BindingNavigator1 = New System.Windows.Forms.BindingNavigator(Me.components)
        Me.BindingNavigatorCountItem = New System.Windows.Forms.ToolStripLabel
        Me.BindingNavigatorDeleteItem = New System.Windows.Forms.ToolStripButton
        Me.BindingNavigatorMoveFirstItem = New System.Windows.Forms.ToolStripButton
        Me.BindingNavigatorMovePreviousItem = New System.Windows.Forms.ToolStripButton
        Me.BindingNavigatorSeparator = New System.Windows.Forms.ToolStripSeparator
        Me.BindingNavigatorPositionItem = New System.Windows.Forms.ToolStripTextBox
        Me.BindingNavigatorSeparator1 = New System.Windows.Forms.ToolStripSeparator
        Me.BindingNavigatorMoveNextItem = New System.Windows.Forms.ToolStripButton
        Me.BindingNavigatorMoveLastItem = New System.Windows.Forms.ToolStripButton
        Me.BindingNavigatorSeparator2 = New System.Windows.Forms.ToolStripSeparator
        Me.BindingNavigatorAddNewItem = New System.Windows.Forms.ToolStripButton
        Me.txtClientName = New System.Windows.Forms.TextBox
        Me.btnDisableThenAccept = New System.Windows.Forms.Button
        Me.btnAcceptThenDisable = New System.Windows.Forms.Button
        Me.txtClient = New System.Windows.Forms.TextBox
        CType(Me.masterBindingSource, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.ds, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.dtServiceHeader, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.BindingNavigator1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.BindingNavigator1.SuspendLayout()
        Me.SuspendLayout()
        '
        'masterBindingSource
        '
        Me.masterBindingSource.DataMember = "ServiceHeader"
        Me.masterBindingSource.DataSource = Me.ds
        Me.masterBindingSource.Sort = "Client"
        '
        'ds
        '
        Me.ds.DataSetName = "NewDataSet"
        Me.ds.Tables.AddRange(New System.Data.DataTable() {Me.dtServiceHeader})
        '
        'dtServiceHeader
        '
        Me.dtServiceHeader.Columns.AddRange(New System.Data.DataColumn() {Me.colSHClient, Me.colSHClientName})
        Me.dtServiceHeader.Constraints.AddRange(New System.Data.Constraint() {New System.Data.UniqueConstraint("Constraint1", New String() {"Client"}, False)})
        Me.dtServiceHeader.TableName = "ServiceHeader"
        '
        'colSHClient
        '
        Me.colSHClient.Caption = "Client"
        Me.colSHClient.ColumnName = "Client"
        Me.colSHClient.DefaultValue = ""
        '
        'colSHClientName
        '
        Me.colSHClientName.ColumnName = "ClientName"
        '
        'BindingNavigator1
        '
        Me.BindingNavigator1.AddNewItem = Nothing
        Me.BindingNavigator1.BindingSource = Me.masterBindingSource
        Me.BindingNavigator1.CountItem = Me.BindingNavigatorCountItem
        Me.BindingNavigator1.DeleteItem = Me.BindingNavigatorDeleteItem
        Me.BindingNavigator1.Dock = System.Windows.Forms.DockStyle.None
        Me.BindingNavigator1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.BindingNavigatorMoveFirstItem, Me.BindingNavigatorMovePreviousItem, Me.BindingNavigatorSeparator, Me.BindingNavigatorPositionItem, Me.BindingNavigatorCountItem, Me.BindingNavigatorSeparator1, Me.BindingNavigatorMoveNextItem, Me.BindingNavigatorMoveLastItem, Me.BindingNavigatorSeparator2, Me.BindingNavigatorAddNewItem, Me.BindingNavigatorDeleteItem})
        Me.BindingNavigator1.Location = New System.Drawing.Point(12, 18)
        Me.BindingNavigator1.MoveFirstItem = Me.BindingNavigatorMoveFirstItem
        Me.BindingNavigator1.MoveLastItem = Me.BindingNavigatorMoveLastItem
        Me.BindingNavigator1.MoveNextItem = Me.BindingNavigatorMoveNextItem
        Me.BindingNavigator1.MovePreviousItem = Me.BindingNavigatorMovePreviousItem
        Me.BindingNavigator1.Name = "BindingNavigator1"
        Me.BindingNavigator1.PositionItem = Me.BindingNavigatorPositionItem
        Me.BindingNavigator1.Size = New System.Drawing.Size(222, 25)
        Me.BindingNavigator1.TabIndex = 0
        Me.BindingNavigator1.Text = "BindingNavigator1"
        '
        'BindingNavigatorCountItem
        '
        Me.BindingNavigatorCountItem.Name = "BindingNavigatorCountItem"
        Me.BindingNavigatorCountItem.Size = New System.Drawing.Size(36, 22)
        Me.BindingNavigatorCountItem.Text = "of {0}"
        Me.BindingNavigatorCountItem.ToolTipText = "Total number of items"
        '
        'BindingNavigatorDeleteItem
        '
        Me.BindingNavigatorDeleteItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image
        Me.BindingNavigatorDeleteItem.Image = CType(resources.GetObject("BindingNavigatorDeleteItem.Image"), System.Drawing.Image)
        Me.BindingNavigatorDeleteItem.Name = "BindingNavigatorDeleteItem"
        Me.BindingNavigatorDeleteItem.RightToLeftAutoMirrorImage = True
        Me.BindingNavigatorDeleteItem.Size = New System.Drawing.Size(23, 22)
        Me.BindingNavigatorDeleteItem.Text = "Delete"
        '
        'BindingNavigatorMoveFirstItem
        '
        Me.BindingNavigatorMoveFirstItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image
        Me.BindingNavigatorMoveFirstItem.Image = CType(resources.GetObject("BindingNavigatorMoveFirstItem.Image"), System.Drawing.Image)
        Me.BindingNavigatorMoveFirstItem.Name = "BindingNavigatorMoveFirstItem"
        Me.BindingNavigatorMoveFirstItem.RightToLeftAutoMirrorImage = True
        Me.BindingNavigatorMoveFirstItem.Size = New System.Drawing.Size(23, 22)
        Me.BindingNavigatorMoveFirstItem.Text = "Move first"
        '
        'BindingNavigatorMovePreviousItem
        '
        Me.BindingNavigatorMovePreviousItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image
        Me.BindingNavigatorMovePreviousItem.Image = CType(resources.GetObject("BindingNavigatorMovePreviousItem.Image"), System.Drawing.Image)
        Me.BindingNavigatorMovePreviousItem.Name = "BindingNavigatorMovePreviousItem"
        Me.BindingNavigatorMovePreviousItem.RightToLeftAutoMirrorImage = True
        Me.BindingNavigatorMovePreviousItem.Size = New System.Drawing.Size(23, 22)
        Me.BindingNavigatorMovePreviousItem.Text = "Move previous"
        '
        'BindingNavigatorSeparator
        '
        Me.BindingNavigatorSeparator.Name = "BindingNavigatorSeparator"
        Me.BindingNavigatorSeparator.Size = New System.Drawing.Size(6, 25)
        '
        'BindingNavigatorPositionItem
        '
        Me.BindingNavigatorPositionItem.AccessibleName = "Position"
        Me.BindingNavigatorPositionItem.AutoSize = False
        Me.BindingNavigatorPositionItem.BackColor = System.Drawing.SystemColors.Window
        Me.BindingNavigatorPositionItem.Name = "BindingNavigatorPositionItem"
        Me.BindingNavigatorPositionItem.Size = New System.Drawing.Size(18, 21)
        Me.BindingNavigatorPositionItem.Text = "0"
        Me.BindingNavigatorPositionItem.ToolTipText = "Current position"
        '
        'BindingNavigatorSeparator1
        '
        Me.BindingNavigatorSeparator1.Name = "BindingNavigatorSeparator1"
        Me.BindingNavigatorSeparator1.Size = New System.Drawing.Size(6, 25)
        '
        'BindingNavigatorMoveNextItem
        '
        Me.BindingNavigatorMoveNextItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image
        Me.BindingNavigatorMoveNextItem.Image = CType(resources.GetObject("BindingNavigatorMoveNextItem.Image"), System.Drawing.Image)
        Me.BindingNavigatorMoveNextItem.Name = "BindingNavigatorMoveNextItem"
        Me.BindingNavigatorMoveNextItem.RightToLeftAutoMirrorImage = True
        Me.BindingNavigatorMoveNextItem.Size = New System.Drawing.Size(23, 22)
        Me.BindingNavigatorMoveNextItem.Text = "Move next"
        '
        'BindingNavigatorMoveLastItem
        '
        Me.BindingNavigatorMoveLastItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image
        Me.BindingNavigatorMoveLastItem.Image = CType(resources.GetObject("BindingNavigatorMoveLastItem.Image"), System.Drawing.Image)
        Me.BindingNavigatorMoveLastItem.Name = "BindingNavigatorMoveLastItem"
        Me.BindingNavigatorMoveLastItem.RightToLeftAutoMirrorImage = True
        Me.BindingNavigatorMoveLastItem.Size = New System.Drawing.Size(23, 22)
        Me.BindingNavigatorMoveLastItem.Text = "Move last"
        '
        'BindingNavigatorSeparator2
        '
        Me.BindingNavigatorSeparator2.Name = "BindingNavigatorSeparator2"
        Me.BindingNavigatorSeparator2.Size = New System.Drawing.Size(6, 25)
        '
        'BindingNavigatorAddNewItem
        '
        Me.BindingNavigatorAddNewItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image
        Me.BindingNavigatorAddNewItem.Image = CType(resources.GetObject("BindingNavigatorAddNewItem.Image"), System.Drawing.Image)
        Me.BindingNavigatorAddNewItem.Name = "BindingNavigatorAddNewItem"
        Me.BindingNavigatorAddNewItem.RightToLeftAutoMirrorImage = True
        Me.BindingNavigatorAddNewItem.Size = New System.Drawing.Size(23, 22)
        Me.BindingNavigatorAddNewItem.Text = "Add new"
        '
        'txtClientName
        '
        Me.txtClientName.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.masterBindingSource, "ClientName", True))
        Me.txtClientName.Location = New System.Drawing.Point(12, 83)
        Me.txtClientName.Name = "txtClientName"
        Me.txtClientName.Size = New System.Drawing.Size(222, 20)
        Me.txtClientName.TabIndex = 2
        '
        'btnDisableThenAccept
        '
        Me.btnDisableThenAccept.Location = New System.Drawing.Point(254, 24)
        Me.btnDisableThenAccept.Name = "btnDisableThenAccept"
        Me.btnDisableThenAccept.Size = New System.Drawing.Size(128, 23)
        Me.btnDisableThenAccept.TabIndex = 3
        Me.btnDisableThenAccept.Text = "Disable then Accept"
        Me.btnDisableThenAccept.UseVisualStyleBackColor = True
        '
        'btnAcceptThenDisable
        '
        Me.btnAcceptThenDisable.Location = New System.Drawing.Point(254, 53)
        Me.btnAcceptThenDisable.Name = "btnAcceptThenDisable"
        Me.btnAcceptThenDisable.Size = New System.Drawing.Size(128, 23)
        Me.btnAcceptThenDisable.TabIndex = 4
        Me.btnAcceptThenDisable.Text = "Accept then Disable"
        Me.btnAcceptThenDisable.UseVisualStyleBackColor = True
        '
        'txtClient
        '
        Me.txtClient.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.masterBindingSource, "Client", True))
        Me.txtClient.Location = New System.Drawing.Point(12, 56)
        Me.txtClient.Name = "txtClient"
        Me.txtClient.Size = New System.Drawing.Size(76, 20)
        Me.txtClient.TabIndex = 1
        '
        'frmInvoiceEntry
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.AutoScroll = True
        Me.ClientSize = New System.Drawing.Size(398, 112)
        Me.Controls.Add(Me.btnAcceptThenDisable)
        Me.Controls.Add(Me.btnDisableThenAccept)
        Me.Controls.Add(Me.BindingNavigator1)
        Me.Controls.Add(Me.txtClient)
        Me.Controls.Add(Me.txtClientName)
        Me.Name = "frmInvoiceEntry"
        Me.Text = "Invoice Entry"
        CType(Me.masterBindingSource, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.ds, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.dtServiceHeader, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.BindingNavigator1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.BindingNavigator1.ResumeLayout(False)
        Me.BindingNavigator1.PerformLayout()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

#End Region

    Private Sub frmInvoiceEntry_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ds.Tables(0).Rows.Add("MIC", "Microsoft")
        ds.Tables(0).Rows.Add("IBM", "IBM")
        ds.Tables(0).Rows.Add("APL", "Apple")

        BindingNavigatorDeleteItem.Enabled = False

    End Sub

    Private Sub btnDisableThenAccept_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisableThenAccept.Click

        BindingNavigatorDeleteItem.Enabled = False
        ds.AcceptChanges()

    End Sub

    Private Sub btnAcceptThenDisable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAcceptThenDisable.Click

        ds.AcceptChanges()
        BindingNavigatorDeleteItem.Enabled = False

    End Sub

End Class
ASKER CERTIFIED SOLUTION
Avatar of Sancler
Sancler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chlade

ASKER

Thanks.  What you said does make sense.  After reading what you said it triggered a different idea for me.  In my actual program, I'm wanting to enable/disable based on whether a specific field is blank.  So what I did was put code in the Paint event of the delete button and it works great.

Here's my code:

    Private Sub BindingNavigatorDeleteItem_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles BindingNavigatorDeleteItem.Paint

        If txtClientName.Text.Trim = "" Then
            BindingNavigatorDeleteItem.Enabled = True
        Else
            BindingNavigatorDeleteItem.Enabled = False
        End If

    End Sub
Hi,

I do a lot of enabling and disabling of contols and most of the time everything works fine.  However when it does not I also put my code in the paint event.

Here is a little sample Code.

Good luck.

Kip

 Private Sub tblOrderHeaderBindingNavigator_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles TblOrderHeaderBindingNavigator.Paint

        If Me.btnHSave110.Enabled Then
            Me.grdHeader001.Enabled = False
           'Next Line example
        end if
end sub