Solved

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

Posted on 2006-07-20
11
1,023 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

22 Experts available now in Live!

Get 1:1 Help Now