Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 726
  • Last Modified:

Highlight buttons on a menu

I have an MDI form with buttons on a menu that open child forms. Works great. How could I highlight in some way the button for the particular form that is open so users can see which form is open.
0
wiswalld
Asked:
wiswalld
  • 19
  • 13
  • 3
  • +1
1 Solution
 
ColemssCommented:
ON mdiactivate change the button borderstyle
0
 
jpaulinoCommented:
>> How could I highlight

What do you mean to highlight ? Change the backcolor ?

Are you using Toolstrip ?
0
 
wiswalldAuthor Commented:
>>ON mdiactivate change the button borderstyle

Give me a little more on this. Not sure what you mean.




>>What do you mean to highlight ? Change the backcolor ?
Yes any type of change in the button would work. Yes a toolstrip.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
wiswalldAuthor Commented:
This is the code on one of the buttons:

For Each child As Form In Me.MdiChildren
            child.Close()
        Next

        mdlOpenMDIChild.LoadForm(Me, "Blotter")
0
 
ColemssCommented:
A form has event that are fired when it is activated created mouseenter leave etc.

You can use the events to have a button change the way it looks at the appropriate time.
Look at the properties of the toolstrip buttons.  They have back color bordersyles etc .  Store the current value then change it on form enter activate or what ever event works. then change it back on form exit.
0
 
wiswalldAuthor Commented:
Me.ToolStripButton1.BackColor.Red??????
0
 
wiswalldAuthor Commented:
OK I got the code to change a button, but I actually have six buttons and if I click one button and open a form when I click the next button which closes the first form and opens the second the first button stays the same. I could use a lot of code to fix this but there has to be a simpler way. I am using

ToolStripButton1.BackColor = Color.Red

I know I could put something like

ToolStripButton1.BackColor = Color.Red
ToolStripButton2.BackColor = Color.Blue
ToolStripButton2.BackColor = Color.Blue
and so on to change all buttons to the color I want but seems like a lot of work.
0
 
ColemssCommented:
   Private Sub ToolStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles ToolStrip1.ItemClicked
        'Dim tsb As ToolStripButton = CType(sender, ToolStripButton)
        For Each t As ToolStripButton In ToolStrip1.Items
            t.BackColor = Color.Gray
        Next

    End Sub

    'link all toolstrip click events here
    Private Sub ToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click, ToolStripButton2.Click
        Dim tsb As ToolStripButton = CType(sender, ToolStripButton)
        tsb.BackColor = Color.Aqua

    End Sub
0
 
wiswalldAuthor Commented:
Sorry been out of service for a while broke my ankle. I get the following error:

Unable to cast object of type 'System.Windows.Forms.ToolStripSeparator' to type 'System.Windows.Forms.ToolStripButton'.

on the first line:

For Each t As ToolStripButton In ToolStrip1.Items              <<<<<<here>>>>
            t.BackColor = Color.Gray
        Next

        For Each child As Form In Me.MdiChildren
            child.Close()
        Next

        mdlOpenMDIChild.LoadForm(Me, "Blotter")
0
 
ColemssCommented:
The code I posted was actually running.

Are you sure ToolStrip1 is a toolstrip object?
0
 
wiswalldAuthor Commented:
yes it is a toolstrip
0
 
ColemssCommented:
Does it even start looping?
0
 
wiswalldAuthor Commented:
The form loads with no errors but as soon as I click the button I get the error posted above. I have put this in the click event for the button and maybe that is wrong.

Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Dim tsb As ToolStripButton = CType(sender, ToolStripButton)
        For Each t As ToolStripButton In ToolStrip1.Items
            t.BackColor = Color.Gray
        Next

        For Each child As Form In Me.MdiChildren
            child.Close()
        Next

        mdlOpenMDIChild.LoadForm(Me, "Blotter")
    End Sub
0
 
wiswalldAuthor Commented:
OK I think I had the code in the wrong place. I figured the first part, but where does the second part go. Does it go in the click for all the buttons?
0
 
wiswalldAuthor Commented:
Does anyone else ever get the stupid error

No question specified

when they try to post
0
 
ColemssCommented:
Yes the click for all the buttons
0
 
wiswalldAuthor Commented:
Private Sub ToolStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles ToolStrip1.ItemClicked
        Dim tsb As ToolStripButton = CType(sender, ToolStripButton)
        For Each t As ToolStripButton In ToolStrip1.Items
            t.BackColor = Color.Gray
        Next
    End Sub



Unable to cast object of type 'System.Windows.Forms.ToolStripSeparator' to type 'System.Windows.Forms.ToolStripButton'.
0
 
ColemssCommented:
K look at this.  I think you have the work event hooked up.

