Enabling only button on the active form?

Troy

Well-known member
Joined
Feb 7, 2005
Messages
153
Programming Experience
10+
Ok I'm getting confused with which event I need to place my code to enable and disable buttons on my toolbar.

I had an MDI interface and only want certain buttons to enable when certain forms are active.

I feel it should be as simple as adding code to the event of the MDI child to enable the buttons only when that child is active and given focus but when I try to add code to the gotfocus event and lostfocus event the buttons don't change properly.

There must be an easy way to set this up that I'm overlooking. Can someone help?
 
You mean the ToolStrip toolbarish control? Look into using the merging options of the strip and items, this can be set up to automatically do stuff like this. (but you probably want to have the item removed when it should not be there)

About the events you mention read the documentation that says you should not use GotFucus and LostFocus but rather Enter/Leave events for controls or Activated/Deactive events for forms. But you should be able to solve it with toolstrip merging.
 
Yes I meant Toolstrip control. I'll try the add/remove for it like you suggest I guess I'll remove the toolstrip items in the leave event and add them in the enter. The forms don't actually deactivate but rather just lose focus to another form that is open so I don't want to use the Activate/deactivate unless that doesn't act like closing the form.
 
Ok, here's a variant of the ToolStripManager you're so fond of ;) This code is only for MdiParent, no TSM code in childs at all. It merges the active childs toolstrip into the parent (set child toolstrip Visible=False in designer so the empty strip won't show). In case a child is deactivated or closed any previous merge is reverted.
VB.NET:
Private Sub MDIParent1_MdiChildActivate(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.MdiChildActivate
    ToolStripManager.RevertMerge(ParentToolStrip)
    If Me.ActiveMdiChild IsNot Nothing Then
        ToolStripManager.Merge(Me.ActiveMdiChild.Controls("ChildToolStrip"), ParentToolStrip)
    End If
End Sub
This could also be a good event to set a parent item Enabled=False if you don't want a replace/remove merge but specifically want to display the item as disabled. Enable/disable child form items in the child forms Activated/Deactivate events.

A few tips; MergeAction/MergeIndex is set for source items, these settings have no effect for target items. Either the Text (precedence) or the MergeIndex determines a match, even if the text if not displayed (DisplayStyle Image). To remove a target item after the merge let a source item Replace it or Remove it(+self), a dummy source item can also be used for this purpose just to remove something from target.
 
Back
Top