This treeview class works differently from the windows forms class. Every example I see to change selected node uses items.selectednode or nodes.selectednode.
However, I am struggling to find a method of doing this for this class:
https://msdn.microsoft.com/en-us/library/system.windows.controls.treeview(v=vs.110).aspx
I wanted to enable right click to popup my context menu. Could not find another solution, so here's what I did, and it works:
private void TreeSetup_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
((TreeViewItem)sender).IsSelected = true;
e.Handled = true;
}
private void TreeSetup_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
ContextMenu PopupMenu = this.FindResource("cmButton") as ContextMenu;
if (TreeSetup.SelectedItem != null)
{
PopupMenu.PlacementTarget = sender as TreeViewItem;
PopupMenu.IsOpen = true;
}
}
Related
I have a ContextMenuStrip that is assigned to several different listboxes. I am trying to figure out when the ContextMenuStrip is clicked what ListBox it was used on. I tried the code below as a start but it is not working. The sender has the correct value, but when I try to assign it to the menuSubmitted it is null.
private void MenuViewDetails_Click(object sender, EventArgs e)
{
ContextMenu menuSubmitted = sender as ContextMenu;
if (menuSubmitted != null)
{
Control sourceControl = menuSubmitted.SourceControl;
}
}
Any help would be great. Thanks.
Using the assistance below, I figured it out:
private void MenuViewDetails_Click(object sender, EventArgs e)
{
ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
if (menuItem != null)
{
ContextMenuStrip calendarMenu = menuItem.Owner as ContextMenuStrip;
if (calendarMenu != null)
{
Control controlSelected = calendarMenu.SourceControl;
}
}
}
For a ContextMenu:
The problem is that the sender parameter points to the item on the context menu that was clicked, not the context menu itself.
It's a simple fix, though, because each MenuItem exposes a GetContextMenu method that will tell you which ContextMenu contains that menu item.
Change your code to the following:
private void MenuViewDetails_Click(object sender, EventArgs e)
{
// Try to cast the sender to a MenuItem
MenuItem menuItem = sender as MenuItem;
if (menuItem != null)
{
// Retrieve the ContextMenu that contains this MenuItem
ContextMenu menu = menuItem.GetContextMenu();
// Get the control that is displaying this context menu
Control sourceControl = menu.SourceControl;
}
}
For a ContextMenuStrip:
It does change things slightly if you use a ContextMenuStrip instead of a ContextMenu. The two controls are not related to one another, and an instance of one cannot be casted to an instance of the other.
As before, the item that was clicked is still returned in the sender parameter, so you will have to determine the ContextMenuStrip that owns this individual menu item. You do that with the Owner property. Finally, you'll use the SourceControl property to determine which control is displaying the context menu.
Modify your code like so:
private void MenuViewDetails_Click(object sender, EventArgs e)
{
// Try to cast the sender to a ToolStripItem
ToolStripItem menuItem = sender as ToolStripItem;
if (menuItem != null)
{
// Retrieve the ContextMenuStrip that owns this ToolStripItem
ContextMenuStrip owner = menuItem.Owner as ContextMenuStrip;
if (owner != null)
{
// Get the control that is displaying this context menu
Control sourceControl = owner.SourceControl;
}
}
}
Older post, but in case someone like myself comes across it:
For a ContextMenuStrip, the above didn't work for me, but it led to finding what did.
void DeleteMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
ContextMenuStrip menu = sender as ContextMenuStrip;
Control sourceControl = menu.SourceControl;
MessageBox.Show(sourceControl.Name);
}
This gave me the expected control's name. You can put in validation etc with if statements, I'm just posting to get to the point.
I had great difficulty getting any of this code to work. This is the simplest solution I could find:
For A ContextMenuStrip:
Control _sourceControl = null;
private void contextMenuStrip_Opened(object sender, EventArgs e)
{
_sourceControl = contextMenuStrip.SourceControl;
}
private void contextMenuItem_Click(object sender, EventArgs e)
{
var menuItem = (ToolStripMenuItem)sender;
_sourceControl.Text = menuItem.Text;
MessageBox.Show(menuItem.Name);
MessageBox.Show(sourceControl.Name);
}
Cast sender to ToolStripItem to reach Owner which will be a ToolStrip that doesn't have a SourceControl property.
Cast Owner to ContextMenuStrip to reach SourceControl.
Control sc = ((ContextMenuStrip)((ToolStripItem)sender).Owner).SourceControl;
How about just using ActiveForm.ActiveControl, in this example from a C1 grid:
C1.Win.FlexGrid.C1FlexGrid fg = frmMain.ActiveForm.ActiveControl as C1.Win.FlexGrid.C1FlexGrid;
The easiest solution would be:
Control parentControl = ((sender as MenuItem).GetContextMenu()).SourceControl;
How can I return to the last used RibbonTab that was in focus when the window was last closed?
You could create a variable to hold the reference to the RibbonTab and listen to the SelectionChanged event on your Ribbon object.
MyRibbonObj.SelectionChanged += delegate(object sender, SelectionChangedEventArgs args)
{
RibbonTab rt = ((sender as Ribbon).SelectedItem as RibbonTab);
MyReferenceToRibbonTab = rt;
}
This way you keep track of the latest selected RibbonTab within your Ribbon.
PS: code might needs tweaks. I didn't test it.
Create a setting LastRibbonTab
Save the last tab used in MainWindow_Closed
MainWindow_Closed(object sender, EventArgs e)
{
Properties.Settings.Default.LastRibbonTab = (MyRibbon.SelectedItem as RibbonTab).Header.ToString();
Properties.Settings.Default.Save();
}
Select the last tab in MainWindow_Loaded
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Init();
foreach (RibbonTab r in MyRibbon.Items)
if (r.Header.ToString() == Properties.Settings.Default.LastRibbonTab)
{
MyRibbon.SelectedItem = r;
break;
}
}
When I look it up, they list it as having a .Checked property. But both in Visual Studio and on msdn, it doesn't list any kid of Checked property.
ContextMenuStrip menu = new ContextMenuStrip ( );
var menuItem = menu.Items.Add ( "CheckedItem" );
//menuItem.Checked?
Is there a way to do this?
You need to cast to ToolStripMenuItem:
((ToolStripMenuItem)menuItem).Checked = true;
I had 2 checked items on or off, so I used this format:
private void onToolStripMenuItem_Click(object sender, EventArgs e)
{
offToolStripMenuItem.Checked = false;
}
private void offToolStripMenuItem_Click(object sender, EventArgs e)
{
onToolStripMenuItem.Checked = false;
}
This code will change StripMenuItem checked state after every mouse click.
Note: Tool Strip menu item name is: uruchomZSystememToolStripMenuItem
private void uruchomZSystememToolStripMenuItem_Click(object sender, EventArgs e)
{
uruchomZSystememToolStripMenuItem.Checked = !uruchomZSystememToolStripMenuItem.Checked;
}
I have a ContextMenuStrip that is assigned to several different listboxes. I am trying to figure out when the ContextMenuStrip is clicked what ListBox it was used on. I tried the code below as a start but it is not working. The sender has the correct value, but when I try to assign it to the menuSubmitted it is null.
private void MenuViewDetails_Click(object sender, EventArgs e)
{
ContextMenu menuSubmitted = sender as ContextMenu;
if (menuSubmitted != null)
{
Control sourceControl = menuSubmitted.SourceControl;
}
}
Any help would be great. Thanks.
Using the assistance below, I figured it out:
private void MenuViewDetails_Click(object sender, EventArgs e)
{
ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
if (menuItem != null)
{
ContextMenuStrip calendarMenu = menuItem.Owner as ContextMenuStrip;
if (calendarMenu != null)
{
Control controlSelected = calendarMenu.SourceControl;
}
}
}
For a ContextMenu:
The problem is that the sender parameter points to the item on the context menu that was clicked, not the context menu itself.
It's a simple fix, though, because each MenuItem exposes a GetContextMenu method that will tell you which ContextMenu contains that menu item.
Change your code to the following:
private void MenuViewDetails_Click(object sender, EventArgs e)
{
// Try to cast the sender to a MenuItem
MenuItem menuItem = sender as MenuItem;
if (menuItem != null)
{
// Retrieve the ContextMenu that contains this MenuItem
ContextMenu menu = menuItem.GetContextMenu();
// Get the control that is displaying this context menu
Control sourceControl = menu.SourceControl;
}
}
For a ContextMenuStrip:
It does change things slightly if you use a ContextMenuStrip instead of a ContextMenu. The two controls are not related to one another, and an instance of one cannot be casted to an instance of the other.
As before, the item that was clicked is still returned in the sender parameter, so you will have to determine the ContextMenuStrip that owns this individual menu item. You do that with the Owner property. Finally, you'll use the SourceControl property to determine which control is displaying the context menu.
Modify your code like so:
private void MenuViewDetails_Click(object sender, EventArgs e)
{
// Try to cast the sender to a ToolStripItem
ToolStripItem menuItem = sender as ToolStripItem;
if (menuItem != null)
{
// Retrieve the ContextMenuStrip that owns this ToolStripItem
ContextMenuStrip owner = menuItem.Owner as ContextMenuStrip;
if (owner != null)
{
// Get the control that is displaying this context menu
Control sourceControl = owner.SourceControl;
}
}
}
Older post, but in case someone like myself comes across it:
For a ContextMenuStrip, the above didn't work for me, but it led to finding what did.
void DeleteMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
ContextMenuStrip menu = sender as ContextMenuStrip;
Control sourceControl = menu.SourceControl;
MessageBox.Show(sourceControl.Name);
}
This gave me the expected control's name. You can put in validation etc with if statements, I'm just posting to get to the point.
I had great difficulty getting any of this code to work. This is the simplest solution I could find:
For A ContextMenuStrip:
Control _sourceControl = null;
private void contextMenuStrip_Opened(object sender, EventArgs e)
{
_sourceControl = contextMenuStrip.SourceControl;
}
private void contextMenuItem_Click(object sender, EventArgs e)
{
var menuItem = (ToolStripMenuItem)sender;
_sourceControl.Text = menuItem.Text;
MessageBox.Show(menuItem.Name);
MessageBox.Show(sourceControl.Name);
}
Cast sender to ToolStripItem to reach Owner which will be a ToolStrip that doesn't have a SourceControl property.
Cast Owner to ContextMenuStrip to reach SourceControl.
Control sc = ((ContextMenuStrip)((ToolStripItem)sender).Owner).SourceControl;
How about just using ActiveForm.ActiveControl, in this example from a C1 grid:
C1.Win.FlexGrid.C1FlexGrid fg = frmMain.ActiveForm.ActiveControl as C1.Win.FlexGrid.C1FlexGrid;
The easiest solution would be:
Control parentControl = ((sender as MenuItem).GetContextMenu()).SourceControl;
I'm using a ListView control with multirow and fullrow select on. When I'm selecting multiple rows at once, some of my rows magically become checked. This happens when dragging the mouse over and also when selecting one, and shift clicking another.
See image describing issue here:
What in the grapefruit is going on? Anyone?
Unfortunately there are bugs in the ListView class, this is one of them. The following code is a fix that worked for me.
Edit: Sorry, this doesn't work quite right, although it does prevent the error that you show in your question. This prevents selecting multiple items and then checking them by clicking the check box.
void SetupListView()
{
listView.ItemCheck += new ItemCheckEventHandler(listView_ItemCheck);
listView.MouseDown += new MouseEventHandler(listView_MouseDown);
listView.MouseUp += new MouseEventHandler(listView_MouseUp);
listView.MouseLeave += new EventHandler(listView_MouseLeave);
}
bool mouseDown = false;
void listView_MouseLeave(object sender, EventArgs e)
{
mouseDown = false;
}
void listView_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
void listView_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
}
void listView_ItemCheck(object sender, ItemCheckEventArgs e)
{
if(mouseDown)
{
e.NewValue = e.CurrentValue;
}
}
I answered this in another forum:
http://www.codeproject.com/Messages/3417741/Re-how-to-disable-multi-select-of-checkbox-in-list.aspx
HTH - hground
it`s simple question
just try this
private void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (ModifierKeys == Keys.Control || ModifierKeys == Keys.Shift)
{
e.NewValue = e.CurrentValue;
}
}