Don't move the Labels outside a PictureBox



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I am creating an application in which I can move the Labels that are on a PictureBox.

The problem is that I want these to only Labels move inside the PictureBox.



Here is my code:



protected void lbl_MouseMove(object sender, MouseEventArgs e)

Label lbl = sender as Label;

try

if (lbl != null && e.Button == MouseButtons.Left)

if (m_lblLocation != new Point(0, 0))

Point newLocation = lbl.Location;
newLocation.X = newLocation.X + e.X - m_lblLocation.X;
newLocation.Y = newLocation.Y + e.Y - m_lblLocation.Y;
lbl.Location = newLocation;
this.Refresh();



catch(Exception ex)


protected void lbl_MouseUp(object sender, MouseEventArgs e)

Label lbl = sender as Label;

try

if (lbl != null && e.Button == MouseButtons.Left)

m_lblLocation = Point.Empty;


catch(Exception ex)


protected void lbl_MouseDown(object sender, MouseEventArgs e)

Label lbl = sender as Label;

try

if (lbl != null && e.Button == MouseButtons.Left)

m_lblLocation = e.Location;


catch(Exception ex)



In above code I have created some mouse events for the Labels.










share|improve this question
























  • just put the labels on a panel, and fill the background of the panel with the image

    – GuidoG
    Nov 15 '18 at 11:55


















1















I am creating an application in which I can move the Labels that are on a PictureBox.

The problem is that I want these to only Labels move inside the PictureBox.



Here is my code:



protected void lbl_MouseMove(object sender, MouseEventArgs e)

Label lbl = sender as Label;

try

if (lbl != null && e.Button == MouseButtons.Left)

if (m_lblLocation != new Point(0, 0))

Point newLocation = lbl.Location;
newLocation.X = newLocation.X + e.X - m_lblLocation.X;
newLocation.Y = newLocation.Y + e.Y - m_lblLocation.Y;
lbl.Location = newLocation;
this.Refresh();



catch(Exception ex)


protected void lbl_MouseUp(object sender, MouseEventArgs e)

Label lbl = sender as Label;

try

if (lbl != null && e.Button == MouseButtons.Left)

m_lblLocation = Point.Empty;


catch(Exception ex)


protected void lbl_MouseDown(object sender, MouseEventArgs e)

Label lbl = sender as Label;

try

if (lbl != null && e.Button == MouseButtons.Left)

m_lblLocation = e.Location;


catch(Exception ex)



In above code I have created some mouse events for the Labels.










share|improve this question
























  • just put the labels on a panel, and fill the background of the panel with the image

    – GuidoG
    Nov 15 '18 at 11:55














1












1








1








I am creating an application in which I can move the Labels that are on a PictureBox.

The problem is that I want these to only Labels move inside the PictureBox.



Here is my code:



protected void lbl_MouseMove(object sender, MouseEventArgs e)

Label lbl = sender as Label;

try

if (lbl != null && e.Button == MouseButtons.Left)

if (m_lblLocation != new Point(0, 0))

Point newLocation = lbl.Location;
newLocation.X = newLocation.X + e.X - m_lblLocation.X;
newLocation.Y = newLocation.Y + e.Y - m_lblLocation.Y;
lbl.Location = newLocation;
this.Refresh();



catch(Exception ex)


protected void lbl_MouseUp(object sender, MouseEventArgs e)

Label lbl = sender as Label;

try

if (lbl != null && e.Button == MouseButtons.Left)

m_lblLocation = Point.Empty;


catch(Exception ex)


protected void lbl_MouseDown(object sender, MouseEventArgs e)

Label lbl = sender as Label;

try

if (lbl != null && e.Button == MouseButtons.Left)

m_lblLocation = e.Location;


catch(Exception ex)



In above code I have created some mouse events for the Labels.










share|improve this question
















I am creating an application in which I can move the Labels that are on a PictureBox.

The problem is that I want these to only Labels move inside the PictureBox.



Here is my code:



protected void lbl_MouseMove(object sender, MouseEventArgs e)

Label lbl = sender as Label;

try

if (lbl != null && e.Button == MouseButtons.Left)

if (m_lblLocation != new Point(0, 0))

Point newLocation = lbl.Location;
newLocation.X = newLocation.X + e.X - m_lblLocation.X;
newLocation.Y = newLocation.Y + e.Y - m_lblLocation.Y;
lbl.Location = newLocation;
this.Refresh();



