I have this code :
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if(chckeckbox.checked)
{
//the distance that the mouse made when the left click remained clicked
}
}
by what do I have to change the comments so that it works?
in addition to that my if it already finds in an event so I am a little lost ... I have to put this if in another event or it is well here?
PS : i've ever tried something like that but it's gives me 0 all time.
Form1 testa = new Form1();
Point i = testa.Location;
Point z = testa.Location;
int res = i.X - z.X;
int pls = i.Y - z.Y;
Handle MouseDown and keep the point. Then in MouseClick event calculate the distance:
Point p1;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
p1 = e.Location;
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
MessageBox.Show($"dx = {e.Location.X - p1.X}, dy= {e.Location.Y - p1.Y}");
}
Related
It seems like my script is not being executed. I created my own exit button, pressed on "view code" and added this line:
public void leaveButton_Click ( object sender, EventArgs e )
{
this.Close();
}
I tried the same with:
Application.Exit();
I thought that it might just not work on an image (which it actually should?) so I created a button with the same function... The same result; nothing.
I ignored it for the first part and looked for some different lines to add. My borders are set to none but I still wanted it to be movable. So I added this:
private bool mouseDown;
private Point lastLocation;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
lastLocation = e.Location;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown)
{
this.Location = new Point(
(this.Location.X - lastLocation.X) + e.X, (this.Location.Y - lastLocation.Y) + e.Y);
this.Update();
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
I do not truly understand it so there might be an error in it. It does nothing.
I have an PictureBox that I want to move up on the y axis after a button click. The problem is that the PictureBox simply appears there after the button click. I want it to move to the new position, not teleport. What do I do?
public partial class Form1 : Form
{
Point stageplus1 = new Point(164, 325);
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox5.Location = stageplus1;
}
private void pictureBox5_Click(object sender, EventArgs e)
{
}
}
Expanding on BJ Myers comment this is how you can implement that:
private void button2_Click(object sender, EventArgs e)
{
this.timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
// calculate new position
this.pictureBox1.Top++;
this.pictureBox1.Left++;
// when to stop
if (this.pictureBox1.Top > (this.Height - (2 * this.pictureBox1.Height)))
{
this.timer1.Enabled = false;
}
}
I used the default Timer control that raises a Tick at an interval. The Tick event gets executed on the UI thread so you don't have to be bothered by cross thread errors.
If added to your form this is what you'll get:
If you need to animate more stuff you might want to look into using a Background worker and helper classes like I show in this answer of mine
When is try with code, there appear two label and when move, screen become white from where they move. I want single label move with mouse move.
bool mDown = false;
private void label13_MouseMove(object sender, MouseEventArgs e)
{
if (mDown)
{
label13.Location = e.Location;
}
}
private void label13_MouseDown(object sender, MouseEventArgs e)
{
mDown = true;
}
private void label13_MouseUp(object sender, MouseEventArgs e)
{
mDown = false;
}
The e.Location gives you a mouse position relative to the control that is being clicked. So to fix that, instead of
label13.Location = e.Location;
use
var pos = this.PointToClient(Cursor.Position);
label13.Location = new Point(pos.X - offset.X, pos.Y - offset.Y);`
Create the offset variable as a property of the form (type Point) and initialize it on the mouse down event:
offset = e.Location;
I'm creating this program in which the user drags/drops a text block into a rectangle, and then store the contents of that specific textblock when he/she drops it into the rectangle.
I figured out how to do the drag/drop, but I just can't figure out how to check if the rectangle contains the textblock.
Here's the code so far:
bool captured = false;
double x_shape, x_canvas, y_shape, y_canvas;
UIElement source = null;
private void shape_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
source = (UIElement)sender;
Mouse.Capture(source);
captured = true;
x_shape = Canvas.GetLeft(source);
x_canvas = e.GetPosition(LayoutRoot).X;
y_shape = Canvas.GetTop(source);
y_canvas = e.GetPosition(LayoutRoot).Y;
}
private void shape_MouseMove(object sender, MouseEventArgs e)
{
if (captured)
{
double x = e.GetPosition(LayoutRoot).X;
double y = e.GetPosition(LayoutRoot).Y;
x_shape += x - x_canvas;
Canvas.SetLeft(source, x_shape);
x_canvas = x;
y_shape += y - y_canvas;
Canvas.SetTop(source, y_shape);
y_canvas = y;
}
}
private void shape_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Mouse.Capture(null);
captured = false;
}
private void rectangle1_MouseEnter(object sender, MouseEventArgs e)
{
if (Mouse.Capture(null))
{
textBox1.Text = "test";
}
}
The "Shape" events apply to the textblock by the way, just for clarification.
I tried to find a shortcut and make it so with the rectangle1_MouseEnter event that if the mouse isn't clicked, then store the values (didn't include the code for the storing values). However, the problem is, this idea doesn't work because textBox1.Text="test" isn't registered, and I don't see why it isn't.
we able to move windows forms when we mouse down on title bar .
but how can I move windows when mouse down in form ?
You'll need to record when the mouse is down and up using the MouseDown and MouseUp events:
private bool mouseIsDown = false;
private Point firstPoint;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
firstPoint = e.Location;
mouseIsDown = true;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseIsDown = false;
}
As you can see, the first point is being recorded, so you can then use the MouseMove event as follows:
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseIsDown)
{
// Get the difference between the two points
int xDiff = firstPoint.X - e.Location.X;
int yDiff = firstPoint.Y - e.Location.Y;
// Set the new point
int x = this.Location.X - xDiff;
int y = this.Location.Y - yDiff;
this.Location = new Point(x, y);
}
}
You can do it manually by handling the MouseDown event, as explained in other answers. Another option is to use this small utility class I wrote some time ago. It allows you to make the window "movable" automatically, without a line of code.
Listen for the event when the mouse button goes down in the form and then listen for mouse moves until it goes up again.
Here's a codeproject article that shows how to do this: Move window/form without Titlebar in C#
You can't use location provided in MouseUp or Down, you should use system location like this
private Point diffPoint;
bool mouseDown = false;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
//saves position difference
diffPoint.X = System.Windows.Forms.Cursor.Position.X - this.Left;
diffPoint.Y = System.Windows.Forms.Cursor.Position.Y - this.Top;
mouseDown = true;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown)
{
this.Left = System.Windows.Forms.Cursor.Position.X - diffPoint.X;
this.Top = System.Windows.Forms.Cursor.Position.Y - diffPoint.Y;
}
}
This works, tested.