Repeated point creation - c#

I am using MousePosition to try to determine which way the user moves his or her mouse after the MouseDown event is triggered. The problem is I am trying to make a starting variable using Point datatype and I only want this Point to be filled with data once but I have it in a MouseDown so I can use the mouse arguments e.X and e.Y to determine where the MouseDown event was called. Here is an example of what im thinking of
public void panel1_MouseDown(object sender, MouseEventArgs e)
{
Point start = new Point(e.X, e.Y);
}
public void panel1_MouseMove(object sender, MouseEventArgs e)
{
if(MousePosition.X > start.X)
{
Console.WriteLine("you have moved right");
}
}
so how would I only create that start variable once while still having it have access to the e mouse argument. and also how would I access the variable between panel1_MouseDown and panel1_MouseMove? If you see a better alternative to doing this too that would be great! Thank you all for the help!

If you have aversion to define new class members (for some reason), this is the least you could have:
Point? start = null;
public void panel1_MouseDown(object sender, MouseEventArgs e)
{
start = new Point(e.X, e.Y);
}
public void panel1_MouseUp(object sender, MouseEventArgs e)
{
start = null;
}
public void panel1_MouseMove(object sender, MouseEventArgs e)
{
if(start.HasValue)
if(MousePosition.X > start.Value.X)
Console.WriteLine("you have moved right");
}
But for clarity (and functionality) I'd have an additional boolean variable:
bool mouseIsDown = false;
Point start;
public void panel1_MouseDown(object sender, MouseEventArgs e)
{
start = new Point(e.X, e.Y);
mouseIsDown = true;
}
public void panel1_MouseUp(object sender, MouseEventArgs e)
{
mouseIsDown = false;
}
public void panel1_MouseMove(object sender, MouseEventArgs e)
{
if(mouseIsDown)
if(MousePosition.X > start.X)
Console.WriteLine("you have moved right");
}

declare your start point as private class variable
Private Point start;
public void panel1_MouseDown(object sender, MouseEventArgs e)
{
this.start = new Point(e.X, e.Y);
}
public void panel1_MouseMove(object sender, MouseEventArgs e)
{
if(MousePosition.X > this.start.X)
{
Console.WriteLine("you have moved right");
}
}

Related

C# - Mouse Down event for form is not working

I'm trying to make draggable window form, but when I tried to detect mouse down and up event to form, but event does not fire.
Other things seems working but those 2 events are not.
public form()
{
InitializeComponent();
this.BackColor = Color.Fuchsia;
this.TransparencyKey = this.BackColor;
Debug.Write("initialized");
}
private void form_MouseDown(object sender, MouseEventArgs e)
{
Debug.Write("dragging..");
}
private void form_MouseUp(object sender, MouseEventArgs e)
{
Debug.Write("drag done");
}
it is better to add a panel to your form and set Dock to top in order to cover the title bar area. then use this sample code
private bool isDragging = false;
private Point lastLocation;
private void panel_MouseDown(object sender, MouseEventArgs e)
{
isDragging = true;
lastLocation = e.Location;
}
private void panel_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
this.Location = new Point(
(this.Location.X - lastLocation.X) + e.X,
(this.Location.Y - lastLocation.Y) + e.Y);
this.Update();
}
}
private void panel_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
don't forget to add handlers in the form constructor
panel.MouseDown += panel_MouseDown;
panel.MouseMove += panel_MouseMove;
panel.MouseUp += panel_MouseUp;
In your constructor for form, register those methods your using to listen for the mouse down events like this.
public form() {
InitializeComponent();
this.BackColor = Color.Fuchsia;
this.TransparencyKey = this.BackColor;
Debug.Write("initialized");
//register the methods to listen for the events
this.MouseDown += form_MouseDown;
this.MouseUp += form_MouseUp;
}
private void form_MouseDown(object sender,
MouseEventArgs e) {
Debug.Write("dragging..");
}
private void form_MouseUp(object sender,
MouseEventArgs e) {
Debug.Write("drag done");
}

Problems with Image Tag in Picturebox

