move Form2 according to form1 - c#

My problem is:
i want Form 2 to be moved only when I move form 1, and it will always stay beneath Form1.
i tried everything i could think of: Location point and set desktop position, i tried a timer of realtime moving , i just cant get it , this shouldn't be so difficult :(
i'm using a panel to move form 1
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
mov = 1;
movX = e.X;
movY = e.Y;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (mov == 1)
{
this.SetDesktopLocation(MousePosition.X - movX, MousePosition.Y - movY);
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
mov = 0;
}
i also tried to make One form and put panels in it and make the form transparent, but i get an issue when i'm trying to move the form via the panel.

i found solution on this:
Move Form1 when I move Form2 in C#
in first form:
protected override void OnLocationChanged(EventArgs e)
{
if (secondForm == null)
{
secondForm = new SecondForm();
}
if (secondForm.wasShown == true)
{
secondForm.Location = new Point(this.Left - parentOffset.X,
this.Top - parentOffset.Y);
}
base.OnLocationChanged(e);
}
in the second form:
public bool wasShown = false;
private void SecondForm_Load(object sender, EventArgs e)
{
this.StartPosition = FormStartPosition.Manual;
Location = new Point(topForm.lX -40, topForm.lY +85);
wasShown = true;
this.Owner = topForm;
}

Related

How to move a PictureBlock build dynamically

I haven't found a similar question but maybe I haven't found the good keys word, my apologies if there is one.
It's my first question on this website so please tell me if I should ask my question differently! :)
I'm trying to create some pictureblock in a panel thanks to a button and drag and drop them but I don't know how to call the pictureBlock by clicking on it
I tried this, I'm able to create the block by clicking on a button and I'm able the move the last block created but I can't move other blocks.
I tried many solutions but I don't understand how to call the pictureblock i clicked on instead of calling "structures[i]"
Thank you for your help!
form 1 :
private void button1_Click(object sender, EventArgs e)
{
//ajout_panel1();
ajout_panelI();
}
private void pictureBox_Click(object sender, MouseEventArgs e)
{
pictureBox_MouseDown(this.structures[i], e);
pictureBox_MouseMove(this.structures[i], e);
}
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}
private void pictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
this.structures[i-1].Left = e.X + this.structures[i-1].Left - MouseDownLocation.X;
this.structures[i-1].Top = e.Y + this.structures[i-1].Top - MouseDownLocation.Y; }
}
}
Form 1 designer:
private void ajout_panelI()
{
System.Windows.Forms.PictureBox newbox = new System.Windows.Forms.PictureBox();
this.structures.Add(newbox);
panel1.SuspendLayout();
SuspendLayout();
panel1.Controls.Add(this.structures[i]);
this.structures[i].BackColor = System.Drawing.SystemColors.GradientActiveCaption;
this.structures[i].Location = new System.Drawing.Point(pos_x, pos_y);
this.structures[i].Name = pic_name();
this.structures[i].Size = new System.Drawing.Size(200, 100);
this.structures[i].TabIndex = 0;
this.structures[i].MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseDown);
this.structures[i].MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseMove);
this.structures[i].Paint += new System.Windows.Forms.PaintEventHandler(this.panel2_Paint_1);
panel1.ResumeLayout(false);
ResumeLayout(false);
pos_y = pos_y + 150;
i++;
}
public System.Windows.Forms.Panel panel1;
private System.Windows.Forms.PictureBox panel2;
private System.Windows.Forms.Panel panel3;
private System.Windows.Forms.Button button1;
public int i = 0;
private int pos_x =30;
private int pos_y =30;
private List<System.Windows.Forms.PictureBox> structures { get; set; } = new List<System.Windows.Forms.PictureBox>();
private System.Windows.Forms.PictureBox structure;
private string name;

C# Windows Form Application in VS: this.Close(); && Application.Exit(); isn't working

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.

