I want to show usercontrol as popup at mouse location.
However below code doesn't work for this;
System.Drawing.Point mousePos = System.Windows.Forms.Cursor.Position;
System.Drawing.Point formPos = this.Bounds.Location;
popupChart1.ultraLabel1.Text = e.DataRow.ToString() ;
popupChart1.Location = new System.Drawing.Point(mousePos.X - formPos.X, mousePos.Y - formPos.Y);
popupChart1.Show();
It doesn't throws any error, it just show it self at another place. What could be the reason ?
You should set the location after the form is rendered.
And if you want it to show at your mouse cursor position then simply use Cursor.Position instead of the math you did
popupChart1.Show();
popupChart1.Location = Cursor.Position;
OR
if you want to set the location before you show the window you can do
popupChart1.Location = Cursor.Position;
popupChart1.StartPosition = FormStartPosition.Manual;
popupChart1.Show();
Related
Hello I have a windows form. I want to show a popup at the position of mouse but when I get the mouse position with mouseposition property,it doesnt show right below the cursor.
When I click the cell the popup should appear right below the cursor instead it shows at random places
I have a code like this:
Point p = new Point();
p = MousePosition;
if (e.Column.Name=="colnot_detay")
{
labelControl1.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"not_detay").ToString();
popupContainerControl2.Location = p;
popupContainerControl2.Show();
}
I'm working on a simple practice: Double click to create a textBox on mouse location (X,Y)
It is creating the object, but far from the mouse exact position.
private void DynamicObjects_MouseDoubleClick(object sender, MouseEventArgs e)
{
System.Windows.Forms.TextBox txtBox = new System.Windows.Forms.TextBox();
this.Controls.Add(txtBox);
txtBox.Top = MousePosition.Y;
txtBox.Left = MousePosition.X;
//txtBox.Location = MousePosition; --Still off away from Mouse real location
//txtBox.Location = MousePosition.Y; -- Fives erro Cannot implicitly convert Int to 'System.Drawing.Point'
}
Only way I've found to short of work is using .Top and .Left.
Why is creating far from mouse?
MousePosition gets the position of mouse cursor in screen coordinates.
We have to translate it to the client coordinates.
var textBox = new TextBox();
textBox.Location = PointToClient(MousePosition);
Controls.Add(textBox);
We need to call this method from the same control that we will place the TextBox on.
For example:
this.PointToClient
this.Controls.Add
or
panel.PointToClient
panel.Controls.Add
So im trying to get mouse coordinates from a click on an image, and it gives the wrong coordinates. When i move the mouse to draw, the line appears away from the cursor.
This is the code i use to get the mouse coordinates:
private void ponaredek_MouseDown(object sender, MouseButtonEventArgs e)
{
mouseDown = true;
//x1 = System.Windows.Forms.Control.MousePosition;
x1 = new System.Drawing.Point((int)e.GetPosition(this).X, (int)e.GetPosition(this).Y);
}
x1 is of type System.Drawing.Point (i need the point from drawing, to use in emgucv). What do i have to do to correct the cursor location (i drew where the cursor was)
You want to get the mouse position relative to the Image element, not the Window. So replace
e.GetPosition(this)
by
e.GetPosition((IInputElement)sender)
or
e.GetPosition(ponaredek)
if that is the Image element.
It should look like this:
var pos = e.GetPosition((IInputElement)sender);
x1 = new System.Drawing.Point(pos.X, pos.Y);
Also make sure the Image element's Stretch property is set to None.
I want to create a windows form that when i click on a button it show mouse position and type of left click or right click in a label.I use this code:
this.Cursor = new Cursor(Cursor.Current.Handle);
label1.Text = Cursor.Position.X;
label1.Text+= Cursor.Position.Y;
but this show mouse position only from application form not anywhere,how to change it that return mouse position from desktop no a form?
thank you in advance.
Here is a good article how help you to getting Position of mouse cursor when clicked out side the form's :
Get Cursor Position outside form
You can add to the cursor position in the form, the position of the form in the screen.
Label1.Text = Form1.Location.X + Cursor.Position.X;
Label1.Text += Form1.Location.Y + Cursor.Position.Y;
I tried the Cursor.Position, but it isn't working. I want it to point to a textbox. What should I do? Should I create a cursor object or something like that?
You can use mouse_event to set the cursor, its a win32 function so you need to pinvoke
Be sure to set the absolute flag
http://www.pinvoke.net/default.aspx/user32.mouse_event
PointConverter pc = new PointConverter();
Point pt = new Point();
pt = (Point)pc.ConvertFromString("0, 100"); //X,Y value
Cursor.Position = pt;
Try this solution.
Source: http://social.msdn.microsoft.com/Forums/en-us/f7eeb890-a87d-4dbb-9ea8-a77ded3ee363/changing-the-mousecursor-position
The following code was written as a Windows Forms application.
Put this in your button event handler. This will move the cursor to the exact center of the textbox (textbox1):
Point p = new Point(textBox1.Location.X + textBox1.Width / 2, textBox1.Location.Y + textBox1.Height / 2);
Cursor.Position = PointToScreen(p);