0

I have a very specific job - I have to overlay a transparent label over a photo of some text of the same font and size. This requires a simple control scheme for the label to move 1 pixel in each direction using a set of 4 buttons, as the location of the scanned text under the label might change batch to batch. These are the buttons I have:

4 buttons for left, right, up and down

I am aware that a control's position is based on its parent. In this case that might have changed as I have this for the overlayed label:

    var pos = this.PointToScreen(lblOverlay.Location);
    pos = pbMain.PointToClient(pos);
    lblOverlay.Parent = pbMain;
    lblOverlay.Location = pos;
    lblOverlay.BackColor = Color.Transparent;

This changes the parent of the label to the picturebox under it. What I cannot explain is why in the winforms builder, the label's location is 65; 70, but when I want to change it's location using a different method than the buttons I mentioned, I have to use half of those values:

lblOverlay.Location = new Point(31, 35);

So in the end my question is this: How would I go about coding the buttons so that they nudge the label's position by 1 in the corresponding direction? There only needs to be an example of one of the buttons, I should be able to figure out the other three.

kiryakov
  • 145
  • 1
  • 7
  • Best use a common Click event: `if (sender == leftButton) label.Left--; else...`. I would also add checking for e.g.the shift key: `int f = Control.ModifierKeys.HasFlag(Keys.ShiftKey) ? 10 : 1;`and the use `-=f` etc.. – TaW Aug 08 '20 at 15:12
  • https://stackoverflow.com/a/13228495/17034 – Hans Passant Aug 08 '20 at 15:15

1 Answers1

1

Simple example of TaW's suggestion from the comments.

Note that I changed to Control.ModifierKeys==Keys.Shift as that will be true when ONLY the Shift key is pressed. As it was originally originally written, it would be true if you held down Ctrl or Alt with the Shift Key.

private void Form1_Load(object sender, EventArgs e)
{
    Point pt = pbMain.PointToClient(lblOverlay.PointToScreen(new Point(0, 0)));
    lblOverlay.Parent = pbMain;
    lblOverlay.Location = pt;
    lblOverlay.BackColor = Color.Transparent;
}

private void btnAll_Click(object sender, EventArgs e)
{            
    int jump = Control.ModifierKeys==Keys.Shift ? 10 : 1;
    if (sender == btnLeft)
    {
        lblOverlay.Left -= jump;
    }
    else if(sender == btnRight)
    {
        lblOverlay.Left += jump;
    }
    else if (sender == btnUp)
    {
        lblOverlay.Top -= jump;
    }
    else if (sender == btnDown)
    {
        lblOverlay.Top += jump;
    }
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40