ContextMenuStrip loses the focus from an item in the tree view if it is right clicked - winforms

I have a contextmenuStrip associated with a tree view.
Now for instance, i have four nodes in the tree structure and Node 4 is selected.
Behavior:
ContextMenuStrip - When u right click on the Node 2 , that node is selected and as soon as the context menu strip opens up, the focus goes back to Node 4.
With old component 'Context Menu' this functionality works fine i.e. Node 2 has the focus till the time context menu is open.
I would like to have Node 2 selected as long as context menu is open. And selection/focus shall go back to Node 4 when context menu is closed.
Request please advice.
Thanks and Best Regards
Sumit

Yes, the TreeView control is pretty flaky when the focus is changed while one of its events runs. Which is one reason it distinguishes the BeforeXxxx and AfterXxxx events. Unfortunately, the context menu strip is shown too soon. The solution is to display the context menu yourself by implementing the NodeMouseClick event. Like this:
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
if (e.Button == MouseButtons.Right) {
treeView1.SelectedNode = e.Node;
contextMenuStrip1.Show(treeView1, e.Location);
}
}
I'll leave restoring the focus afterwards up to you. It doesn't make much sense to implement it.

Related

WPF Exiting "Dropped" Combobox, Items no longer editable?

Friends,
I have a WPF Combobox. When the Combobox is opened, I have the items dynamically generated based upon environment variables. So basically a combobox that is bound to a list that is dynamically changing.
Everything works as expected until I Exit the combobox with the dropdown open to enter another control(another combobox).
When I reopen the first combobox, the items appear to be frozen and no longer bound to the list when INDEED the list is changing and is still bound. Its almost like the binding broke.
When this event occurs, I have attempted to forcefully add items, and that doesn't work either. I can see in the code behind that the combobox now contains the additional items, yet it doesn't appear that contain them in the UI.
What is this black magic? Any way to prevent it? is this some type of Stuck focus issue? Maybe the dropdown isn't re-sizing?
I think i have narrowed it down to the physical dropdown is not re-sizing to the new items.
EDIT*
The controls are dynamically generating, so I have no real hard code to show you other than this.
private void CBControl_DropDownOpened(object sender, EventArgs e)
{
((ComboBox)sender).Items.Add("Option");
}
On this event, I will add an items to the combobox, although the items ARE being added to the list, they are not displayed in the UI.
EDIT 2*
I figured it out, so i have 2 comboboxes, it appears that the 2nd was steeling and holding the focus INSIDE the dropdown. (odd bug)
in order to fix it i needed to release it bu changing the index of the 2nd combo box WHILE it is open.
int sel = ComboBoxTwo.SelectedIndex;
ComboBoxTwo.IsDropDownOpen = true;
ComboBoxTwo.SelectedIndex = -1;
ComboBoxTwo.IsDropDownOpen = true;
ComboBoxTwo.SelectedIndex = sel;
and I had to manage the unintended recursive call.
So, here it is.
You got 2 ComboBoxes. If you Open one then directly click into another, the focus is moved to the second combobox dropdown.
If you make any changes to the first combobox, the changes will not take effect to the style(dropdown resize) UNTIL you release the focus from the 2nd combo box dropdown item. Although the items change will take effect, the resize wont.
to release the focus, you need to open the second combox and change the selected index like so:
int sel = ComboBoxTwo.SelectedIndex;
ComboBoxTwo.IsDropDownOpen = true;
ComboBoxTwo.SelectedIndex = -1;
ComboBoxTwo.IsDropDownOpen = true;
ComboBoxTwo.SelectedIndex = sel;
10 hours of debugging. 5 hours of research. First solution I have found. It may be dirty, but its all I can find.

Adding items to below gridview when dropped on top gridview

On my main page, I have a button and telerik mainRadGridView. When I click my button, I get a popup which is divided it into two regions left and right.
On left, I have a RadTreeView and on right, I have a popupRadGridView.
Initially when my popup is open, the popupRadGridView is disabled. When I drop from left to right for first time in my popup nothing is added to popupRadGridView since it is disable which is correct.
But when I drop for second time on to my disabled popupRadGridView inside my opened popup, there is an element added to my mainRadGridView on my main page which is wrong.
I cannot understand how to stop adding onto my main page. Any help is greatly appreciated.
check for the e.Options.ParticipatingVisualRoots in Drop Event of GridView - should provide you the no of active opened child windows participating in the operation.
For additional information follow this link
Telerik Silverlight
I solved my issue as below
In my mainpage codebehind. I did like below
void mainRadGridView_OnDropInfo(object sender, DragDropEventArgs e)
{
if(e.Options.ParticipatingVisualRoots.Count > 0)
{
return;
}
else
{
do stuff;
}
}

WPF Tab_selectionChanged

I have this strange issue I am facing currently.
I have created an WPF application based on WPF page navigation. I have few button and depending on the button click the the user is navigated to respective WPF page.
In these WPF pages I have Tab controls and have used selectionchanged event handler to perform some task.
Now to the issue,
When I try to go a particular page, the selectionchanged event is also executed even before the page is loaded completely, I have tried to use the windows.loaded (based on the answer provided to my previous question - here) - I have no luck.
[I am using WPF Navigation framework]
Somehow the selectionchanged event is executing twice.
How do I stop this from happening?
I think you should check SelectionChange.AddedItems and SelectionChange.RemovedItems to find the difference between these to firings. I guess that when you select a page, SelectionChange.RemovedItems==0 while when you click on a tabItem to select it, SelectionChange.RemovedItems==1. if so just write:
if (SelectionChange.RemovedItems==0)
return;
Edit1: Please see the first comment.
Edit 2
void tablcontrol_SelectionChange(object sender, SelectionChangedEventArgs e)
{
if (e.RemovedItems.Count == 0)
{
// I guess this is the event that happens when a page is selected
// in this case just a TabItem is added to the selection
// and nothing is removed, so do nothing
return;
}
// if you are here, it means that this is another selection changed event
// so Perform those tasks that you mentioned in your question
}

Silverlight 4, TreeView Drag/Drop. Drop Confirmation followed by Cancel

If I'm 'Drop'ing a TreeView node onto another Node. At the end of a Drag/Drop operation, I want to prompt the user to 'Confirm' the Drop, and then cancel/undo the Drop if they cancel.
This confirmation is currently in the Drop operation event , ie by opening a child window. However by the time the user confirms one way or the other, the event will already have finished and so I don't have an opportunity to 'Cancel' in the normal way? ie...
itemDragArgs.Cancel = true;
itemDragArgs.Handled = true;
Has anyone had any experience with such a scenario?
We have a similar scenario with some other controls in which,
The dropped Node is ignored (cancelled & handled = true) but keep the reference of the node (in an object field or smth)
upon confirmation, add the object to the node.
Something like this would be of any help?

C# WinForms - TreeView, Context Menu

Suppose I am using a context menu to add child nodes to a treeview control.
(1) I am right-clicking on the node
(2)context menu pop up
(3)then I click "Add" menu item
(4)a dialogBox opens up
(5) I input the name in that DialogBox and press OK
(6) A new Node is created.
How can I get the reference of the current Node when I am clicking on the context menu item?
I need this coz the parent object is stored in the Tag property of the current node.
If you handle TreeNodeMouseClick, then your TreeNodeMouseClickEventHandler will be passed a TreeNodeMouseClickEventArgs argument.
TreeNodeMouseClickEventArgs.Node will be the TreeNode reference you want. See the TreeNodeMouseClick docs for an example similar to:
void treeView1_NodeMouseClick(object sender,
TreeNodeMouseClickEventArgs e)
{
TreeNode theTreeNodeIWant = e.Node
}
If you need to, you can store a reference in a member variable so another method can access it.
You can get the mouse position from
System.Windows.Forms.Cursor.Position
Save this before showing the context menu.
Then use the method on the Treeview containing your items
GetChildAtPoint(Point)
and add a child below that.

Resources