Solved

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

Posted on 2006-07-20
11
998 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_
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:lapucca
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 3

Expert Comment

by:BBK_
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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_
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now