Set both Parent and Child item as "Selected" in asp:Menu - c#

So i am trying to do the following in order to style (StaticSelectedStyle-CssClass and DynamicSelectedStyle-CssClass) the selected child item AND its parent. The following code causes only the parent to style:
private bool SetSelectedMenuItem(MenuItemCollection menuItems)
{
foreach (MenuItem item in menuItems)
{
string url = ResolveUrl(item.NavigateUrl);
string pageUrl = Request.RawUrl;
if (pageUrl.Equals(url))
{
item.Selected = true;
if (item.Parent != null)
{
item.Parent.Selected = true;
}
return true;
}
if (SetSelectedMenuItem(item.ChildItems))
{
return true;
}
}
return false;
}
Is it even possible to have a child and its parent item to have their selected value set to true?
Any help to get this working would be great!

It's not possible. Take a look at documentation on the Selected property. The last line of the Remarks section says this:
Only one menu item can be selected at a time in the Menu control.

Related

How to access items inside sub menu item in C# windows form application in ToolStripMenuItems?

There's a toolstripmenuitem in my Windows form Application.
I need to access every sub menu items and check whether there is a specific menu item name is available and if that item found, I want to disable it.
Eg:
Report
|__Stock
| |__Stock Balance
| |__Stock Reorder
|__Sales
|__Summary
My code is like this. According to my code, I can access sub menu(Stock) and disable it. But I'm unable to access child items(Stock Balance) inside sub menu.
String specificMenuItemName = "Stock Balance";
foreach (ToolStripMenuItem menuItem in MainMenuStrip.Items)
{
if (menuItem != null)
{
if (menuItem.HasDropDownItems)
{
foreach (ToolStripItem subMenuItem in menuItem.DropDownItems)
{
if (subMenuItem is ToolStripSeparator)
{ }
else
{
if (specificMenuItemName == subMenuItem.Text)
{
subMenuItem.Enabled = false;
}
}
}
}
}
}
How do I access to Stock Balance and disable it?
What about a recursive function that walks down every item that has drop-down items until it finds the one with the specified name? something like this (quick-and-dirty, skipped checking for separators and stuff like that...):
private static void DisableItem(ToolStripDropDownItem menu, bool enable, string text)
{
if (!menu.HasDropDownItems)
if (Equals(menu.Text, text))
menu.Enabled = enable;
else
return;
foreach(var subItem in menu.DropDownItems)
{
var item = subItem as ToolStripDropDownItem;
if (item == null) continue;
if (item.HasDropDownItems)
DisableItem(item, enable, text);
if (Equals(item.Text, text))
item.Enabled = enable;
}
}

PredictFocus() not working for next

I have created a WPF application . I need to identify Next focusable element . For that I have added following code.
UIElement elementWithFocus = System.Windows.Input.Keyboard.FocusedElement as UIElement;
var a = elementWithFocus.PredictFocus(FocusNavigationDirection.Next);
But it is showing that Next is not supported. How I can achieve the same?
Try to list all the controls on your window and search for the one which has it's TabIndex property equals to the current TabIndex property + 1
private UIElement SearchNextControl(int currentTabIndex)
{
foreach (UIElement element in AllTheControls)
{
if(element.TabIndex == (currentTabIndex + 1))
{
return element;
}
}
return null;
}

How to disable menu items?

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.

Select TreeView Node

I've a TreeNode and I need to allow user to select only one child node for parent.
Example:
-Car
---Ferrari
---Lamborghini
---Porsche
-Shoes
---Nike
---Puma
---Adidas
I can select "Ferrari" and "Nike", but not other child in "Car" or "Shoes". How can I make it?
After I do this, I need to concat text of Parent and child like this: Car: Ferrari.
Can you help me?
Regards.
You could handle the BeforeCheck event and clear the siblings checkboxes, e.g. :
private bool skipEvents = false; // this flag to avoid infinite recursion
void treeView1_BeforeCheck(object sender, TreeViewCancelEventArgs e)
{
// if is a root (car or shoes), or it's a recursive call, just return
if (skipEvents || e.Node.Parent == null)
return;
skipEvents = true;
foreach (TreeNode sibling in e.Node.Parent.Nodes)
{
// set the other siblings to unchecked
if (sibling != e.Node)
sibling.Checked = false;
}
skipEvents = false;
}
Here's an example to concatenate the parents and childs selected:
public string GetSelectionString()
{
string categorySep = Environment.NewLine;
string parentChildSep = " : ";
StringBuilder sb = new StringBuilder();
foreach (TreeNode root in this.treeView1.Nodes)
{
foreach (TreeNode node in root.Nodes)
{
if (node.Checked)
{
if (sb.Length > 0)
sb.Append(categorySep);
sb.Append(root.Text);
sb.Append(parentChildSep);
sb.Append(node.Text);
break;
}
}
}
return sb.ToString();
}
for example if Ferrari and Puma are selected, it returns a string like this:
Car : Ferrari
Shoes : Puma
EDIT as per comment:
This code does what you ask in your comment (selection/deselection of parents children):
private bool skipEvents = false; // this flag to avoid infinite recursion
void treeView1_BeforeCheck(object sender, TreeViewCancelEventArgs e)
{
// if it's a recursive call, just return
if (skipEvents)
return;
skipEvents = true;
// it's a root (e.g. car or shoes)
if (e.Node.Parent == null)
{
// if root node is going to be checked, just cancel the action (i.e. block parent selection)
if (!e.Node.Checked)
{
e.Cancel = true;
}
}
else
{
foreach (TreeNode sibling in e.Node.Parent.Nodes)
{
// set the other siblings to unchecked
if (sibling != e.Node)
sibling.Checked = false;
}
}
skipEvents = false;
}
void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
// if it's a recursive call, just return
if (skipEvents)
return;
this.skipEvents = true;
// it's a root (e.g. car or shoes)
if (e.Node.Parent == null)
{
// root node has been unchecked, so uncheck the children
if (!e.Node.Checked)
{
foreach (TreeNode child in e.Node.Nodes)
child.Checked = false;
}
}
else
{
// if a child node has been checked --> check the parent
// otherwise, uncheck the parent
e.Node.Parent.Checked = e.Node.Checked;
}
this.skipEvents = false;
}
N.B.
TreeView class has a known bug that arises in Vista/Windows7 concerning checkboxes.
Basically if you double-click a checkbox it doesn't lauch any event, so this management will be compromised.
To solve this issue, you can disable double-click by using the class explained in this post instead of TreeView.
If you need one selection per tree, I would suggest using two TreeViews. I would also question whether or not you need to be using TreeViews or whether two ListBoxes or ComboBoxes might be more appropriate.
If you don't know how many 'trees' you'll have, but you do know how deep they are, you could use two or more ListBoxes (or ListViews) to display basically a list of lists:
Categories:
Shoes: Nike
Cars: Ferrari
Fruits: Apple (selected)
Selected Category (Fruits):
Apple (selected)
Orange
Pear
Kiwi

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

Categories