Graphical error while moving usercontrol (c# win7)

I am having trouble with painting usercontrol on form while moving the control.
The background of the control is shortened if moved to the right or downwards.
My control
public class DotPanel : Control
{
public DotPanel()
{
this.DoubleBuffered = true;
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillRectangle(new SolidBrush(Color.Yellow),new Rectangle(this.Location,this.Size));
}
}
The main form just places the usercontrol on specified location and supplies the mouse down/move/up events to do the moving
Basicaly this>>
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mouseX = e.X;
mouseY = e.Y;
panelX = dp1.Left;
panelY = dp1.Top;
moving = true;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (moving)
{
dp1.Left = panelX + e.X - mouseX;
dp1.Top = panelY + e.Y - mouseY;
this.Invalidate(true);
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
moving = false;
this.Invalidate(true);
}
Thanks in advance for any clues...
the overriden paint was the problem.
exchanging this.Size and this.Location for ClientRectangle.Size and ClientRectangle.Location worked...

Moving Child Form with Parent Form

I've got 2 forms: A Parent and a supporting Child form.
When the Child form is initially displayed, I set something called my "Zero Point", which is where the Child form should be positioned relative to the Parent, and defined by one of the controls on the Parent:
private void miShowChild_Click(object sender, EventArgs e) {
if ((m_childForm == null) || m_childForm .IsDisposed) {
m_formLeft = this.Left + this.Control1.Left;
m_formTop = this.Top + this.Control1.Top;
m_childForm = new ChildForm1();
m_childForm.Show(this);
m_childForm.Parent_Shift(m_formTop, m_formLeft);
}
m_roleMap.TopLevel = true;
}
When the Parent form is moved somewhere else, that information is translated to the Child form:
private void Form1_MoveResize(object sender, EventArgs e) {
if ((m_childForm != null) && !m_childForm.IsDisposed) {
m_formLeft = this.Left + this.Control1.Left;
m_formTop = this.Top + this.Control1.Top;
m_childForm.Parent_Shift(m_formTop, m_formLeft);
}
}
That logic seems OK, right?
So, what happens in the Child form?
First, I've got these variables defined:
private bool m_automate;
private int m_left, m_top;
private Point m_zero;
private void ChildForm_Load(object sender, EventArgs e) {
m_left = 0;
m_top = 0;
m_zero = Point.Empty;
}
When the Child is set using Parent_Shift, I want to get where this "Zero Point" should be defined:
public void Parent_Shift(int parentTop, int parentLeft) {
m_automate = true;
if (m_zero != Point.Empty) {
this.Left = parentLeft - m_left;
this.Top = parentTop - m_top;
} else {
this.Left = parentLeft;
this.Top = parentTop;
}
m_zero = new Point(this.Left, this.Top);
m_automate = false;
}
When the Child form is repositioned, and not because of the Parent moving, then I want to record how far from my "Zero Point" this is:
private void Form_MoveResize(object sender, EventArgs e) {
if (!m_automate && this.Visible) {
if (m_zero != Point.Empty) {
m_left = m_zero.X - this.Left;
m_top = m_zero.Y - this.Top;
} else {
m_zero = new Point(this.Left, this.Top);
}
}
}
All of the methods are firing, but I can't seem to get the coordinate system figured out.
I can move the Child form around manually, then move the Parent form and the Child form warps to some other location - then moves proportional to the Parent.
I want the Child to remain at some (X, Y) coordinates relative to the Parent.
When the Child form is moved, that relative coordinate needs to be updated to be where ever the Child form was left.
Does anyone know how to fix my issue?
Put this code in your parent. You don't need any code in your child form.
public partial class ParentForm : Form
{
private bool updatingChildPosition = false;
private Point childFormOffset = Point.Empty;
private ChildForm child = null;
public ParentForm()
{
InitializeComponent();
this.child = new ChildForm();
child.Show();
child.Move += child_Move;
UpdateChildFormOffset();
}
void child_Move(object sender, EventArgs e)
{
// Child form is moved, store it's new offset
UpdateChildFormOffset();
}
private void UpdateChildFormOffset()
{
if (!updatingChildPosition)
{
this.childFormOffset = new Point(
this.child.Location.X - this.Location.X,
this.child.Location.Y - this.Location.Y);
}
}
private void ParentForm_Move(object sender, EventArgs e)
{
// Updating child position
this.updatingChildPosition = true;
child.Location = new Point(
this.Location.X + childFormOffset.X,
this.Location.Y + childFormOffset.Y);
this.updatingChildPosition = false;
}
}

Drag Drop Picturebox

I am trying to make an application in which i have 7 Pictureboxes and i want to drag drop one each time i click then move the mouse on the form. I did make it to some extend by moving the picture-box on the form but i cannot keep the original image. When i drag drop a picture box i want to drag drop just a copy not the whole picture box on the form. Any help will be greatly appreciated. Kind regards.
public Form1()
{
controller = Controller.getController();
InitializeComponent();
this.AllowDrop = true;
this.pbOR.MouseDown += pbOR_MouseUp;
}
private void pbOR_MouseDown(object sender, MouseEventArgs e)
{
downPoint = e.Location;
pbOR.Parent = this;
pbOR.BringToFront();
}
private void pbOR_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
pbOR.Left += e.X - downPoint.X;
pbOR.Top += e.Y - downPoint.Y;
}
}
private void pbOR_MouseUp(object sender, MouseEventArgs e)
{
Control c = GetChildAtPoint(new Point(pbOR.Left - 1, pbOR.Top));
if (c == null) c = this;
Point newLoc = c.PointToClient(pbOR.Parent.PointToScreen(pbOR.Location));
pbOR.Parent = c;
pbOR.Location = newLoc;
}

Categories