So, i try to learn how to Drag and Drop an Image from one PictureBox to another and that works well. But how can i drag and drop the Image TAG from pictureox1 to picturebox2?
i currently have 3 source images and 3 drop boxes.
the dropbox6 is locked with a countdown after a buttonclick (see button2)
(later i will lock all dropboxes)
after i hit this button a countdown starts and if the countdown is 0 (ZERO) only then i can drag one of the 3 images to this box.
however, i have given each of these 3 images in the souceboxes a TAG name.
how can i drop this tagname also to the dropbox?
here is what i have so far:
(the Label labeled "Counter" is actually Label4 in the code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Game
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
// pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.Copy);
((PictureBox)sender).DoDragDrop(((PictureBox)sender).Image, DragDropEffects.Copy);
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox4.AllowDrop = true;
pictureBox5.AllowDrop = true;
pictureBox6.AllowDrop = true;
pictureBox6.Enabled = false;
}
private void pictureBox4_DragEnter(object sender, DragEventArgs e)
{
if(e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Copy;
}
}
private void pictureBox4_DragLeave(object sender, EventArgs e)
{
}
private void pictureBox4_DragDrop(object sender, DragEventArgs e)
{
Image getPicture = (Bitmap) e.Data.GetData(DataFormats.Bitmap);
pictureBox4.Image = getPicture;
}
private void pictureBox5_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Copy;
}
}
private void pictureBox5_DragLeave(object sender, EventArgs e)
{
}
private void pictureBox5_DragDrop(object sender, DragEventArgs e)
{
Image getPicture = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
pictureBox5.Image = getPicture;
}
private void pictureBox6_DragDrop(object sender, DragEventArgs e)
{
Image getPicture = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
pictureBox6.Image = getPicture;
timer1.Enabled = false;
}
private void pictureBox6_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Copy;
timer1.Enabled = false;
}
}
private void pictureBox6_DragLeave(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void pictureBox4_Click(object sender, EventArgs e)
{
MessageBox.Show("test");
}
private void timer1_Tick(object sender, EventArgs e)
{
counter--;
if (counter == 0)
timer1.Stop();
label4.Text = counter.ToString();
if(counter == 0)
{
pictureBox6.Enabled = true;
label4.Enabled = false;
timer1.Stop();
}
}
private void button1_Click(object sender, EventArgs e)
{
System.Windows.Forms.Application.Exit();
}
private void pictureBox6_Click(object sender, EventArgs e)
{
MessageBox.Show(pictureBox6.Tag.ToString());
}
private int counter = 3;
private void button2_Click(object sender, EventArgs e)
{
int counter = 3;
timer1 = new Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000; // 1 second
timer1.Start();
label4.Text = counter.ToString();
if(label4.Text == "0")
{
timer1.Stop();
}
pictureBox6.Image = Properties.Resources.shoreSite_d_1_l_x_x;
button2.Visible=false;
}
}
internal class bild1
{
private Bitmap shoreSite_d_1_l_x_x;
public bild1(Bitmap shoreSite_d_1_l_x_x)
{
this.shoreSite_d_1_l_x_x = shoreSite_d_1_l_x_x;
}
}
}
In MouseDown you are saying what you want to transmit in the DoDragDrop call. In your current case you are transmitting the image ((PictureBox)sender).Image but you can chose to transmit the tag as well...
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
((PictureBox)sender).DoDragDrop(((PictureBox)sender).Image, DragDropEffects.Copy);
((PictureBox)sender).DoDragDrop(((PictureBox)sender).Tag, DragDropEffects.Copy);
}
...Then make sure to parse for each possible input type in DragDrop
private void pictureBox6_DragDrop(object sender, DragEventArgs e)
{
var image = e.Data.GetData(DataFormats.Bitmap) as Bitmap;
var tag = e.Data.GetData(DataFormats.Text) as string;
if (image != null)
{
pictureBox4.Image = image;
}
if (tag != null)
{
pictureBox4.Tag = tag;
}
timer1.Enabled = false;
}
Beware, that you will need to update all the code that checks that the data is Bitmap before it arrives at DragDrop ie in DragEnter and write code that handles both Bitmap and Text, otherwise the Drag functionality will break.
AWESOME!!
It works now. I will continue this Project.

Setting a "mouseDown" bool using labels

