I'm trying to open a contextmenustrip at the place where I right-clicked the mouse, but it always shows at top left of the screen.
Here is the code I used:
private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
contextMenuStrip1.Show(new Point(e.X,e.Y));
doss.getdossier(connection.conn, Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value));
}
}
if (e.Button == MouseButtons.Right)
{
contextMenuStrip1.Show(Cursor.Position);
}
the reason it's not appearing is because you are using e.X and e.Y for the values. They are not the actual location on the screen. They are the location of the mouse within the datagrid. So say you clicked on the first cell of the first row, that will be near the top left of that component. e.X and e.Y are the mouse locations within the component.
assuming you are in Windows Forms, try this:
if (e.Button == MouseButtons.Right)
{
Control control = (Control) sender;
// Calculate the startPoint by using the PointToScreen
// method.
var startPoint = control.PointToScreen(new Point(e.X, e.Y));
contextMenuStrip1.Show(startPoint);
...
...
Related
I'm hooking my form with middle click. And for now i middle click to hook my form then middle click again to trigger my method to draw on my picturebox (which is on my form).
I'd like to middle click once and instantly draw on my picturebox instead of two middle click. I tried MouseHover and MouseEnter with this code :
private void PbxDrawing_MouseEnter(object sender, EventArgs e)
{
bMoving = true;
Point pos = PbxDrawing.PointToClient(Cursor.Position);
x = pos.X;
y = pos.Y;
}
Mouse move :
private void PbxDrawing_MouseMove(object sender, MouseEventArgs e)
{
if(bMoving && x!=-2 && y != -2)
{
g.DrawLine(pen,new Point(x,y), e.Location);
x = e.X;
y = e.Y;
}
}
It allows me to know the position of the cursor and draw but to draw i've to release middle click .
How can i draw from 1 middle click if my middle click was made outside of my form ?
Edit : Clarify question
Just check if the Middle Button is down in the MouseMove event:
private void PbxDrawing_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Middle)
{
x = e.X;
y = e.Y;
}
}
private void PbxDrawing_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Middle)
{
g.DrawLine(pen, new Point(x, y), e.Location);
x = e.X;
y = e.Y;
}
}
Your use of g is a red flag, though. Is g created with PbxDrawing.CreateGraphics()? If yes, this is wrong and should be refactored to use the e.Graphics from the Paint() event of the PictureBox.
I'm trying to learn how to use the ContextMenuStrip
when using this code :
private void DG_dataGridView_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var hitTest = DG_dataGridView.HitTest(e.X, e.Y);
if (hitTest.Type == DataGridViewHitTestType.ColumnHeader)//currentMouseOverRow >= 0)
{
string colName = DG_dataGridView.Columns[hitTest.ColumnIndex].Name;
GlobalParam.Insatance.ClickData = new RightClickData(hitTest, colName);
RightClickToolStrip.Show(DG_dataGridView, new Point(e.X, e.Y));
}
}
}
I see the menu pop up in the right position
but when instead I use this code :
private void DG_dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var hitTest = DG_dataGridView.HitTest(e.Location.X, e.Location.Y);
string colName = DG_dataGridView.Columns[e.ColumnIndex].Name;
GlobalParam.Insatance.CustomMouseGridClickData = new CustomMouseOnGridClickData(e, hitTest.Type, colName);
RightClickToolStrip.Show(DG_dataGridView, new Point(e.X, e.Y));
}
}
I see the menu pop on the top left corner of my DataGridView
I know this is due to the operation of DataGridViewCellMouseEventArgs
I have tried to set other control in the toolStrip.Show method without success
what is the proper way to get my click position
With the ColumnHeaderMouseClick event, you don't have to test for the column getting clicked or not using that HitTest method. Also, I think the coordinates are relative to the column being clicked, so you can try using the GetColumnDisplayRectangle function to offset it property:
void DG_dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
if (e.Button == MouseButtons.Right) {
if (e.ColumnIndex > -1) {
Rectangle r = DG_dataGridView.GetColumnDisplayRectangle(e.ColumnIndex, true);
RightClickToolStrip.Show(DG_dataGridView, r.Left + e.X, r.Top + e.Y);
}
}
}
I have a panel with multiple pictureboxes over it. I want to create a rectangle over panel and Picturebox when MouseLeft Button is down and Mouse is dragged. I understand Rectangle Mouse select area can be created with the combination of Mousedown on Panel and MouseMove events on Picturebox and Panel.
Now My Implementation is , When i receive Mousedown event on Panel I save Mousedown Location and look for MouseMove events. if mousemove events is on panel i draw a rectangle over panel and if Mouse Move event is on Picturebox i start drwaing Rectangle for that Picturebox.
Now the Problem is , when mouseleft button is down and mouse move is done over panel i am able to receive mousemove events over it and draw rectangle over panel. However i am not receiving any MouseMove Events for Picturebox to create rectangle select area for it.
Is my Implementation Correct and can someone kindly propose the solution for this ?
I am Posting My Code Snippet for reference.
Code:
//Panel Class
public partial class Childwindow : Form
{
//Mouse down event
private void ClickEventOnchildForm(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
RectStartPoint = e.Location;
IsSelecting = true;
Invalidate();
}
}
//Mouse Move event
private void flowLayoutPanel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
return;
Point tempEndPoint = e.Location;
Rect.Location = new Point(
Math.Min(RectStartPoint.X, tempEndPoint.X),
Math.Min(RectStartPoint.Y, tempEndPoint.Y));
Rect.Size = new Size(
Math.Abs(RectStartPoint.X - tempEndPoint.X),
Math.Abs(RectStartPoint.Y - tempEndPoint.Y));
this.flowLayoutPanel1.Invalidate();
}
//Paint event
private void flowLayoutPanel1_Paint_1(object sender, PaintEventArgs e)
{
if (Rect != null && Rect.Width > 0 && Rect.Height > 0)
{
e.Graphics.FillRectangle(selectionBrush, Rect);
}
}
}
Public class Picturebox: UserControl{
private void picbox_MouseMove(object sender, MouseEventArgs e)
{//Higlight particular Picturebox
}
}
Hello I have found this code that might help me with following issue, I'm trying to make drag, drop and move label in my Form by mouse.
private Point MouseDownLocation;
private void MyControl_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}
private void MyControl_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
this.Left = e.X + this.Left - MouseDownLocation.X;
this.Top = e.Y + this.Top - MouseDownLocation.Y;
}
}
But when I assing mousemove and mousedown as events to label and i try to grab the label and move with mouse it moves with the whole Form.
May I ask where is should the code be improved?
Thank you for your time.
Instead of using this.Left (which is the form), you need to move your control:
private void MyControl_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MyControl.Left = e.X + MyControl.Left - MouseDownLocation.X;
MyControl.Top = e.Y + MyControl.Top - MouseDownLocation.Y;
}
}
In addition, you may want to capture the mouse on button down, and release it on button up. That will prevent very fast movements from "breaking" your logic. For details, see Mouse Capture in Windows Forms.
I have a datagridview where I show infomation about products. I want to bind a contextmenu when the user selects a cell and then right clicks on that cell. I have another contextmenu and that one is bound to the columns of the datagridview. If a user right clicks on a column the contextmenu shows.
I have tried like this but it does not work. The context menu shows when the user right clicks on a cell, but the contextmenu that is bound to the column header does not work.
private void GridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
productContextMenu.Show(GridView1, e.Location);
}
}
How do I make it so that when the user right clicks on a datagridview shows up?
Many thanx in advance.
EDIT
Thnx for the answers. I solved the problem like this:
private void GridView1_MouseUp(object sender, MouseEventArgs e)
{
DataGridView.HitTestInfo hitTestInfo;
if (e.Button == MouseButtons.Right)
{
hitTestInfo = GridView1.HitTest(e.X, e.Y);
if (hitTestInfo.Type == DataGridViewHitTestType.Cell)
{
productContextMenu.Show(GridView1, e.Location);
}
}
}
Both the contextmenus shows. When I click on the column that context menu shows, and when I click on a cell that contextmenu shows.
Try this
private void dataGridView1_CellMouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
contextMenu.Show(datagridview, e.Location);
}
}
or
private void dataGridView_MouseUp(object sender, MouseEventArgs e)
{
// Load context menu on right mouse click
DataGridView.HitTestInfo hitTestInfo;
if (e.Button == MouseButtons.Right)
{
hitTestInfo = dataGridView.HitTest(e.X, e.Y);
// If column is first column
if (hitTestInfo.Type == DataGridViewHitTestType.Cell && hitTestInfo.ColumnIndex == 0)
contextMenuForColumn1.Show(dataGridView, new Point(e.X, e.Y));
// If column is second column
if (hitTestInfo.Type == DataGridViewHitTestType.Cell && hitTestInfo.ColumnIndex == 1)
contextMenuForColumn2.Show(dataGridView, new Point(e.X, e.Y));
}
}
For the problem with the relative position, you can also use this aproach:
DataGridViewColumn dgvC = new DataGridViewColumn();
DataGridViewRow dgvR = new DataGridViewRow();
dgvC = dgv.Columns[e.ColumnIndex];
dgvR = dgv.Rows[e.RowIndex];
Point p = new Point();
p.X = (dgvC.Width * e.ColumnIndex) + e.X;
p.Y = (dgvR.Height * e.RowIndex) + e.Y;
dgv.ContextMenu.Show(dgv, p);