How do i make a picturebox selectable?

前端 未结 3 573
梦如初夏
梦如初夏 2020-11-30 10:20

I am making a very basic map editor. I\'m halfway through it and one problem i hit is how to delete an object.

I would like to press delete but there appears to be n

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 10:57

    You'll want the PictureBox to participate in the tabbing order and show that it has the focus. That takes a bit of minor surgery. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. Implement the KeyDown event.

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    class SelectablePictureBox : PictureBox {
      public SelectablePictureBox() {
        this.SetStyle(ControlStyles.Selectable, true);
        this.TabStop = true;
      }
      protected override void OnMouseDown(MouseEventArgs e) {
        this.Focus();
        base.OnMouseDown(e);
      }
      protected override void OnEnter(EventArgs e) {
        this.Invalidate();
        base.OnEnter(e);
      }
      protected override void OnLeave(EventArgs e) {
        this.Invalidate();
        base.OnLeave(e);
      }
      protected override void OnPaint(PaintEventArgs pe) {
        base.OnPaint(pe);
        if (this.Focused) {
          var rc = this.ClientRectangle;
          rc.Inflate(-2, -2);
          ControlPaint.DrawFocusRectangle(pe.Graphics, rc);
        }
      }
    }
    

提交回复
热议问题