catch(Exception ex)


protected void lbl_MouseUp(object sender, MouseEventArgs e)

Label lbl = sender as Label;

try

if (lbl != null && e.Button == MouseButtons.Left)

m_lblLocation = Point.Empty;


catch(Exception ex)


protected void lbl_MouseDown(object sender, MouseEventArgs e)

Label lbl = sender as Label;

try

if (lbl != null && e.Button == MouseButtons.Left)

m_lblLocation = e.Location;


catch(Exception ex)



In above code I have created some mouse events for the Labels.







c# winforms drag-and-drop label picturebox






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 10:56









Jimi

9,66542035




9,66542035










asked Nov 15 '18 at 9:32









MickeyMickey

277




277












  • just put the labels on a panel, and fill the background of the panel with the image

    – GuidoG
    Nov 15 '18 at 11:55


















  • just put the labels on a panel, and fill the background of the panel with the image

    – GuidoG
    Nov 15 '18 at 11:55

















just put the labels on a panel, and fill the background of the panel with the image

– GuidoG
Nov 15 '18 at 11:55






just put the labels on a panel, and fill the background of the panel with the image

– GuidoG
Nov 15 '18 at 11:55













2 Answers
2






active

oldest

votes


















2














The PictureBox control is not a container, you can't directly put another control inside it, as you would do with a Panel, a GroupBox or other controls that implement IContainerControl.

You could parent the Label (in this case), setting the Label Parent to a PictureBox handle. The Label.Bounds will then reflect the parent Bounds.

However it's not necessary: you can just calculate the position of the Label in relation to the control that contains both (Label(s) and PictureBox):



You can restrict the movements of other Label controls subscribing to the MovableLabel_MouseDown/MouseUp/MouseMove events.



An example:



bool ThisLabelCanMove;
Point LabelMousePosition = Point.Empty;

private void MovableLabel_MouseDown(object sender, MouseEventArgs e)

if (e.Button == MouseButtons.Left)

LabelMousePosition = e.Location;
ThisLabelCanMove = true;



private void MovableLabel_MouseUp(object sender, MouseEventArgs e)

ThisLabelCanMove = false;


private void MovableLabel_MouseMove(object sender, MouseEventArgs e)

if (ThisLabelCanMove)

Label label = sender as Label;

Point LabelNewLocation = new Point(label.Left + (e.Location.X - LabelMousePosition.X),
label.Top + (e.Location.Y - LabelMousePosition.Y));
LabelNewLocation.X = (LabelNewLocation.X < pictureBox1.Left) ? pictureBox1.Left : LabelNewLocation.X;
LabelNewLocation.Y = (LabelNewLocation.Y < pictureBox1.Top) ? pictureBox1.Top : LabelNewLocation.Y;
LabelNewLocation.X = (LabelNewLocation.X + label.Width > pictureBox1.Right) ? label.Left : LabelNewLocation.X;
LabelNewLocation.Y = (LabelNewLocation.Y + label.Height > pictureBox1.Bottom) ? label.Top : LabelNewLocation.Y;
label.Location = LabelNewLocation;




Label Move Inside Bounds






