Solved

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

Posted on 2006-07-20
11
1,089 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
Technology Partners: 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!

 

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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

685 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