Solved

How do I manage the checkonClick/uncheck on a menu item?

Posted on 2006-07-20
11
1,065 Views
Last Modified: 2013-12-03
I have a View menu item that offiers Large Icon, Details.....  views.  I would like to have the menu item have a checked sign when selected by a user and then remove the check sign when user selects a different view option.  Thank you.
0
Comment
Question by:lapucca
  • 6
  • 4
11 Comments
 
LVL 3

Expert Comment

by:BBK_
ID: 17151797
1)      Add a ContextMenu to the Form
2)      Define 4 List view types as 4 MenuItem
3)      Assign the ContectMeny Property of List view as the newly inserted Contextmenu
4)      Now on the Popup() event of context menu Check the “View” of list view .
5)      Using a Switch – case statement like
switch (listView1.View)
{
case View.LargeIcon : contextMenu1.MenuItems[0].Checked = true; break;
......Write 4 types here
}            
0
 

Author Comment

by:lapucca
ID: 17156157
There is a CheckOnClick property for the menu item that I turn on already.  It does have a check sign when I click on those item but  it doesn't un-check when I click on another item.   This is the problem that I need help with.  Thank you.
0
 
LVL 1

Expert Comment

by:flashaoy
ID: 17159560
Tried It out and worked fine.

private void m_SortItem_Click(object sender, EventArgs e)
{
//Clearing check marks from all menu items
foreach (ToolStripMenuItem item in sortToolBtn.DropDownItems)
{
item.Checked = false;
}

//Selected the current item which is clicked.
m_SortItem.Checked = true;
}
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:lapucca
ID: 17163959
I'm not sure where is this event?  I don't see anything like it on the menustrip control's events.  Where should I add thdcode?  Thanks.
0
 

Author Comment

by:lapucca
ID: 17164914
I think I should put this at        
private void viewToolStripMenuItem_Click(object sender, EventArgs e)

but, what is "sortToolBtn.DropDownItems"??

I need to do it for each of the View option item, Large Icon, Small Icon, List and Detail.

Thanks.
0
 
LVL 3

Expert Comment

by:BBK_
ID: 17165374
lapucca, I have worked out some thing for you.



            //On Popup of the Contextmenu
            private void contextMenu1_Popup(object sender, System.EventArgs e)
            {
                  Large.Checked = false;
                  List.Checked = false;
                  Details.Checked = false;      
                  Small.Checked = false;      
                  switch(listView1.View)
                  {
            case View.Details : {Details.Checked = true; break;}
            case View.LargeIcon:{Large.Checked = false; break;}
            case View.SmallIcon:{Small.Checked = true; break;}
            case View.List:      {List.Checked = true; break;}

                  }
            }

//This is the Click of menuItem
//To take this select the item and take the event list, Click on the "Click" event.
//Take each next menuitem and select the same event hadler from the dropdown

private void menuItem_Click(object sender, System.EventArgs e)
{
      Large.Checked = false;
      List.Checked = false;
      Details.Checked = false;      
      Small.Checked = false;
      ((MenuItem)sender).Checked = true;
      switch(((MenuItem)sender).Text)
      {
            case "Large": {listView1.View = View.LargeIcon; break;}
            case "List": {listView1.View = View.List; break;}
            case "Report": {listView1.View = View.Details; break;}
            case "Details": {listView1.View = View.SmallIcon; break;}
                  }
            }
0
 

Author Comment

by:lapucca
ID: 17165476
Hi BBK,

This is the main menu strip in my main form.  It's not a popup contextmenu.  Thank you.
0
 
LVL 3

Accepted Solution

by:
BBK_ earned 500 total points
ID: 17165559
Still the idea is same..

But if it is a main menu, then instead of assigning the Check at POPUP, you can take care it in program, like when the application starts the View Style will be LIST. So at that time make the List as checked, then the second part will take care...
I.e

private void menuItem_Click(object sender, System.EventArgs e)
{
     Large.Checked = false;
     List.Checked = false;
     Details.Checked = false;    
     Small.Checked = false;
     ((MenuItem)sender).Checked = true;
     switch(((MenuItem)sender).Text)
     {
          case "Large": {listView1.View = View.LargeIcon; break;}
          case "List": {listView1.View = View.List; break;}
          case "Report": {listView1.View = View.Details; break;}
          case "Details": {listView1.View = View.SmallIcon; break;}
               }
          }
0
 

Author Comment

by:lapucca
ID: 17170165
I still can get this to work becuase I don't know the exact property to use for the ToolStripMenu under the Menu strip.  The following is what I got but I am getting a casting error and I can't find the "checked" option.   Thanks.

        private void largeIconToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (ToolStripDropDownItem mi in viewToolStripMenuItem.DropDownItems)
                mi.DropDown = false;
            NodeDetail.Items["Large Icon"].Checked = true;
            NodeDetail.View = View.LargeIcon;
        }
0
 
LVL 3

Expert Comment

by:BBK_
ID: 17173543
I assume you are using NET Framework version 2.0.
Please refer the following link
http://msdn2.microsoft.com/en-us/library/system.windows.forms.toolstripdropdownitem.aspx
0
 

Author Comment

by:lapucca
ID: 17173590
Yes, I am using vs2005, .net 2.0.  I looked at the link you provided but I don't see what I'm looking for.  The largeIconToolStripMenuItem is the drop down menu from my mainmenu(menustrip).  I need to something like mi.checked to set it to false but I don't see it in the example you gave me.  Thank you.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question