Results 1 to 6 of 6

Thread: [RESOLVED] Subclass BindingNavigator - Strange behavior

  1. #1

    Thread Starter
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Resolved [RESOLVED] Subclass BindingNavigator - Strange behavior

    Hello all,

    One of the VBF member was asking how to add a close button on the BindingNavigator control once and reuse it on all other forms, so I try to create a custom bindingnavigator class that inherits the BindingNavigator class.
    Below is the inherited class:
    vb.net Code:
    1. Public Class BindingNavigatorEx
    2.     Inherits System.Windows.Forms.BindingNavigator
    3.  
    4.     Public Sub New()
    5.         MyBase.New()
    6.  
    7.         'Add the closeform button
    8.         Dim closeButton As New ToolStripButton("Close", Nothing, New EventHandler(AddressOf CloseForm_OnClick))
    9.         Me.Items.Add(closeButton)
    10.  
    11.         'Add a separator
    12.         Dim separator As New ToolStripSeparator()
    13.         separator.Size = New System.Drawing.Size(6, 25)
    14.         Me.Items.Add(separator)
    15.  
    16.     End Sub
    17.  
    18.     Private Sub CloseForm_OnClick(ByVal sender As Object, ByVal e As System.EventArgs)
    19.         'Find the outer most parent container control and trycast it to a form
    20.         'and call the close method on that form
    21.         Dim parent As Form = Nothing
    22.         Dim child As Control = Me
    23.         Dim ctrl As Control = Me.Parent
    24.         Do Until ctrl Is Nothing
    25.             child = ctrl
    26.             ctrl = ctrl.Parent
    27.         Loop
    28.         parent = TryCast(child, Form)
    29.         If parent IsNot Nothing Then
    30.             parent.Close()
    31.         End If
    32.     End Sub
    33. End Class
    The thing that I can't figure out is that it looks OK in design mode, but at run time, the custom binding navigator shows 2 "Close" buttons. Only one of them will close the form when clicked, the other one doesn't function. See the attached image.
    Any ideas?
    Last edited by stanav; Sep 11th, 2009 at 08:19 AM.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Subclass BindingNavigator - Strange behavior

    I know what is happening, and there is a workaround for it. When you add that inherited BindingNavigator to the form (container) it will run a call to New (obviously) so that's where it adds the first close button. Then if you take a look at the Designer generated code for the BindingNavigator you will see in there somewhere this:

    vb.net Code:
    1. '
    2.         'BindingNavigatorEx1
    3.         '
    4.         Me.BindingNavigatorEx1.AddNewItem = Me.BindingNavigatorAddNewItem
    5.         Me.BindingNavigatorEx1.CountItem = Me.BindingNavigatorCountItem
    6.         Me.BindingNavigatorEx1.DeleteItem = Me.BindingNavigatorDeleteItem
    7.         '**Delete Me.ToolStripButton1 from next line**
    8.         Me.BindingNavigatorEx1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.ToolStripButton1, _
    9.                                                                                         Me.BindingNavigatorMoveFirstItem, _
    10.                                                                                         Me.BindingNavigatorMovePreviousItem, _
    11.                                                                                         Me.BindingNavigatorSeparator, _
    12.                                                                                         Me.BindingNavigatorPositionItem, _
    13.                                                                                         Me.BindingNavigatorCountItem, _
    14.                                                                                         Me.BindingNavigatorSeparator1, _
    15.                                                                                         Me.BindingNavigatorMoveNextItem, _
    16.                                                                                         Me.BindingNavigatorMoveLastItem, _
    17.                                                                                         Me.BindingNavigatorSeparator2, _
    18.                                                                                         Me.BindingNavigatorAddNewItem, _
    19.                                                                                         Me.BindingNavigatorDeleteItem})
    20.         Me.BindingNavigatorEx1.Location = New System.Drawing.Point(0, 0)
    21.         Me.BindingNavigatorEx1.MoveFirstItem = Me.BindingNavigatorMoveFirstItem
    22.         Me.BindingNavigatorEx1.MoveLastItem = Me.BindingNavigatorMoveLastItem
    23.         Me.BindingNavigatorEx1.MoveNextItem = Me.BindingNavigatorMoveNextItem
    24.         Me.BindingNavigatorEx1.MovePreviousItem = Me.BindingNavigatorMovePreviousItem
    25.         Me.BindingNavigatorEx1.Name = "BindingNavigatorEx1"
    26.         Me.BindingNavigatorEx1.PositionItem = Me.BindingNavigatorPositionItem
    27.         Me.BindingNavigatorEx1.Size = New System.Drawing.Size(1189, 25)
    28.         Me.BindingNavigatorEx1.TabIndex = 0
    29.         Me.BindingNavigatorEx1.Text = "BindingNavigatorEx1"
    30.         '
    31.         'ToolStripButton1 **Delete these lines**
    32.         '
    33.         Me.ToolStripButton1.Name = "ToolStripButton1"
    34.         Me.ToolStripButton1.Size = New System.Drawing.Size(37, 22)
    35.         Me.ToolStripButton1.Text = "Close"

    If you notice, it is creating code for the Close button that you added when you added the BindingNavigator to the form. So when the application actually gets run, it will create the first button on the call to New, and then the second button in this code here. The workaround is to just delete the Designer code for that ToolStripButton.

    I'm not sure how to prevent the Designer from generating the code for that button.

  3. #3
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Subclass BindingNavigator - Strange behavior

    Here's another workaround actually, you don't have to mess with designer code, you just won't see the Button until the application is run:

    vb.net Code:
    1. Public Class BindingNavigatorEx
    2.     Inherits System.Windows.Forms.BindingNavigator
    3.  
    4.     Public Sub New()
    5.         MyBase.New()
    6.     End Sub
    7.  
    8.     Private Sub CloseForm_OnClick(ByVal sender As Object, ByVal e As System.EventArgs)
    9.         'Find the outer most parent container control and trycast it to a form
    10.         'and call the close method on that form
    11.         Dim parent As Form = Nothing
    12.         Dim child As Control = Me
    13.         Dim ctrl As Control = Me.Parent
    14.         Do Until ctrl Is Nothing
    15.             child = ctrl
    16.             ctrl = ctrl.Parent
    17.         Loop
    18.         parent = TryCast(child, Form)
    19.         If parent IsNot Nothing Then
    20.             parent.Close()
    21.         End If
    22.     End Sub
    23.  
    24.     Protected Overrides Sub InitLayout()
    25.         MyBase.InitLayout()
    26.         If Not Me.DesignMode Then
    27.             Dim closeButton As New ToolStripButton("Close", Nothing, New EventHandler(AddressOf CloseForm_OnClick))
    28.             Me.Items.Add(closeButton)
    29.         End If
    30.     End Sub
    31.  
    32. End Class

  4. #4

    Thread Starter
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Subclass BindingNavigator - Strange behavior

    I re-read the documentation for BindingNavigator and found out what I was doing wrong.
    I should've overridden the AddStandardItems method and insert the button there.
    So this is the re-done code and it works 100%.
    vb.net Code:
    1. Public Class BindingNavigatorEx
    2.     Inherits System.Windows.Forms.BindingNavigator
    3.  
    4.     Private WithEvents _CloseForm As ToolStripItem = Nothing
    5.     Public Property CloseFormItem() As ToolStripItem
    6.         Get
    7.             Return _CloseForm
    8.         End Get
    9.         Set(ByVal value As ToolStripItem)
    10.             _CloseForm = value
    11.         End Set
    12.     End Property
    13.  
    14.     Public Sub New()
    15.         MyBase.New()
    16.     End Sub
    17.  
    18.     Public Overrides Sub AddStandardItems()
    19.         Me._CloseForm = New ToolStripButton("Close")
    20.         Me._CloseForm.Name = "ToolStripButtonCloseForm"
    21.         Me.Items.Add(Me._CloseForm)
    22.         'Add a separator
    23.         Dim separator As New ToolStripSeparator()
    24.         Me.Items.Add(separator)
    25.         MyBase.AddStandardItems()
    26.     End Sub
    27.  
    28.     Private Sub _CloseForm_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles _CloseForm.Click
    29.         If Me.DesignMode = False Then
    30.             'Find the outer most parent container control and trycast it to a form
    31.             'and call the close method on that form
    32.             Dim parent As Form = Nothing
    33.             Dim child As Control = Me
    34.             Dim ctrl As Control = Me.Parent
    35.             Do Until ctrl Is Nothing
    36.                 child = ctrl
    37.                 ctrl = ctrl.Parent
    38.             Loop
    39.             parent = TryCast(child, Form)
    40.             If parent IsNot Nothing Then
    41.                 parent.Close()
    42.             End If
    43.         End If
    44.     End Sub
    45. End Class
    So, as usually, there are many ways to skin a cat
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [RESOLVED] Subclass BindingNavigator - Strange behavior

    stanav, why not just do a

    Code:
    Dim F as form = Me.FindForm
    If F isnot nothing then
        F.Close()
    End If
    instead of looping back through the parent/child tree to find the parent form?

  6. #6

    Thread Starter
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [RESOLVED] Subclass BindingNavigator - Strange behavior

    Quote Originally Posted by kleinma View Post
    stanav, why not just do a

    Code:
    Dim F as form = Me.FindForm
    If F isnot nothing then
        F.Close()
    End If
    instead of looping back through the parent/child tree to find the parent form?
    Good suggestion, Kleinma
    Didn't think of that... I need some coffee
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width