Control.AllowDrop Özellik

Tanım

Denetimin kullanıcının üzerine sürüklediği verileri kabul edip etmediğini belirten bir değer alır veya ayarlar.

C#
public virtual bool AllowDrop { get; set; }

Özellik Değeri

true denetimde sürükle ve bırak işlemlerine izin veriliyorsa; aksi takdirde , false. Varsayılan değer: false.

Örnekler

Aşağıdaki kod örneği, kullanıcının forma bir resim veya görüntü dosyası sürüklemesini ve bırakıldığında görüntülenmesini sağlar. OnPaint Form her boyanırken görüntüyü yeniden boyamak için yöntemi geçersiz kılındı; aksi takdirde görüntü yalnızca bir sonraki yeniden boyamaya kadar kalır. Olay DragEnter işleme yöntemi, forma sürüklenen verilerin türünü belirler ve uygun geri bildirimi sağlar. Olay DragDrop işleme yöntemi, verilerden bir Image oluşturulabiliyorsa görüntüyü formda görüntüler. DragEventArgs.X ve DragEventArgs.Y değerleri ekran koordinatları olduğundan, örnek bunları istemci koordinatlarına dönüştürmek için yöntemini kullanırPointToClient.

C#
private Image picture;
private Point pictureLocation;

public Form1()
{
   // Enable drag-and-drop operations and 
   // add handlers for DragEnter and DragDrop.
   this.AllowDrop = true;
   this.DragDrop += new DragEventHandler(this.Form1_DragDrop);
   this.DragEnter += new DragEventHandler(this.Form1_DragEnter);
}

protected override void OnPaint(PaintEventArgs e)
{
   // If there is an image and it has a location, 
   // paint it when the Form is repainted.
   base.OnPaint(e);
   if(this.picture != null && this.pictureLocation != Point.Empty)
   {
      e.Graphics.DrawImage(this.picture, this.pictureLocation);
   }
}

private void Form1_DragDrop(object sender, DragEventArgs e)
{
   // Handle FileDrop data.
   if(e.Data.GetDataPresent(DataFormats.FileDrop) )
   {
      // Assign the file names to a string array, in 
      // case the user has selected multiple files.
      string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
      try
      {
         // Assign the first image to the picture variable.
         this.picture = Image.FromFile(files[0]);
         // Set the picture location equal to the drop point.
         this.pictureLocation = this.PointToClient(new Point(e.X, e.Y) );
      }
      catch(Exception ex)
      {
         MessageBox.Show(ex.Message);
         return;
      }
   }

   // Handle Bitmap data.
   if(e.Data.GetDataPresent(DataFormats.Bitmap) )
   {
      try
      {
         // Create an Image and assign it to the picture variable.
         this.picture = (Image)e.Data.GetData(DataFormats.Bitmap);
         // Set the picture location equal to the drop point.
         this.pictureLocation = this.PointToClient(new Point(e.X, e.Y) );
      }
      catch(Exception ex)
      {
         MessageBox.Show(ex.Message);
         return;
      }
   }
   // Force the form to be redrawn with the image.
   this.Invalidate();
}

private void Form1_DragEnter(object sender, DragEventArgs e)
{
   // If the data is a file or a bitmap, display the copy cursor.
   if (e.Data.GetDataPresent(DataFormats.Bitmap) || 
      e.Data.GetDataPresent(DataFormats.FileDrop) ) 
   {
      e.Effect = DragDropEffects.Copy;
   }
   else
   {
      e.Effect = DragDropEffects.None;
   }
}

Devralanlara Notlar

Türetilmiş bir sınıfta özelliğini AllowDrop geçersiz kıldığınızda, temel uygulamayı genişletmek için temel sınıfın AllowDrop özelliğini kullanın. Aksi takdirde, tüm uygulamayı sağlamanız gerekir. Özelliğin get hem hem set de erişimcilerini AllowDrop geçersiz kılmanız gerekmez; gerekirse yalnızca birini geçersiz kılabilirsiniz.

Şunlara uygulanır

Ürün Sürümler
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Ayrıca bkz.