share|improve this answer






























    0














    you need to track two things:
    1. is the mouse press or not - bool IsMouseDown = false;
    2. the start location of the label- Point StartPoint;



    // mouse is not down
    private void label1_MouseUp(object sender, MouseEventArgs e)

    IsMouseDown = false;



    //mouse is down and set the starting postion
    private void label1_MouseDown(object sender, MouseEventArgs e)

    //if left mouse button was pressed
    if (e.Button == System.Windows.Forms.MouseButtons.Left)

    IsMouseDown = true;
    label1.BringToFront();
    StartPoint = e.Location;




    //check the label is withing the borders of the picture box
    private void label1_MouseMove(object sender, MouseEventArgs e)

    if (IsMouseDown)

    int left = e.X + label1.Left - StartPoint.X;
    int right = e.X + label1.Right - StartPoint.X;
    int top = e.Y + label1.Top - StartPoint.Y;
    int bottom = e.Y + label1.Bottom - StartPoint.Y;
    if (left > pictureBox1.Left && top > pictureBox1.Top && pictureBox1.Bottom >= bottom && pictureBox1.Right >= right)

    label1.Left = left;
    label1.Top = top;








    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316286%2fdont-move-the-labels-outside-a-picturebox%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      The PictureBox control is not a container, you can't directly put another control inside it, as you would do with a Panel, a GroupBox or other controls that implement IContainerControl.

      You could parent the Label (in this case), setting the Label Parent to a PictureBox handle. The Label.Bounds will then reflect the parent Bounds.

      However it's not necessary: you can just calculate the position of the Label in relation to the control that contains both (Label(s) and PictureBox):



      You can restrict the movements of other Label controls subscribing to the MovableLabel_MouseDown/MouseUp/MouseMove events.



      An example:



      bool ThisLabelCanMove;
      Point LabelMousePosition = Point.Empty;

      private void MovableLabel_MouseDown(object sender, MouseEventArgs e)

      if (e.Button == MouseButtons.Left)

      LabelMousePosition = e.Location;
      ThisLabelCanMove = true;



      private void MovableLabel_MouseUp(object sender, MouseEventArgs e)

      ThisLabelCanMove = false;


      private void MovableLabel_MouseMove(object sender, MouseEventArgs e)

      if (ThisLabelCanMove)

      Label label = sender as Label;

      Point LabelNewLocation = new Point(label.Left + (e.Location.X - LabelMousePosition.X),
      label.Top + (e.Location.Y - LabelMousePosition.Y));
      LabelNewLocation.X = (LabelNewLocation.X < pictureBox1.Left) ? pictureBox1.Left : LabelNewLocation.X;
      LabelNewLocation.Y = (LabelNewLocation.Y < pictureBox1.Top) ? pictureBox1.Top : LabelNewLocation.Y;
      LabelNewLocation.X = (LabelNewLocation.X + label.Width > pictureBox1.Right) ? label.Left : LabelNewLocation.X;
      LabelNewLocation.Y = (LabelNewLocation.Y + label.Height > pictureBox1.Bottom) ? label.Top : LabelNewLocation.Y;
      label.Location = LabelNewLocation;




      Label Move Inside Bounds






      share|improve this answer



























        2














        The PictureBox control is not a container, you can't directly put another control inside it, as you would do with a Panel, a GroupBox or other controls that implement IContainerControl.

        You could parent the Label (in this case), setting the Label Parent to a PictureBox handle. The Label.Bounds will then reflect the parent Bounds.

        However it's not necessary: you can just calculate the position of the Label in relation to the control that contains both (Label(s) and PictureBox):



        You can restrict the movements of other Label controls subscribing to the MovableLabel_MouseDown/MouseUp/MouseMove events.



        An example:



        bool ThisLabelCanMove;
        Point LabelMousePosition = Point.Empty;

        private void MovableLabel_MouseDown(object sender, MouseEventArgs e)

        if (e.Button == MouseButtons.Left)

        LabelMousePosition = e.Location;
        ThisLabelCanMove = true;



        private void MovableLabel_MouseUp(object sender, MouseEventArgs e)

        ThisLabelCanMove = false;


        private void MovableLabel_MouseMove(object sender, MouseEventArgs e)

        if (ThisLabelCanMove)

        Label label = sender as Label;

        Point LabelNewLocation = new Point(label.Left + (e.Location.X - LabelMousePosition.X),
        label.Top + (e.Location.Y - LabelMousePosition.Y));
        LabelNewLocation.X = (LabelNewLocation.X < pictureBox1.Left) ? pictureBox1.Left : LabelNewLocation.X;
        LabelNewLocation.Y = (LabelNewLocation.Y < pictureBox1.Top) ? pictureBox1.Top : LabelNewLocation.Y;
        LabelNewLocation.X = (LabelNewLocation.X + label.Width > pictureBox1.Right) ? label.Left : LabelNewLocation.X;
        LabelNewLocation.Y = (LabelNewLocation.Y + label.Height > pictureBox1.Bottom) ? label.Top : LabelNewLocation.Y;
        label.Location = LabelNewLocation;




        Label Move Inside Bounds






        share|improve this answer

























          2












          2








          2







          The PictureBox control is not a container, you can't directly put another control inside it, as you would do with a Panel, a GroupBox or other controls that implement IContainerControl.

          You could parent the Label (in this case), setting the Label Parent to a PictureBox handle. The Label.Bounds will then reflect the parent Bounds.

          However it's not necessary: you can just calculate the position of the Label in relation to the control that contains both (Label(s) and PictureBox):



          You can restrict the movements of other Label controls subscribing to the MovableLabel_MouseDown/MouseUp/MouseMove events.



          An example:



          bool ThisLabelCanMove;
          Point LabelMousePosition = Point.Empty;

          private void MovableLabel_MouseDown(object sender, MouseEventArgs e)

          if (e.Button == MouseButtons.Left)

          LabelMousePosition = e.Location;
          ThisLabelCanMove = true;



          private void MovableLabel_MouseUp(object sender, MouseEventArgs e)

          ThisLabelCanMove = false;


          private void MovableLabel_MouseMove(object sender, MouseEventArgs e)

          if (ThisLabelCanMove)

          Label label = sender as Label;

          Point LabelNewLocation = new Point(label.Left + (e.Location.X - LabelMousePosition.X),
          label.Top + (e.Location.Y - LabelMousePosition.Y));
          LabelNewLocation.X = (LabelNewLocation.X < pictureBox1.Left) ? pictureBox1.Left : LabelNewLocation.X;
          LabelNewLocation.Y = (LabelNewLocation.Y < pictureBox1.Top) ? pictureBox1.Top : LabelNewLocation.Y;
          LabelNewLocation.X = (LabelNewLocation.X + label.Width > pictureBox1.Right) ? label.Left : LabelNewLocation.X;
          LabelNewLocation.Y = (LabelNewLocation.Y + label.Height > pictureBox1.Bottom) ? label.Top : LabelNewLocation.Y;
          label.Location = LabelNewLocation;




          Label Move Inside Bounds






          share|improve this answer













          The PictureBox control is not a container, you can't directly put another control inside it, as you would do with a Panel, a GroupBox or other controls that implement IContainerControl.

          You could parent the Label (in this case), setting the Label Parent to a PictureBox handle. The Label.Bounds will then reflect the parent Bounds.

          However it's not necessary: you can just calculate the position of the Label in relation to the control that contains both (Label(s) and PictureBox):



          You can restrict the movements of other Label controls subscribing to the MovableLabel_MouseDown/MouseUp/MouseMove events.



          An example:



          bool ThisLabelCanMove;
          Point LabelMousePosition = Point.Empty;

          private void MovableLabel_MouseDown(object sender, MouseEventArgs e)

          if (e.Button == MouseButtons.Left)

          LabelMousePosition = e.Location;
          ThisLabelCanMove = true;



          private void MovableLabel_MouseUp(object sender, MouseEventArgs e)

          ThisLabelCanMove = false;


          private void MovableLabel_MouseMove(object sender, MouseEventArgs e)

          if (ThisLabelCanMove)

          Label label = sender as Label;

          Point LabelNewLocation = new Point(label.Left + (e.Location.X - LabelMousePosition.X),
          label.Top + (e.Location.Y - LabelMousePosition.Y));
          LabelNewLocation.X = (LabelNewLocation.X < pictureBox1.Left) ? pictureBox1.Left : LabelNewLocation.X;
          LabelNewLocation.Y = (LabelNewLocation.Y < pictureBox1.Top) ? pictureBox1.Top : LabelNewLocation.Y;
          LabelNewLocation.X = (LabelNewLocation.X + label.Width > pictureBox1.Right) ? label.Left : LabelNewLocation.X;
          LabelNewLocation.Y = (LabelNewLocation.Y + label.Height > pictureBox1.Bottom) ? label.Top : LabelNewLocation.Y;
          label.Location = LabelNewLocation;




          Label Move Inside Bounds







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 10:52









          JimiJimi

          9,66542035




          9,66542035























              0














              you need to track two things:
              1. is the mouse press or not - bool IsMouseDown = false;
              2. the start location of the label- Point StartPoint;



              // mouse is not down
              private void label1_MouseUp(object sender, MouseEventArgs e)

              IsMouseDown = false;



              //mouse is down and set the starting postion
              private void label1_MouseDown(object sender, MouseEventArgs e)

              //if left mouse button was pressed
              if (e.Button == System.Windows.Forms.MouseButtons.Left)

              IsMouseDown = true;
              label1.BringToFront();
              StartPoint = e.Location;




              //check the label is withing the borders of the picture box
              private void label1_MouseMove(object sender, MouseEventArgs e)

              if (IsMouseDown)

              int left = e.X + label1.Left - StartPoint.X;
              int right = e.X + label1.Right - StartPoint.X;
              int top = e.Y + label1.Top - StartPoint.Y;
              int bottom = e.Y + label1.Bottom - StartPoint.Y;
              if (left > pictureBox1.Left && top > pictureBox1.Top && pictureBox1.Bottom >= bottom && pictureBox1.Right >= right)

              label1.Left = left;
              label1.Top = top;








              share|improve this answer



























                0














                you need to track two things:
                1. is the mouse press or not - bool IsMouseDown = false;
                2. the start location of the label- Point StartPoint;



                // mouse is not down
                private void label1_MouseUp(object sender, MouseEventArgs e)

                IsMouseDown = false;



                //mouse is down and set the starting postion
                private void label1_MouseDown(object sender, MouseEventArgs e)

                //if left mouse button was pressed
                if (e.Button == System.Windows.Forms.MouseButtons.Left)

                IsMouseDown = true;
                label1.BringToFront();
                StartPoint = e.Location;




                //check the label is withing the borders of the picture box
                private void label1_MouseMove(object sender, MouseEventArgs e)

                if (IsMouseDown)

                int left = e.X + label1.Left - StartPoint.X;
                int right = e.X + label1.Right - StartPoint.X;
                int top = e.Y + label1.Top - StartPoint.Y;
                int bottom = e.Y + label1.Bottom - StartPoint.Y;
                if (left > pictureBox1.Left && top > pictureBox1.Top && pictureBox1.Bottom >= bottom && pictureBox1.Right >= right)

                label1.Left = left;
                label1.Top = top;








                share|improve this answer

























                  0












                  0








                  0







                  you need to track two things:
                  1. is the mouse press or not - bool IsMouseDown = false;
                  2. the start location of the label- Point StartPoint;



                  // mouse is not down
                  private void label1_MouseUp(object sender, MouseEventArgs e)

                  IsMouseDown = false;



                  //mouse is down and set the starting postion
                  private void label1_MouseDown(object sender, MouseEventArgs e)

                  //if left mouse button was pressed
                  if (e.Button == System.Windows.Forms.MouseButtons.Left)

                  IsMouseDown = true;
                  label1.BringToFront();
                  StartPoint = e.Location;




                  //check the label is withing the borders of the picture box
                  private void label1_MouseMove(object sender, MouseEventArgs e)

                  if (IsMouseDown)

                  int left = e.X + label1.Left - StartPoint.X;
                  int right = e.X + label1.Right - StartPoint.X;
                  int top = e.Y + label1.Top - StartPoint.Y;
                  int bottom = e.Y + label1.Bottom - StartPoint.Y;
                  if (left > pictureBox1.Left && top > pictureBox1.Top && pictureBox1.Bottom >= bottom && pictureBox1.Right >= right)

                  label1.Left = left;
                  label1.Top = top;








                  share|improve this answer













                  you need to track two things:
                  1. is the mouse press or not - bool IsMouseDown = false;
                  2. the start location of the label- Point StartPoint;



                  // mouse is not down
                  private void label1_MouseUp(object sender, MouseEventArgs e)

                  IsMouseDown = false;



                  //mouse is down and set the starting postion
                  private void label1_MouseDown(object sender, MouseEventArgs e)

                  //if left mouse button was pressed
                  if (e.Button == System.Windows.Forms.MouseButtons.Left)

                  IsMouseDown = true;
                  label1.BringToFront();
                  StartPoint = e.Location;




                  //check the label is withing the borders of the picture box
                  private void label1_MouseMove(object sender, MouseEventArgs e)

                  if (IsMouseDown)

                  int left = e.X + label1.Left - StartPoint.X;
                  int right = e.X + label1.Right - StartPoint.X;
                  int top = e.Y + label1.Top - StartPoint.Y;
                  int bottom = e.Y + label1.Bottom - StartPoint.Y;
                  if (left > pictureBox1.Left && top > pictureBox1.Top && pictureBox1.Bottom >= bottom && pictureBox1.Right >= right)

                  label1.Left = left;
                  label1.Top = top;









                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 10:01









                  styxstyx

                  980613




                  980613



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316286%2fdont-move-the-labels-outside-a-picturebox%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo