I have a question.
I put a treeview control on my form and added some nodes.
public Form1()
{
InitializeComponent();
treeView1.Nodes.Add("root node #1");
treeView1.Nodes.Add("root node #2");
treeView1.Nodes.Add("root node #3");
treeView1.Nodes.Add("root node #4");
treeView1.Nodes.Add("root node #5");
}
I want to change standard right-click behavior. When I right-clicked on a tree Node then Treeview changed for a while selectedIndex. I don't want it. How can I fix standard behavior ?
In ideal, it would be: right click on a treenode text --> context menu appears, right click anywhere outside treenode text --> (absolutely) nothing happens.
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
// put your logic here like
// ContextMenu1.Show();
}
}
Alex,
Try this. The BeforeSelect handlers event args Cancel is tied to the fact that the right mouse is down. This suppresses the firing of the SelectedIndex changed. The MouseDown tracks if the right mouse is depressed and displays the context menu. The display is safe to move to MouseUp instead of MouseDown. The MouseUp clears the flag indicating that the RightMouse button is depressed.
All of this information on how I did this is available on MSDN. The trick is actually reading the names of all of the events -- Yes I know there are a lot -- then making a list of the "interesting ones" in your case you named SelectedIndex changing, and Mouse clicks. That immediately limits the event names you should read in detail on... If you want the text to not highlight while you're right clicking... well that's a different matter entirely and I caution you against it as it's valuable user feedback.
bool isRBut = false;
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
isRBut = e.Button == MouseButtons.Right;
if (isRBut)
{
TreeViewHitTestInfo hti =treeView1.HitTest(e.Location);
if (hti.Location == TreeViewHitTestLocations.Label)
contextMenuStrip1.Show(treeView1, new Point(hti.Node.Bounds.Left, hti.Node.Bounds.Bottom));
}
}
private void treeView1_MouseUp(object sender, MouseEventArgs e)
{
isRBut = e.Button == MouseButtons.Right;
}
private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
e.Cancel = isRBut;
}
Additionally, here's a bit of human language trivia for you. Hopefully this will help you in the future. Phrases such as "No, no, no" are interpreted by native English speakers as very rude. Just do your best to list the behavior you see, and the behavior you want. Even if people misunderstand stick to the facts only and leave out the obvious signs of frustration. This will help you get what you're after. As well on SO if someone has a habit of not accepting answers many members here will have a habit of not providing future answers to such members.
Override the MouseClick event, and in the event check if the click was right click
private void treeView1_MouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
//Do something
}
}
You need to handle the NodeMouseClick event and check the right mouse button was clicked:
treeView1.NodeMouseClick += (o, e) => {
if(e.Button == MouseButtons.Right)
{
//show menu...
}
};
Related
I have a button on a form and want to handle both left and right clicks.
I am handling the MouseClick event, but this is only raised on a left click.
Is this a problem somewhere in my code (a setting that I have missed) or the intended functionality?
If this is not possible to fix, what is the best workaround - to handle the MouseUp event?
The reason I would like to use MouseClick is so that double clicks are automatically recognised.
Thanks for any feedback.
Use MouseUp !!
private void button6_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MessageBox.Show("LEFT");
}
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
MessageBox.Show("RIGHT");
}
}
Its hard to answer without code but in general, it should work.
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MessageBox.Show("LEFT");
}
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
MessageBox.Show("RIGHT");
}
}
// EventHandler
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseClick);
Edit: There is a MouseDoubleClick Event you might want to use to recognize double clicks. Works both, for left and right musebuttons.
Apparently the answer to this is that OnClick does not handle right click events for Buttons. The solution was therefore to use MouseUp/MouseDown and check for double clicks/clicks where the mouse moves on/off halfway through manually.
I'm trying to build a simple interface that allows users to drop files into a listBox to add them to a process, and to drag them out to remove them. Everything is working fine, but I'd like to add one feature to make it just a tad more sophisticated.
Right now, I have the removal of the item tied to the DragLeave event, which means that as soon as the mouse leaves the box, the item is removed. But I'd like for users to be able to change their minds. In other words, if they realize they're dragging the wrong file out, I'd like them to be able to move the mouse back into the listBox and release the mouse to cancel the action. I'm thinking that means I need to be able to capture the MouseUp event instead of the DragLeave event. But that hasn't been successful so far.
Below is the code I'm currently using for removing files dragged out. How can I modify to keep the files from being removed form the list until the user lets the mouse button go?
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
if (listBox1.Items.Count == 0)
{
return;
}
int index = listBox1.IndexFromPoint(e.X, e.Y);
string s = listBox1.Items[index].ToString();
DragDropEffects dde1 = DoDragDrop(s, DragDropEffects.All);
}
private void listBox1_DragLeave(object sender, EventArgs e)
{
ListBox lb = sender as ListBox;
lb.Items.Remove(lb.SelectedItem);
}
Edit 2013/05/16
The comments and answers so far have been useful, but I realize my question isn't clear enough. In this case, I'm displaying a dialog separate from the parent form that is basically as big as the listBox. When someone drags a file out of the list, they're dragging it off the form completely. Have I backed myself into a corner by doing this? I recognize I'm making it harder than it has to be, but I'd still like to see how it would work if it's possible.
Here's a fairly quick hack approach to gaining the functionality you want:
public object lb_item = null;
private void listBox1_DragLeave(object sender, EventArgs e)
{
ListBox lb = sender as ListBox;
lb_item = lb.SelectedItem;
lb.Items.Remove(lb.SelectedItem);
}
private void listBox1_DragEnter(object sender, DragEventArgs e)
{
if (lb_item != null)
{
listBox1.Items.Add(lb_item);
lb_item = null;
}
}
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
lb_item = null;
if (listBox1.Items.Count == 0)
{
return;
}
int index = listBox1.IndexFromPoint(e.X, e.Y);
string s = listBox1.Items[index].ToString();
DragDropEffects dde1 = DoDragDrop(s, DragDropEffects.All);
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
lb_item = null;
}
Every time the user drags an item out of the box, it's saved temporarily until the user drops it somewhere else or mouses down on a new item in the list.
Note the important part of this is detecting when and where the user let's go of that mouse, which is the rationale behind handling the DragDrop event of Form1, the parent of listBox1.
Depending on the sophistication and density of the rest of your layout, where you handle DragDrop could be much different for you. This is why it's kind of "hacky", but it's also quite simple. It shouldn't matter, though, where or how many times you null lb_item since it pertains only to that specific ListBox.
I suppose another way to do it would be to track the user's mouse states and act accordingly, which may be more appropriate for you if it's inconceivable to handle a lot of DragDrop stuff.
EDIT: If you wanted to be REAL thorough, you could enumerate through every control of the base form using foreach and programmatically append a handler for the DragDrop event to that control, then remove it when done... but that may be getting a little nutty. I'm sure someone has a better approach.
I have a tree view on the left side. Selecting a node displays relevant information in a form on the right side.
Would I be able to keep the tree and any one control (textbox, combobox, checkbox) on the right in focus at the same time? This will enable a user to select a field, make a change, select another node, and without having to go back and select the same field again, just type and change the value of the same field.
Thanx.
EDIT
I suppose one could implement such behaviour manually:
private Control __cFocus;
private void {anyControl}_Focus(object sender, EventArgs e)
{
__cFocus = (Control)sender;
}
private void treeView1_AfterSelect(object sender, EventArgs e)
{
__cFocus.Focus();
}
I was just wondering if there exists an automatic / more elegant solution
EDIT 2
Ok, so it seems I'll have to implement it manually. Manual implementation it is then. However, now there seem to be another problem; not sure if I should ask this as a separate question.
When selecting a node the textbox gains focus as intended, but only when using the keyboard. It doesn't work when selecting a node with the mouse. First I thought that it might be a mouse event that's interfering, but stepping revealed that the MouseUp event fired first and then the AfterSelect event which sets the focus, so I don't think it's interfering. The textbox's Enter event is also fired, but for some reason it loses focus again to the tree.
Thanx
no, you cannot keep two controls in focus at the same time. But what you can do is set the focus to the target control in the treeview AfterSelect event
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
textBox1.Focus();
textBox1.SelectAll();
}
then in your textbox leave, save the changes, like so:
private void textBox1_Leave(object sender, EventArgs e)
{
//save changes here
}
this way, everytime you select an item in the treeview, check your textbox for change and save as needed, then you will refocus on the textbox for your next edit
There only can be one element having the focus!
But I have an idea for you that might solve your problem. Assuming you have a window with a TreeView and a TextBox. Set the HideSelection property of the TreeView to false and subscribe the AfterSelect event (like edeperson already answered) like this:
private void OnTreeViewAfterSelect(object sender, TreeViewEventArgs e)
{
textBox1.Text = e.Node.Text;
textBox1.Focus();
}
Then subscribe the KeyDown event of the TextBox and do following in the event method:
private void OnTextBoxKeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == Keys.Up) || (e.KeyCode == Keys.Down))
{
treeView1.Focus();
SendKeys.Send(e.KeyCode == Keys.Up ? "{UP}" : "{DOWN}");
}
}
At last subscribe the Leave event of the TextBox and do following in the event method:
private void OnTextBoxLeave(object sender, EventArgs e)
{
if (treeView1.SelectedNode != null)
{
treeView1.SelectedNode.Text = textBox1.Text;
}
}
And, voilá it should work like you expected it...
If you want to focus on it , you can use usercontrol. you can put your textbox on usercontrol and set focus of this textbox on usercontrol using set properties on treeview select.
No you may not, only one control may be in focus at any given time.
See Moonlight's comment for one way to achieve the behavior that you seek.
Whenever I click outside the tree nodes text, on the control part, it tigers a node click event- but doesn't highlight the node. I am unsure why this is happening.
I want the node to be selected on a click- when you click the nodes text- not the whitespace- I only assume that the nodes width reaches across the whole Treenode? I have the Treeview on dock.fill mode if that has something to do with it- I tried everything but can't get it to behave correctly.
Maybe someone will know what's going on.
Update:
if (e.Location.IsEmpty)
{
Seems to work better- but still selects the node in the blank place where there is no text- Obviously the node width extends across the whole treeview it seems?
Is there a better way to accomplish what I want? Or is that the best way?
UPDATE: Previous idea isn't working- sigh- I thought it did it but it didn't.
New Problem : I think part of the problem is related to the focus now when I switch from treeview.
UPDATE-
The only code I came up with about disabling right mouse click to select node on beforeSelect event is
if (MouseButtons == System.Windows.Forms.MouseButtons.Right)
{
e.Cancel = true;
}
But it didn't work- any help is appreciated- following suggestions of only answer, for more details.
You should use the treeView.HitTest method to determine which part of the node has been clicked.
private bool IsClickOnText(TreeView treeView, TreeNode node, Point location)
{
var hitTest = treeView1.HitTest(location);
return hitTest.Node == node
&& hitTest.Location == TreeViewHitTestLocations.Label;
}
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if(IsClickOnText(treeView1, e.Node, e.Location))
{
MessageBox.Show("click");
}
}
private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
if (e.Action == TreeViewAction.ByMouse)
{
var position = treeView1.PointToClient(Cursor.Position);
e.Cancel = !IsClickOnText(treeView1, e.Node, position);
}
}
Use the .AfterSelect and/or .BeforeSelect events to handle the selection processing instead of the .Click event. Then it will select the node only when you click on the text, and it won't fire .AfterSelect or .BeforeSelect when you click on the white space.
I have a ComboBox that is a simple drop down style. I wanted to open a new window when the user right clicks on an item in the list, but am having trouble getting it to detect a right click has occurred.
My code:
private void cmbCardList_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && cmbCardList.SelectedIndex != -1)
{
frmViewCard vc = new frmViewCard();
vc.updateCardDisplay(cmbCardList.SelectedItem);
vc.Show();
}
}
If I change e.Button == MouseButtons.Left the whole thing fires off just fine. Any way I can get this working as I intend?
I'm afraid that will not be posible unless you do some serious hacking.
This article will explain.
Quoted for you:
Individual Controls
The following controls do not conform to the standard mouse click event behavior:
Button, CheckBox, ComboBox, and RadioButton controls
Left click: Click, MouseClick
Right click: No click events raised
Left double-click: Click, MouseClick;
Click, MouseClick
Right double-click: No click events
raised
As an epitaph to this question, you can make this work using normal .NET functionality; you just have to go a little deeper into the event call stack. Instead of handling the MouseClick event, handle the MouseDown event. I had to do something similar recently, and I simply overrode the OnMouseDown method instead of attaching a handler. But, a handler should work too. Here's the code:
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && !HandlingRightClick)
{
HandlingRightClick = true;
if (!cmsRightClickMenu.Visible)
cmsRightClickMenu.Show(this, e.Location);
else cmsRightClickMenu.Hide();
}
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
HandlingRightClick = false;
base.OnMouseUp(e);
}
private bool HandlingRightClick { get; set; }
The HandlingRightClick property is to prevent multiple triggers of the OnMouseDown logic; the UI will send multiple MouseDown messages, which can interfere with hiding the right-click menu. To prevent this, I only perform the logic once on the first MouseDown trigger (the logic's simple enough that I don't care if two invocations happen to race, but you might), then ignore any other MouseDown triggers until a MouseUp occurs. It's not perfect, but this'll do what you need it to.
You can use the Opening event of ContextMenuStrip to handle right click event.
var chk = new CheckBox();
chk.ContextMenuStrip = cmsNone;
private void cmsNone_Opening(object sender, CancelEventArgs e)
{
e.Cancel = true;
var cms = (ContextMenuStrip)sender;
var chk = cms.SourceControl;
//do your stuff
}