TreeView_NodeMouseClick handler - c#

How to make node being selected by right mouse button down ?
I made it by right mouse button clicked like this:
private void myTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
this.myTreeView.SelectedNode = e.Node;
}
}
I want node being selected not by click, but just button down.

I'm guessing you are looking for something like this:
void myTreeView_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Right) {
TreeNode tn = myTreeView.GetNodeAt(e.Location);
if (tn != null) {
myTreeView.SelectedNode = tn;
}
}
}
Subscribe to the MouseDown event of the TreeView and comment out the NodeMouseClick code.

Related

Visual C# ListView ContextMenu

I have a ListView to display Images Icons, and i need 2 different Context menu on that ListView that have to show when i Right Click inside the ListView.
ContextMenu1 have to show only when i Right Click on a Item
ContextMenu2 have to show when i DO NOT Right click on a Item, but in the blank space of the ListView.
This is the current Code that i have, but it is only working when i right click on a item:
private void ListView_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (ListView.GetItemAt(e.X, e.Y) is ListViewItem)
{
ContextMenu1.Show(Cursor.Position);
}
else
{
ContextMenu2.Show(Cursor.Position);
}
}
}
What i did wrong?
GetItemAt will always return a ListviewItem. You should check for null like this example from MSDN:
private void ListView_MouseDown(object sender, MouseEventArgs e)
{
if (ListView.GetItemAt(e.X, e.Y) != null )
{
ContextMenu1.Show(Cursor.Position);
}
else
{
ContextMenu2.Show(Cursor.Position);
}
}
Try using the MouseDown or MouseUp event instead:
listView1.MouseDown += listView1_MouseDown;
void listView1_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Right) {
if (listView1.GetItemAt(e.X, e.Y) is ListViewItem) {
ContextMenu1.Show(Cursor.Position);
} else {
ContextMenu2.Show(Cursor.Position);
}
}
}
The MouseClick event fires only when a ListItem is clicked.

make delayed mousedown event

I have a datagridview which cells has a click event. The cells also have the following mouseDown event:
if (e.Button == MouseButtons.Left && e.Clicks == 1)
{
string[] filesToDrag =
{
"c:/install.log"
};
gridOperations.DoDragDrop(new DataObject(DataFormats.FileDrop, filesToDrag), DragDropEffects.Copy);
}
whenever I try to click a cell, the mousedown event instantly fires and tries to drag the cell. How can I make the mousedown event fire only if user has holded mouse down for 1 second for example? Thanks!
The proper way to do this is not by time but to trigger it when the user moved the mouse enough. The universal measure for "moved enough" in Windows is the double-click size. Implement the CellMouseDown/Move event handlers, similar to this:
private Point mouseDownPos;
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) {
mouseDownPos = e.Location;
}
private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e) {
if ((e.Button & MouseButtons.Left) == MouseButtons.Left) {
if (Math.Abs(e.X - mouseDownPos.X) >= SystemInformation.DoubleClickSize.Width ||
Math.Abs(e.Y - mouseDownPos.Y) >= SystemInformation.DoubleClickSize.Height) {
// Start dragging
//...
}
}
}

Visual C# Form right click button

I am trying to make a minesweeper type game in visual c# and I want to have different things happen when I right click and left click a button, how do I do this?
I have tried this code but it only registers left clicks:
private void button1_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");
}
}
You will have to use the MouseUp or MouseDown event instead of the Click event to capture right click.
Just try with button1_MouseDown event instead of button1_MouseClick Event.It will solve your problem.
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//do something
}
if (e.Button == MouseButtons.Right)
{
//do something
}
}
Button is reacting only for MouseButtons.Left not for MouseButton.Right and not even for middle.
void Select(object sender, MouseEventArgs e)
{
/* var btn = sender as CardButton;*/
if (e.Button == MouseButtons.Left)
{
if (this.Selected == false)
{
this.Selected = true;
}
else
{
this.Selected = false;
}
}
if (e.Button == MouseButtons.Right)
{
if (this.Selected == false)
{
this.Selected = true;
}
else
{
this.Selected = false;
}
}
Draw();
}

contextmenustrip: check selected item

I have a contextmenustrip that contains some option, I have almost implemented everything, i just want to check which items i have selected in options_MouseClick
private void ConsoleRichTextBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
options.Show();
Point currentPoint;
currentPoint = new Point(e.X, e.Y);
options.Show(ConsoleRichTextBox, currentPoint);
}
}
private void options_MouseClick(object sender, MouseEventArgs e)
{
//if options selected = clear
ConsoleRichTextBox.Clear();
}
You should just handle the Click event of each of the menu items. There's no need to handle the MouseClick event of a contextmenustrip. Also you can add multiple handlers to the same method and differentiate with the sender parameter as this will refer to the exact menu item that was clicked.
ToolStripMenuItem tsmi = new ToolStripMenuItem();
tsmi.Click += tsmi_Click;
and:
public void tsmi_Click(object sender, EventArgs e)
{
if (sender == tsmi)
{
// Do stuff
}
}

Close tab on winforms tab control with middle mouse button

Is there any easy (5 lines of code) way to do this?
The shortest code to delete the tab the middle mouse button was clicked on is by using LINQ.
Make sure the event is wired up
this.tabControl1.MouseClick += tabControl1_MouseClick;
And for the handler itself
private void tabControl1_MouseClick(object sender, MouseEventArgs e)
{
var tabControl = sender as TabControl;
var tabs = tabControl.TabPages;
if (e.Button == MouseButtons.Middle)
{
tabs.Remove(tabs.Cast<TabPage>()
.Where((t, i) => tabControl.GetTabRect(i).Contains(e.Location))
.First());
}
}
And if you are striving for least amount of lines, here it is in one line
tabControl1.MouseClick += delegate(object sender, MouseEventArgs e) { var tabControl = sender as TabControl; var tabs = tabControl.TabPages; if (e.Button == MouseButtons.Middle) { tabs.Remove(tabs.Cast<TabPage>().Where((t, i) => tabControl.GetTabRect(i).Contains(e.Location)).First()); } };
Solution without LINQ not so compact and beautiful, but also actual:
private void TabControlMainMouseDown(object sender, MouseEventArgs e)
{
var tabControl = sender as TabControl;
TabPage tabPageCurrent = null;
if (e.Button == MouseButtons.Middle)
{
for (var i = 0; i < tabControl.TabCount; i++)
{
if (!tabControl.GetTabRect(i).Contains(e.Location))
continue;
tabPageCurrent = tabControl.TabPages[i];
break;
}
if (tabPageCurrent != null)
tabControl.TabPages.Remove(tabPageCurrent);
}
}
Don't have enough points to post a comment to the provided solutions but they all suffer from the same flaw: The controls within the removed tab are not released.
Regards
You could do this:
private void tabControl1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Middle)
{
// choose tabpage to delete like below
tabControl1.TabPages.Remove(tabControl1.TabPages[0]);
}
}
Basically you are just catching a mouse click on tab control and only deleting a page if the middle button was clicked.

Categories

Resources