I'm with a problem moving a label on a panel. When I move this label, reached the top and left (0.0), the label respects the top and left. To spend half of the screen, the label exceeds the panel as shown in picture.
My codes:
public partial class frmStandard : Form
{
Point startposition;
}
public void MouseDown(object sender, MouseEventArgs e)
{
startposition = e.Location;
}
public void MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
((Label)sender).Left = Math.Max(0, e.X + ((Label)sender).Left - startposition.X);
((Label)sender).Top = Math.Max(0, e.Y + ((Label)sender).Top - startposition.Y);
}
}
I need the label does not exceed the panel size.What should be added in the codes?
You need to check the other Borders.
You will have to use the dimension data of the containing Panel for this.
In the spirit of keeping things dynamic, as your code already is, I use he Label's Parent instead of referring to just the one Panel:
private void MouseMove(object sender, MouseEventArgs e)
{
Label L = (Label)sender;
Rectangle PR = L.Parent.ClientRectangle;
if (e.Button == MouseButtons.Left)
{
L.Left = Math.Min(Math.Max(0, e.X + L.Left - startposition.X), PR.Right - L.Width);
L.Top = Math.Min( Math.Max(0, e.Y + L.Top - startposition.Y), PR.Bottom - L.Height);
}
}
To keep it even more general, one could replace Label with Control and let the user move other Controls around wit the same pieces of code..
Related
i tried this code but when i drag and move label inside panel is normal but when i drag to edge bound of panel it disappear i want it to stop on panel bound is there a way for that?
private void lbl29_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
panel1.Invalidate();
}
private void lbl29_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
lbl29.Left = e.X + lbl29.Left - MouseDownLocation.X;
lbl29.Top = e.Y + lbl29.Top - MouseDownLocation.Y;
}
}
You can try these:
Controls.SetChildIndex(lbl29, 0); or
Controls.SetChildIndex(panel1, 1); or
lbl29.BringToFront(); or
panel1.SendToBack();
But Controls.SetChildIndex(lbl29, 0); and Controls.SetChildIndex(panel1, 1); are more useful.
I've been trying to make a drag and drop game. I have 4 panels and 4 labels. You have to drag the labels on top of the correct panel.
The problem is checking if a label is on top of the panel. The user can frely drag the labels.
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
button1.Left = e.X + button1.Left - MouseDownLocation.X;
button1.Top = e.Y + button1.Top - MouseDownLocation.Y;
}
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}
Here is the code i used to move the control. I have to mention that this is a test project, so I used a button instead of a label, but the idea is the same.
Is there any way if I can check whether a control is on top of another or not ?
After each move, simply get the Rectangle from the Bounds property of your button and panel, then use either Intersect() or Contains():
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
button1.Location = new Point(e.X + button1.Left - MouseDownLocation.X, e.Y + button1.Top - MouseDownLocation.Y);
Rectangle btnRC = button1.Bounds;
Rectangle pnlRC = panel1.Bounds;
// see if the rectangles INTERSECT
if (pnlRC.IntersectsWith(btnRC))
{
panel1.BackColor = Color.Green;
}
else
{
panel1.BackColor = this.BackColor;
}
// see if the panel COMPLETELY CONTAINS the button
if (pnlRC.Contains(btnRC))
{
panel1.BackColor = Color.Green;
}
else
{
panel1.BackColor = this.BackColor;
}
}
}
To check if the mouse is over the control, you can check if the Cursor.Position is in the ClientRectangle of the control, but you first need to call PointToClient method of the control to convert the cursor position relative to the panel's (0,0), for example:
var b = panel1.ClientRectangle.Contains(panel1.PointToClient(Cursor.Position));
The flag can be checked in the same event handler which is used to move the control, for example:
if(b) panel1.BackColor = Color.Red; else panel1.BackColor = Color.Gray;
This is more of a question of methodology. I know how to create a control that scales from all sides, but I don't understand why this code doesn't work fluidly without redraw issues (jittering). It scales the panel from the top only, but jitters in the process. What am I missing?
public partial class Form1 : Form
{
Point MousePoint = new Point();
public Form1()
{
InitializeComponent();
panel1.MouseMove += Panel1_MouseMove;
panel1.MouseDown += Panel1_MouseDown;
panel1.Width = 100;
panel1.Height = 100;
}
private void Panel1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MousePoint = e.Location;
}
}
private void Panel1_MouseMove(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
panel1.Top = e.Location.Y + panel1.Location.Y - MousePoint.Y;
panel1.Height = panel1.Height - e.Y + MousePoint.Y;
}
}
}
You are changing two different properties: the top, then the height. That could cause the "jittering" you are seeing.
Try using the SetBounds call instead:
panel1.SetBounds(panel1.Left,
e.Location.Y + panel1.Location.Y - mousePoint.Y,
panel1.Width,
panel1.Height - e.Y + mousePoint.Y);
If there are contained controls inside the panel that are anchored, etc, that could affect that smoothness of the resizing, too.
I have a label I am trying to drag. I click on the label, and on the MouseMove() event I am trying to relocate the position of the label.
public void MyLabel_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
((Label)sender).Location = Cursor.Position;
// I have also tried e.Location but none of these moves the label to
// where the the cursor is, always around it, sometimes completely off
}
}
You usually need to store the offset location of the initial mouse down point in the control, or else the control will move on you in a jittering fashion. Then you just do the math:
Point labelOffset = Point.Empty;
void MyLabel_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
labelOffset = e.Location;
}
}
void MyLabel_MouseMove(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
Label l = sender as Label;
l.Location = new Point(l.Left + e.X - labelOffset.X,
l.Top + e.Y - labelOffset.Y);
}
}
sorry to bother again with a similar question. However, I have an issue with my code again in C#. I would like to give you the code I have got so far:
private void pictureBox2_Click(object sender, EventArgs e)
{
PictureBox flower2 = new PictureBox();
flower2.Image = Properties.Resources.Flower3;
flower2.Location = new Point(panel1.Location.X + 10, panel1.Location.Y + 10);
flower2.Size = new System.Drawing.Size(pictureBox2.Size.Width, pictureBox2.Size.Height);
flower2.Parent = panel1;
this.Controls.Add(flower2);
flower2.BringToFront();
flower2.MouseMove += new MouseEventHandler(flower2_MouseMove);
flower2.MouseDown += new MouseEventHandler(flower2_MouseDown);
}
private void flower2_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}
private void flower2_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
flower2.Left = e.X + flower2.Left - MouseDownLocation.X;
flower2.Top = e.Y + flower2.Top - MouseDownLocation.Y;
}
}
What I wanted it to do is when an image is clicked, create a new one and be able to drag and drop it. I succeeded only when I put my code at the top of the page. Which is not what I want because I want to be able to add as many images as I would like. I tried alot of different methods. The errors are at:
flower2.Left = e.X + flower2.Left - MouseDownLocation.X;
flower2.Top = e.Y + flower2.Top - MouseDownLocation.Y;
At all the words under the name of flower2. This is because, I have defined flower2 in pictureBox2_Click so everytime it's clicked a new PictureBox will generate. But, I need to make it so I can generate as many images as I want and without putting it at the top of the page, this makes it so it only uses one image at a time.
Each picturebox you make is routed to those events, thus the sender object is the picturebox, you just need to cast it to the right type then move that.
PictureBox pb = (PictureBox)sender;
pb.Left = e.X + pb.Left - MouseDownLocation.X;