Dynamic click event on PictureBox

后端 未结 2 2000
说谎
说谎 2021-01-28 18:12

I am getting a list of pictures from a directory and storing the filenames in a List. I then loop through each of these and create a PictureBox<

相关标签:
2条回答
  • 2021-01-28 18:57

    The sender parameter is indeed your PictureBox, downcast to object. Access it this way:

    var pictureBox = sender as PictureBox;
    

    Drawing a border around it could not be so easy as you will have to either override the OnPaint method of the PictureBox, either handle the Paint event.

    You can use this class to draw a black thin border around your image.

    public class CustomBorderPictureBox : PictureBox
    {
        public bool BorderDrawn { get; private set; }
    
        public void ToggleBorder()
        {
            BorderDrawn = !BorderDrawn;
            Invalidate();
        }
    
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            if (BorderDrawn)
                using (var pen = new Pen(Color.Black))
                    pe.Graphics.DrawRectangle(pen, 0, 0, Width - 1, Height - 1);
        }
    }
    
    0 讨论(0)
  • 2021-01-28 19:00

    sender is the PictureBox that was clicked:

    private void PictureClick(object sender, EventArgs e) {
        PictureBox oPictureBox = (PictureBox)sender;
        // add border, do whatever else you want.
    }
    
    0 讨论(0)
提交回复
热议问题