I have a custom TabControl in which I have TabPages with ContextMenu bound to them.
I want the menu to show up only when the page header is being clicked.
What I do is that, when the TabControl is clicked, I check these conditions:
private void MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == Mousebuttons.Right)
{
for (int i = 0; i < TabCount; ++i)
{
Rectangle r = GetTabRect(i);
if (r.Contains(e.Location) /* && it is the header that was clicked*/)
{
// Change slected index, get the page, create contextual menu
ContextMenu cm = new ContextMenu();
// Add several items to menu
page.ContextMenu = cm;
page.ContextMenu.Show(this, e.Location);
}
}
}
}
If I bind MouseUp to the TabControl, I get the ContextMenu in the entire TabPage. If I bind it to the TabPage, I only get the ContextMenu in the body and not in the header.
Is there a way to have a ContextMenu to show up only on header Click ?
Just don't ever assign the ContextMenu to anything...simply display it:
public class MyTabControl : TabControl
{
protected override void OnMouseUp(MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
for (int i = 0; i < TabCount; ++i)
{
Rectangle r = GetTabRect(i);
if (r.Contains(e.Location) /* && it is the header that was clicked*/)
{
// Change slected index, get the page, create contextual menu
ContextMenu cm = new ContextMenu();
// Add several items to menu
cm.MenuItems.Add("hello");
cm.MenuItems.Add("world!");
cm.Show(this, e.Location);
break;
}
}
}
base.OnMouseUp(e);
}
}
Instead of override like Idle_Mind said, you could also do the same with a normal tabcontrol on the mouseevent:
private void tabControl1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
for (int i = 0; i < tabControl1.TabCount; ++i)
{
Rectangle r = tabControl1.GetTabRect(i);
if (r.Contains(e.Location) /* && it is the header that was clicked*/)
{
// Change slected index, get the page, create contextual menu
ContextMenu cm = new ContextMenu();
// Add several items to menu
cm.MenuItems.Add("hello");
cm.MenuItems.Add("world!");
cm.Show(tabControl1, e.Location);
break;
}
}
}
}
It does exactly the same, but doesn't add an extra control in your toolbox:)
You could also make it generic if you want to use it on multiple TabControls.
private void showContextMenu_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
TabControl tabControl1 = sender as TabControl;
[...]
Related
I want to allow user to drag any item from MenuStrip to a ListBox.
I did it between to ListBoxes, but can not do it with MenuStrip.
Thanks a lot for your help.
I use WinForms, C#
For the destination ListBox I modified its property
this.listBox2.AllowDrop = true;
and created the following two events:
private void listBox2_DragOver(
object sender, System.Windows.Forms.DragEventArgs e)
{
e.Effect=DragDropEffects.All;
}
private void listBox2_DragDrop(
object sender, System.Windows.Forms.DragEventArgs e)
{
if(e.Data.GetDataPresent(DataFormats.StringFormat))
{
string str= (string)e.Data.GetData(
DataFormats.StringFormat);
listBox2.Items.Add(str);
}
}
What I need is what should be done to the source MenuStrip to allow drag items from it the ListBox, in over words how to make MenuStrip draggable.
Thanks to all for their help.
I found the solution:
The missing event is that I should add event to ToolStripMenuItem_MouseDown, I prefer to use right click instead of left click to avoid the conflict between ToolStripMenuItem_Click and the drag event, this the code:
AllowDrop = true;
private void tsmi_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
DoDragDrop(sender, System.Windows.Forms.DragDropEffects.Copy);
}
Add also this code to the ListView:
private void lvAllowDropListView_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
System.Windows.Forms.ToolStripMenuItem button = e.Data.GetData(typeof(System.Windows.Forms.ToolStripMenuItem))
as System.Windows.Forms.ToolStripMenuItem;
if (button != null)
{
try
{
SmallImageList = sysIcons.SmallIconsImageList;
LargeImageList = sysIcons.LargeIconsImageList;
System.Windows.Forms.ToolStripMenuItem item = e.Data.GetData(typeof(System.Windows.Forms.ToolStripMenuItem))
as System.Windows.Forms.ToolStripMenuItem;
if (item != null)
{
AddToolStripMenuItem(item.Text, item.Name);
}
}
catch { }
}
}
private void AddToolStripMenuItem(string name, string tag)
{
System.Windows.Forms.ListViewItem item = new System.Windows.Forms.ListViewItem(name);
int Index = -1;
for (int i = 0; i < Items.Count;i++ )
if(Items[i].Tag.ToString() == tag)
{
Index = i;
break;
}
if (Index == -1)
{
item.Tag = tag;
Items.Add(item);
}
}
Drag Menu Strip Item is the same like ListBox item.
Check your code...
I have a custom control containing a button with a label underneath. I want to make these controls to be draggable over another one to arrange them as I want in a flowlayoutpanel.
Now is working only if I drag the control from it's background (marked with yellow in the picture bellow) to the other control's yellow marked area, but not if i drag from the button or label area..
How can I make it so I can move the custom control no matter from where I grab and drop it on the other control. Basically to be only one control not like a container for the button and label..
This is my code so far:
private void flowLayoutPanel1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void flowLayoutPanel1_DragDrop(object sender, DragEventArgs e)
{
CustomControl target = sender as CustomControl;
if (target != null)
{
int targetIndex = FindCSTIndex(target);
if (targetIndex != -1)
{
string pictureBoxFormat = typeof(CustomControl).FullName;
if (e.Data.GetDataPresent(pictureBoxFormat))
{
CustomControl source = e.Data.GetData(pictureBoxFormat) as CustomControl;
int sourceIndex = this.FindCSTIndex(source);
if (targetIndex != -1)
this.flowLayoutPanel1.Controls.SetChildIndex(source, targetIndex);
}
}
}
}
private int FindCSTIndex(CustomControl cst_ctr)
{
for (int i = 0; i < this.flowLayoutPanel1.Controls.Count; i++)
{
CustomControl target = this.flowLayoutPanel1.Controls[i] as CustomControl;
if (cst_ctr == target)
return i;
}
return -1;
}
private void OnCstMouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
CustomControl cst = sender as CustomControl;
cst.DoDragDrop(cst, DragDropEffects.Move);
}
}
And the custom control class:
public class CustomControl : Control
{
private Button _button;
private Label _label;
public CustomControl(Button button, Label label)
{
_button = button;
_label = label;
button.Width = 50;
button.Height = 50;
label.Width = 65;
button.BackgroundImageLayout = ImageLayout.Stretch;
Height = button.Height + label.Height;
Width = 68;
// Width = Math.Max(button.Width, label.Width);
Controls.Add(_button);
_button.Location = new Point(0, 0);
Controls.Add(_label);
_label.Location = new Point(0, button.Height);
}
}
Use MouseDown instead of MouseMove to initiate drag-and-drop (MSDN). You can initiate drag-and-drop in the control code itself (assuming what all CustomControls will be drag-and-drop-able), otherwise you may want to create public method to sign childs (exposing childs is bad idea, unless you already use them outside).
public class CustomControl : Control
{
...
public CustomControl(Button button, Label label)
{
...
_button.MouseDown += OnMouseDown;
_label.MouseDown += OnMouseDown;
}
override void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
(sender as Control).DoDragDrop(this, DragDropEffects.Move);
}
}
Untested, but should give you idea.
Managed to solve it:
private void flowLayoutPanel1_DragDrop(object sender, DragEventArgs e)
{
Control target = new Control();
target.Parent = sender as Control;
if (target != null)
{
int targetIndex = FindCSTIndex(target.Parent);
if (targetIndex != -1)
{
string cst_ctrl = typeof(CustomControl).FullName;
if (e.Data.GetDataPresent(cst_ctrl))
{
Button source = new Button();
source.Parent = e.Data.GetData(cst_ctrl) as CustomControl;
if (targetIndex != -1)
this.flowLayoutPanel1.Controls.SetChildIndex(source.Parent, targetIndex);
}
}
}
}
private int FindCSTIndex(Control cst_ctr)
{
for (int i = 0; i < this.flowLayoutPanel1.Controls.Count; i++)
{
CustomControl target = this.flowLayoutPanel1.Controls[i] as CustomControl;
if (cst_ctr.Parent == target)
return i;
}
return -1;
}
private void OnCstMouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Control cst = sender as Control;
cst.DoDragDrop(cst.Parent, DragDropEffects.Move);
}
}
I have a DataGridView on a form. When I right-click a row, I need the program to open a context menu. With this context menu I want to be able to modify the data in the DataGridView.
I have gotten the context menu to show where I right click, but I don't know where to go from here. As I will be deleting (for example) an entire row, I need to get the index of said row and also set it to selected. I tried this with the cell_clicked event but I can't determine if the left or right mouse button was pressed. But with the mouse_click event I cannot get the row index.
Here is my code:
public Form()
{
ContextMenu contextMenu = new ContextMenu();
//Fill Context Menu
MenuItem delete = new MenuItem("Delete");
contextMenu.MenuItems.Add(delete);
}
private void grdSchedules_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
contextMenu.Show(grdSchedules, new Point(e.Y, e.Y));
//Get rowindex here and select row
}
}
I have tried it this way:
private void grdSchedules_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right) //e.Button does not work here
{
contextMenu.Show(grdSchedules, new Point(e.Y, e.Y));
}
}
I created a more simple and faster generic method which works with any datagrids. This method allows selecting rows with a right click. Add this method to your DataGridViews' "MouseDown" event:
public void DataGridView_RightMouseDown_Select(object sender, MouseEventArgs e)
{
// If the user pressed something else than mouse right click, return
if (e.Button != System.Windows.Forms.MouseButtons.Right) { return; }
DataGridView dgv = (DataGridView)sender;
// Use HitTest to resolve the row under the cursor
int rowIndex = dgv.HitTest(e.X, e.Y).RowIndex;
// If there was no DataGridViewRow under the cursor, return
if (rowIndex == -1) { return; }
// Clear all other selections before making a new selection
dgv.ClearSelection();
// Select the found DataGridViewRow
dgv.Rows[rowIndex].Selected = true;
}
I have found a solution. Here is how I did it:
private void grdSchedules_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
int currentMouseOverRow = grdSchedules.HitTest(e.X, e.Y).RowIndex;
for (int x = 0; x < grdSchedules.Rows.Count; x++)
{
if (grdSchedules.Rows[x].Index == currentMouseOverRow)
{
grdSchedules.Rows[x].Selected = true;
}
else
{
grdSchedules.Rows[x].Selected = false;
}
}
contextMenu.Show(grdSchedules, new Point(e.Y, e.Y));
}
}
You could've used grdSchedules_MouseDown or grdSchedules_MouseUp events instead of grdSchedules_MouseClick or grdSchedules_CellClick.
How to select or focus a certain tabpage when it right clicked?
Implement the MouseDown event and find out what tab got clicked:
private void tabControl1_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Right) {
for (int tab = 0; tab < tabControl1.TabCount; ++tab) {
if (tabControl1.GetTabRect(tab).Contains(e.Location)) {
tabControl1.SelectedIndex = tab;
break;
}
}
}
}
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.