how to move a label on a winform at Runtime - c#

using this event the label just disappears, how shod i do this?
private void label4_MouseMove(object sender, MouseEventArgs e)
{
label4.Location = new Point(Cursor.Position.X, Cursor.Position.Y);
}

handle these three event ...
Control actcontrol;
Point preloc;
void label1_Mousedown(object sender, MouseEventArgs e)
{
actcontrol = sender as Control;
preloc = e.Location;
Cursor = Cursors.Default;
}
void label1_MouseMove(object sender, MouseEventArgs e)
{
if (actcontrol == null || actcontrol != sender)
return;
var location = actcontrol.Location;
location.Offset(e.Location.X - preloc.X, e.Location.Y - preloc.Y);
actcontrol.Location = location;
}
void label1_MouseUp(object sender, MouseEventArgs e)
{
actcontrol = null;
Cursor = Cursors.Default;
}

The location of label4 is relative to the container (Form or parent control), Cursor position may be relative to the screen.
You need to adjust the location. For example, if the container is the Form you can find its location in the screen and calculate by it the location of the cursor relative to screen.
This is only one possibility for the cause, but this one is happens a lot :)

Use the form's PointToClient() function to translate the mouse X/Y coordinates into points that are relative to your form, that should do it.
Edit: Use the mouse event args object properties instead:
Label1.Location = New Point(e.X, e.Y)
PS pardon the VB, no C# on this PC

The location of an element is relative to its parent. In this case though you are using the absolute mouse position as its location.
You'll need to translate the mouse position into the coordinate system of the parent element.
Use the PointToClient method on the label's parent element.

Related

c# Mouse events through child control

I have a custom user control that contains a chart element that takes up 80% of the space.
When this is placed in a form, i can click the area not occupied by the chart and the click/mousedown/mouseup events work. But yet when i click in the chart area the mouse events aren't passed through and therefore not triggered.
Is there a global way of doing this without having at add the event function for each control on the form?
void Drag_MouseDown(object sender, MouseEventArgs e)
{
activeControl = sender as UserControl;
previousLocation = e.Location;
Cursor = Cursors.SizeAll;
}
void Drag_MouseMove(object sender, MouseEventArgs e)
{
if (activeControl == null || activeControl != sender)
return;
var location = activeControl.Location;
location.Offset(e.Location.X - previousLocation.X, e.Location.Y - previousLocation.Y);
activeControl.Location = location;
}
void Drag_MouseUp(object sender, MouseEventArgs e)
{
activeControl = null;
Cursor = Cursors.Default;
}
These are at the moment having to be manually set to both the custom usercontrol and the chart
It seems like chart overrides your mouse events. So, you can try to add event listeners to chart as well.
PS: We can assign same method to different listeners.

WinForm not dragging smoothly when moving by client area

Whenever I move a Windows Form by some component (i.e. a Label) in the client area, I end up with a strange mouse offset in which the form does not stay visually underneath the cursor. It will still move according to my mouse location on the screen, but it dramatically shifts southeast of the cursor's position.
I've had to specify a negative offset of my own to counteract this offset; my code is as follows:
private void component_MouseDown(object sender, MouseEventArgs e)
{
if (sender is Label)
{
if (e.Button == MouseButtons.Left)
{
mouseLoc = new Point(-(e.X + OFFSET_X), -(e.Y + OFFSET_Y));
isMouseDown = true;
}
}
}
private void component_MouseMove(object sender, MouseEventArgs e)
{
if (isTitleLabelMouseDown)
{
Point p = Control.MousePosition;
p.Offset(mouseLoc);
Location = p;
}
}
private void component_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
}
This offset does fix the problem, but what throws me for a loop is why the form's location offsets when I move it by its client area in the first place?
Thanks!
You seem to be translating client coordinates to screen coordinates. There is a better way...
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.pointtoscreen%28v=vs.110%29.aspx
Edit: And of course there's a better way to do this whole thing. Basically, you want to intercept the click higher up the chain and tell Windows that the click is actually in the window title, which will cause Windows to do the dragging for you...
Winforms - WM_NCHITEST message for click on control

Unable to set the panel location to the current mouse position inside splitContainer?

I have panel1 in my form, i set the visible property to panel1.Visible=false; I want to show this panel wherever i click on the screen.
I need to grab the current mouse location and then display the panel1 where the top-left corner must be in the same point as the mouse cursor !
Sorry for being so beginner but i really stuck on how to do it.
Code that i tried :
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
panel1.Location = e.Location;
panel1.Show();
}
It might this will be your guide to your task, just use the .PointToScreen and .GetCellDisplayRectangle Method
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex == -1) return;
var cellRectangle = dataGridView1.PointToScreen(
dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Location);
panel1.Location = new Point(cellRectangle.X + 50, cellRectangle.Y - 175);
panel1.Show();
}
As far I can recognize your problem you should use PointToScreen function - more on this here

Getting Form mouse coordinated on PictureBox on mouse click override - C#

I'm drawing a MyPictureBox which inherits from PictureBox, and i override it's OnMouseClick
I set the arguments to be :(MouseEventArgs e) so i get the mouse coordinated when clicked,
The problem is the coordinates are the MyPictureBox relative coordinates while i need the coordinated from the Form.
How can it be done ?
Thanks
First map the passed mouse coordinate to the screen. Then map it back to the client coordinates of the form. So the typical code would look like this, spelled out for clarity:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) {
var pbox = (PictureBox)sender;
var form = this;
var screenPos = pbox.PointToScreen(e.Location);
var formPos = form.PointToClient(screenPos);
// etc..
}
Or the short version:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) {
var formPos = this.PointToClient(pictureBox1.PointToScreen(e.Location));
// etc..
}
The easiest way would be using
this.PointToClient(Cursor.Position);
this will get the position of your mouse cursor relative to your form.

Find the mouse location over a bitmap control and display a rectangle in c#

I want to find the exact mouse location of the panel's display rectangle. I have a panel and assigned a bitmap image on it. I searched long time and found control over mouse location option but it won't fit. Anyone help me in this.
You can use the MouseMove event and exploit the MouseEventArgs:
//panel.MouseMove += new MouseEventHandler(panel_MouseMove);
void panel_MouseMove(object sender, MouseEventArgs e)
{
Point myLocation = e.Location;
//Or e.X and e.Y
}
You can use MouseMove event:
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
Text = String.Format("({0};{1})", e.X, e.Y);
}
You can use e.Location or splitted values e.X and e.Y.

Categories

Resources