How to disable menu items? - c#

I had a problem about Menu. I add a MenuItem in asp.net. I'm using c#. I want to disable Menu Parents and Children with user's permission.
There are 3 users which are "User","Power-User" and "BT_User". "User" have worst permission and "BT_User" have best.
How can I do that? Can anyone answer me?

Implemente your logic from here..
if(UserType == "Power-User")
{
MenuItem mnuItem = Menu1.FindItem("MenuOption"); // If delete a specific item
//to remove
Menu1.Items.Remove(mnuItem);
//to disable and not remove
mnuItem.Enabled = false;
}
if (UserType == "BT_User")
{
Your other logic
}

try this
if (UserType == "Power-User")
{
Menu1.Items.Find("MenuToDelete1", true)[0].Enabled = false;
Menu1.Items.Find("MenuToDelete2", true)[0].Enabled = false;
//or
Menu1.Items.Remove(Menu1.FindItem("MenuToDelete"));
Menu1.Items.Remove(Menu1.FindItem("MenuToDelete2"));
}
if (UserType == "BT_User")
{
Menu1.Items.Find("DeletedItem1", true)[0].Enabled = true;
Menu1.Items.Find("DeletedItem2", true)[0].Enabled = true;
MenuItem item1 = new MenuItem();
item.Text = "DeletedItem1";
MenuItem item2 = new MenuItem();
item.Text = "DeletedItem2";
//or
menuStrip1.Items.Insert(index1, item1);
menuStrip1.Items.Insert(index2, item2);
}

Just leverage the Enabled property of the MenuItem. There is not enough information to tell you how to build the logic around it, but when you want to disable one just do this:
menuItem.Enabled = false;
One thing to note here is that you wouldn't need to continue disabling any children when you disable a parent because with Enabled set to false it's not going to be allowed to fly out any children.
From the MSDN documentation for the Enabled property:
Gets or sets a value that indicates whether the MenuItem object is enabled, allowing the item to display a pop-out image and any child menu items.

Related

Xamarin Navigation Bar Hide Hamburger Menu

I need to hide the hamburger menu on certain pages but still display information in then navbar. I don’t know of any way to accomplish this.
Also, I need the navbar to stay fixed to the top of the screen but it’s getting cut off when the keyboard pops up.
How can I go about this?
FlyoutPage.ShouldShowToolbarButton method is used to determine whether to show/hide hamburger icon , and it is triggered every time when selecting pages.
We can define a bool field ,change its value when directing to specific pages.
FlyoutPage
public override bool ShouldShowToolbarButton()
{
return showIcon;
}
private bool showIcon = true;
private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var item = e.SelectedItem as FlyoutPage1FlyoutMenuItem;
if (item == null)
return;
var page = (Page)Activator.CreateInstance(item.TargetType);
page.Title = item.Title;
Detail = new NavigationPage(page);
IsPresented = false;
FlyoutPage.ListView.SelectedItem = null;
//add this logic
showIcon = (item.Id == 1) ? false : true; //only the second page do not show hamburger icon
}

How to highlight node in TreeView ? Not only text area, but all row

TreeNode.Select() doesn't work. I want it to be highlighted like
All that I have is
You can use TreeView.FullRowSelect property for this. But remember, it is ignored if ShowLines is set to true.
TreeView.FullRowSelect Property
Gets or sets a value indicating whether the selection highlight spans the width of the tree view control.
public class CustomizedTreeView : TreeView
{
public CustomizedTreeView()
{`enter code here`
// Customize the TreeView control by setting various properties.
BackColor = System.Drawing.Color.CadetBlue;
FullRowSelect = true;
HotTracking = true;
Indent = 34;
ShowPlusMinus = false;
// The ShowLines property must be false for the FullRowSelect
// property to work.
ShowLines = false;
}
protected override void OnAfterSelect(TreeViewEventArgs e)
{
// Confirm that the user initiated the selection.
// This prevents the first node from expanding when it is
// automatically selected during the initialization of
// the TreeView control.
if (e.Action != TreeViewAction.Unknown)
{
if (e.Node.IsExpanded)
{
e.Node.Collapse();
}
else
{
e.Node.Expand();
}
}
// Remove the selection. This allows the same node to be
// clicked twice in succession to toggle the expansion state.
SelectedNode = null;
}
}
follow this link
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.treeview.padding?view=netframework-4.7.2

How to disable only the checkboxes (and not the full checkedboxlist) of devexpress?

In non-edit mode, I need the user to be able to browse the list (scroll etc.) but not be able to select the checkboxes.
If I do checkedboxlist.enabled = false, then the whole list becomes disabled. Only I need to disable checkboxes so that user doesn't interact (in edit way) in non-edit mode.
EDIT
I just assign the list of strings to the checkedboxlist's datasource.
this.UserSelectedMsgTypes.DataSource = userSelectedMsgs;
this.UserAvailableMsgTypes.DataSource = availableMsgTypeList;
currently enabling/disabling whole list by doing
this.UserSelectedMsgTypes.Enabled = true/false;
this.UserAvailableMsgTypes.Enabled = true/false;
I tried #James solution earlier, doesnt work. Because somehow the 'ItemCount' is 0 even though there are items. in the datasource it shows that there are 6 items, but in list it shows 0.
Try this:
foreach (DevExpress.XtraEditors.Controls.CheckedListBoxItem item in checkedListBoxControl1.Items)
{
item.Enabled = false;
}
It's a bit of a dirty work around but how about this?
private IEnumerable<DevExpress.XtraEditors.Controls.CheckedListBoxItem> GetCheckItems(string[] myStringArray)
{
foreach(string s in myStringArray)
{
DevExpress.XtraEditors.Controls.CheckedListBoxItem item = new DevExpress.XtraEditors.Controls.CheckedListBoxItem();
item.Description = s;
yield return item;
}
}
Call with:
checkedListBoxControl1.Items.AddRange(GetCheckItems(new string[] {"test1","test2","test3"}).ToArray());
Then apply the first answer with the foreach loop (or set the ENabled = false in the GetCheckItems method).

Get the ContextMenu that a ToolStripDropDownItem belongs to?

How can I get ContextMenu that a ToolStripDropDownItem belongs to? This is for the purpose of using the ContextMenu.SourceControl as the logical sender to an event.
`ToolStripItem.OwnerItem`
[http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripitem.owneritem.aspx][1]
That property can be used to walk up the tree of menu items to the top level item... Isn't what you are after just the Owner property of the menu?
var control = ((ContextMenuStrip)topLevelMenuItem.Owner).SourceControl;
Obviously use as etc and do your null checks...
If I am missing the spot perhaps post a code snippet of the menu built via code to clarify the types (ContextMenu vs ContextMenuStrip etc)
PK :-)
I needed to use the Owner property as a ContextMenuStrip.
ToolStripDropDownItem t = sender as ToolStripDropDownItem;
if (t == null)
return null;
ContextMenuStrip cm = t.Owner as ContextMenuStrip;
if (cm == null)
return null;
return cm.SourceControl;

Radio menu check

Using Winform, is it possible to "radio check" a menu item?
Yes see MSDN
// This method is called from the constructor of the form to set up the menu items.
public void ConfigureMyMenus()
{
/* Set all of these menu items to Radio-Button check marks so the user can see
that only one color can be selected at a time. */
menuItemRed.RadioCheck = true;
menuItemBlue.RadioCheck = true;
menuItemGreen.RadioCheck = true;
}

Categories