I was trying to set up a system to check if the mouse is clicked in a winforms application and have ended up doing this
private void lblChips_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
}
private void lblChips_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
eight times over (for each label).
Does anyone know if there's a more efficient way of doing this? I tried using
private void frmGame4_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
}
private void frmGame4_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
but that doesnt work. Any help would be greatly appreciated.
this
public Form1()
{
InitializeComponent();
CollectFormControls(this);
ControlList.Add(this);
MergeEvents();
}
List<Control> ControlList = new List<Control>();
private void CollectFormControls(Control c)
{
foreach (Control control in c.Controls)
{
ControlList.Add(control);
CheckSubControls(control);
}
}
private void CheckSubControls(Control control)
{
if (control.Controls.Count > -1)
{
CollectFormControls(control);
}
}
private void MergeEvents()
{
for (int i = 0; i < ControlList.Count; i++)
{
ControlList[i].MouseDown += All_MouseDown;
ControlList[i].MouseUp += All_MouseUp;
}
}
public void All_MouseUp(object sender, MouseEventArgs e)
{
this.Text = "up";
}
public void All_MouseDown(object sender, MouseEventArgs e)
{
this.Text = "down";
}
You could make two methods, one for mouse up and one for mouse down, like this:
private void labels_mouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
}
private void labels_mouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
Then, link all the labels to these methods. You can also do this from Code:
label.mouseDown += labels_mouseDown;
label.mouseUp += labels_mouseUp;
This has to be done somewhere under the InitializeComponent(); method. You still have to do this for every label but in the end it's less code.

How to move a borderless form only vertically?

I have created a winform application for school project. But in certain situation I want to let user to move the form only vertically by dragging the form.
So, I have tried this.
private bool dragging = false;
private Point pointClicked;
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (dragging)
{
Point pointMoveTo;
pointMoveTo = this.PointToScreen(new Point(e.Y,e.Y));
pointMoveTo.Offset(-pointClicked.X, -pointClicked.Y);
this.Location = pointMoveTo;
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
dragging = true;
pointClicked = new Point(e.X, e.Y);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
dragging = false;
}
But it doesn't seem to work. It moves the form across the screen. So, is there a way to restrict form movement to vertical?
You should not set this.Location, but only the this.Location.Y value:
this.Location = pointToMoveTo;
should become
this.Top = pointToMoveTo.Y;
Your original code changed both the X and Y coordinates, effectively doing a diagonal move.
Try something like this:
public partial class Form1 : Form
{
private bool dragging = false;
private int pointClickedY;
public Form1() {
InitializeComponent();
}
private void Form1_MouseDown(object sender, MouseEventArgs e) {
dragging = true;
pointClickedY = e.Y;
}
private void Form1_MouseUp(object sender, MouseEventArgs e) {
dragging = false;
}
private void Form1_MouseMove(object sender, MouseEventArgs e) {
if (dragging) {
int pointMoveToY = e.Y;
int delta = pointMoveToY - pointClickedY;
Location = new Point(Location.X, Location.Y + delta);
}
}
}

Timer event c# Crash

I have a timer event as given below and I have got some suggestions from this post. Could you suggest me what is wrong with this? I am getting the following crash:
Unable to cast object of type System.Windows.Forms.Timer to type System.Windows.Forms.Button.
Any suggestions on where I am going wrong??
public MainForm()
{
InitializeComponent();
ButtonTimer.Tick += new EventHandler(ButtonTimer_Tick);
ButtonTimer.Interval = 100;
}
private void ButtonTimer_Tick(object sender, EventArgs e)
{
Button CurrentButton = (Button)sender;
string PressedButton = CurrentButton.Name;
switch (PressedButton)
{
case "BoomUp":break;
}
}
private void BoomUp_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//ButtonTimer.Enabled = true;
ButtonTimer.Start();
}
}
private void BoomUp_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ButtonTimer.Stop();
}
}
You have a problem in ButtomTime_Tick method :
Button CurrentButton = (Button)sender;
sender is not a Button, it's a Timer.
So what you gonna do now ?
You could add a private field in your class
private Button currentButton_;
and
private void BoomUp_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
currentButton_ = e.Button;//but I don't know if e.Button has a Name "BoomUp"
//you can set the name manually if needed :
currentButton_.Name = "BoomUp";
//ButtonTimer.Enabled = true;
ButtonTimer.Start();
}
}
and
private void ButtonTimer_Tick(object sender, EventArgs e)
{
switch (currentButton_.Name)
{
case "BoomUp":break;
}
}
In ButtonTimer_Tick the sender is the timer, not a button. Hence you cast a timer into a button (1st line in that method).

Categories