Click here to Skip to main content
14,600,150 members
   

C#

 
GeneralRe: Can not compare a file and same file stored in mysql - sha256 Pin
wilcodk30-Apr-20 1:28
Memberwilcodk30-Apr-20 1:28 
AnswerRe: Can not compare a file and same file stored in mysql - sha256 Pin
Eddy Vluggen30-Apr-20 6:34
professionalEddy Vluggen30-Apr-20 6:34 
QuestionCompare string time to timer time (noobie) Pin
Member 1481029228-Apr-20 5:36
MemberMember 1481029228-Apr-20 5:36 
AnswerRe: Compare string time to timer time (noobie) Pin
Gerry Schmitz28-Apr-20 6:48
mveGerry Schmitz28-Apr-20 6:48 
QuestionResize and move a drawn rectangle in win forms Pin
Member 1223285027-Apr-20 12:23
MemberMember 1223285027-Apr-20 12:23 
AnswerRe: Resize and move a drawn rectangle in win forms Pin
Luc Pattyn27-Apr-20 13:17
sitebuilderLuc Pattyn27-Apr-20 13:17 
GeneralRe: Resize and move a drawn rectangle in win forms Pin
Member 1223285027-Apr-20 19:38
MemberMember 1223285027-Apr-20 19:38 
GeneralRe: Resize and move a drawn rectangle in win forms Pin
Luc Pattyn28-Apr-20 4:50
sitebuilderLuc Pattyn28-Apr-20 4:50 
Hi,

that still is too complex to my taste.
This is what works for me:

1.
I created my own Rect class, which basically is a rectangle, however it hides all its details from the Form/Panel/PictureBox.
Having a class, however simple it initially is, allows me:
- to treat the thing as an object;
- to make the code more readable;
- to add, at a later time, extra features, e.g. a more elaborate paint behavior without affecting the caller's code.

2.
There is no redundant data, reducing the probability of logic errors.

3.
My MouseDown and MouseUp handlers are extremely short, and my MouseMove handler, which holds all the logic, clearly has two parts (mouse up and mouse down).

using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RectMove {
	public partial class Form1 : Form {
		Rect rect;
		bool m_mouseDown = false;
		Rect movingRect = null;
		Point prevMouseLoc;


		public Form1() {
			InitializeComponent();
			this.DoubleBuffered = true;
		}

		private void Form1_Load(object sender, EventArgs e) {
			rect = new Rect(200, 100, 100, 50);
		}

		private void panel1_Paint(object sender, PaintEventArgs e) {
			Graphics g = e.Graphics;
			rect.Paint(g);
		}

		private void panel1_MouseDown(object sender, MouseEventArgs e) {
			m_mouseDown = true;
			prevMouseLoc = e.Location;
		}

		private void panel1_MouseUp(object sender, MouseEventArgs e) {
			m_mouseDown = false;
		}

		private void panel1_MouseMove(object sender, MouseEventArgs e) {
			if (m_mouseDown) {
				if (movingRect != null) {
					Point mouseLoc = e.Location;
					movingRect.Move(prevMouseLoc, mouseLoc);
					prevMouseLoc = mouseLoc;
					Invalidate(true);
				}
			} else {
				movingRect = null;
				if (rect.Contains(e.Location)) movingRect = rect;
			}
			Console.WriteLine("movingRect=" + movingRect);
		}
	}


public class Rect {
    private static Pen pen=Pens.Red;
    private Rectangle rect;

    public Rect(int x, int y, int w, int h) {
        rect = new Rectangle(x, y, w, h);
    }

    public override string ToString() {
        return "Rect" + rect.Location+rect.Size.ToString();
    }

    public void Move(Point oldLoc, Point newLoc) {
        rect.X += newLoc.X - oldLoc.X;
        rect.Y += newLoc.Y - oldLoc.Y;
    }

    public bool Contains(Point pt) {
        return rect.Contains(pt);
    }

    public void Paint(Graphics g) {
        g.DrawRectangle(pen, rect);
    }
}


Note: I don't clear movingRect after a move, as one could move a rectangle, then release and press the mouse button again without moving the mouse at all, so the rectangle IMO should continue its move.

Smile | :)
Luc Pattyn [My Articles] Nil Volentibus Arduum


modified 28-Apr-20 10:55am.

GeneralRe: Resize and move a drawn rectangle in win forms Pin
Member 1223285028-Apr-20 17:56
MemberMember 1223285028-Apr-20 17:56 
GeneralRe: Resize and move a drawn rectangle in win forms Pin
Luc Pattyn29-Apr-20 2:30
sitebuilderLuc Pattyn29-Apr-20 2:30 
QuestionDate compare Pin
Carlos5827-Apr-20 10:42
MemberCarlos5827-Apr-20 10:42 
AnswerRe: Date compare Pin
Luc Pattyn27-Apr-20 11:00
sitebuilderLuc Pattyn27-Apr-20 11:00 
GeneralRe: Date compare Pin
Carlos5827-Apr-20 12:31
MemberCarlos5827-Apr-20 12:31 
GeneralRe: Date compare Pin
Luc Pattyn27-Apr-20 12:48
sitebuilderLuc Pattyn27-Apr-20 12:48 
GeneralRe: Date compare Pin
Carlos5828-Apr-20 2:19
MemberCarlos5828-Apr-20 2:19 
GeneralRe: Date compare Pin
Luc Pattyn28-Apr-20 2:24
sitebuilderLuc Pattyn28-Apr-20 2:24 
SuggestionRe: Date compare Pin
Richard Deeming28-Apr-20 0:40
mveRichard Deeming28-Apr-20 0:40 
AnswerRe: Date compare Pin
Carlos5828-Apr-20 2:23
MemberCarlos5828-Apr-20 2:23 
QuestionRename files in WinScp Directory using C# Pin
Sriram Valluri27-Apr-20 1:13
MemberSriram Valluri27-Apr-20 1:13 
AnswerRe: Rename files in WinScp Directory using C# Pin
ZurdoDev27-Apr-20 1:21
mveZurdoDev27-Apr-20 1:21 
GeneralRe: Rename files in WinScp Directory using C# Pin
Sriram Valluri27-Apr-20 2:15
MemberSriram Valluri27-Apr-20 2:15 
GeneralRe: Rename files in WinScp Directory using C# Pin
ZurdoDev27-Apr-20 2:27
mveZurdoDev27-Apr-20 2:27 
AnswerRe: Rename files in WinScp Directory using C# Pin
Jin Vincent27-Apr-20 2:36
MemberJin Vincent27-Apr-20 2:36 
GeneralRe: Rename files in WinScp Directory using C# Pin
Sriram Valluri27-Apr-20 8:07
MemberSriram Valluri27-Apr-20 8:07 
AnswerRe: Rename files in WinScp Directory using C# Pin
Luc Pattyn27-Apr-20 8:21
sitebuilderLuc Pattyn27-Apr-20 8:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.