Link the toolstrivk click event here. It basically resets all the buttons back to the default color.
   Private Sub ToolStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles ToolStrip1.ItemClicked
        'Dim tsb As ToolStripButton = CType(sender, ToolStripButton)
        For Each t As ToolStripButton In ToolStrip1.Items
            t.BackColor = Color.Gray
        Next

    End Sub

 Link all Button click events here.  When you click the button it changes the color
    Private Sub ToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click, ToolStripButton2.Click
        Dim tsb As ToolStripButton = CType(sender, ToolStripButton)
        tsb.BackColor = Color.Aqua

    End Sub
0
 
wiswalldAuthor Commented:
This is killing me. I must be doing something wrong. Here is the code for the item clicked and the first two bnuttons:


Private Sub ToolStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles ToolStrip1.ItemClicked

        Dim tsb As ToolStripButton = CType(sender, ToolStripButton)
        For Each t As ToolStripButton In ToolStrip1.Items
            t.BackColor = Color.Gray
        Next

    End Sub




Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click

        Dim tsb As ToolStripButton = CType(sender, ToolStripButton)
        tsb.BackColor = Color.Aqua

       For Each child As Form In Me.MdiChildren
            child.Close()
        Next

        mdlOpenMDIChild.LoadForm(Me, "Blotter")
    End Sub





    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click

        Dim tsb As ToolStripButton = CType(sender, ToolStripButton)
        tsb.BackColor = Color.Aqua

   


        For Each child As Form In Me.MdiChildren
            child.Close()
        Next
        mdlOpenMDIChild.LoadForm(Me, "Form1")
    End Sub
0
 
wiswalldAuthor Commented:
I upped the points
0
 
ColemssCommented:
Ahhh I think I see now.

  'Dim tsb As ToolStripButton = CType(sender, ToolStripButton)
was actually commented out in my code...
Remove the line.
0
 
wiswalldAuthor Commented:
No I caught the comment and removed. Should I still remove the line?
0
 
ColemssCommented:
Yeah it was commented out. I had it there because I was going to cast the send to a toolbar.  

Remove the line
0
 
wiswalldAuthor Commented:
Unable to cast object of type 'System.Windows.Forms.ToolStripSeparator' to type 'System.Windows.Forms.ToolStripButton'.

For Each t As ToolStripButton In ToolStrip1.Items        <<<error here>>>
            t.BackColor = Color.Gray
        Next

0
 
ColemssCommented:
Put a break on the for each.
does it ever get inside the loop?
0
 
wiswalldAuthor Commented:
You lost me
0
 
ColemssCommented:
put a break point one this line

t.BackColor = Color.Gray

I want to know if ever gets inside the loop
0
 
wiswalldAuthor Commented:
made it to the breakpoint
0
 
ColemssCommented:
Found your error in a c# thing see if this applies.

I have this method in the Parent form of an MDI app.


private void UpdateMenuItems()
{
foreach (MdiMenuItem item in m_menuMain.Items)
{
if (item != null)
{
item.UpdateMenuItem();
}
}
}

Where MdiMenuItem = ToolStripMenuItem
and m_menuMain = MenuStrip

When I open a file from my recent file list and call this I get
"Unable to cast object of type 'SystemMenuItem' to type 'ToolStripMenuItem'"

Any idea why I would get this?

It works in unconverted app if MdiMenuItem = MenuItem and
m_menuMain = MainMenu

Also when I add a Separator to the menu it changes my code from
this.m_menuMain.Items.AddRange(new System.Windows.Forms.ToolStripMenuItem[] {
to
this.m_menuMain.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {

---------POst 2
Thank you! It helped to have someone blaze a trail ahead. Here are a few additional things to look for that I ran into.

1) The old separators were a ToolBarButton with their Style set to ToolBarButtonStyle.Separator.
2) The following properties don't exist that existed for ToolBar.
ToolStrip.Appearance
ToolStrip.TextAlign
ToolStrip.ButtonDropDown
3) Had to convert ToolBarButton with Style set to ToolBarButtonStyle.DropDownButton to ToolStripSplitButton.
4) If you want dockable toolbars, you have to convert the code to use a ToolStripContainer and put the ToolStrips in its ToolStripPanels.
0
 
ColemssCommented:
I think this is the hardest I've ever worked for so little points.
0
 
Bob LearnedCommented:
How about toggling the Me.ToolStripButton1.Checked property?

Bob
0
 
wiswalldAuthor Commented:
How about toggling the Me.ToolStripButton1.Checked property?


????????
0
 
Bob LearnedCommented:
When you set the Checked property, and the button is drawn with a visual style, it will highlight the button for.  You should also set the CheckOnClick = True, so that the button will toggle between checked and not checked.

Bob
0
 
wiswalldAuthor Commented:
Gotcha. That will highlight, but how do I uncheck when it is closed?
0
 
Bob LearnedCommented:
You can attach an event handler to the FormClosing event for the form, and change the button Checked value to False.

Bob
0
 
wiswalldAuthor Commented:
Simple. Thanks.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 19
  • 13
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now