Nasıl yapılır: Windows Forms RichTextBox Denetimi ile Sürükleyip Bırakma İşlemlerini Etkinleştirme
Windows Forms denetimiyle sürükle ve bırak RichTextBox işlemleri, ve olayları iş DragEnter tarafından DragDrop yapılır. Bu nedenle sürükle ve bırak işlemleri, denetimle son derece RichTextBox basittir.
RichTextBox denetiminde sürükleme işlemlerini etkinleştirmek için
Denetimin AllowDrop özelliğini RichTextBox olarak
trueayarlayın.Olayın olay işleyicisinde kod DragEnter yazın. Sürüklenen verilerin kabul edilebilir türde (bu durumda metin) olduğundan emin
ifolmak için deyimini kullanın. DragEventArgs.Effectözelliği, numaralamanın herhangi bir DragDropEffects değerine ayarlanmış olabilir.Private Sub RichTextBox1_DragEnter(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) _ Handles RichTextBox1.DragEnter If (e.Data.GetDataPresent(DataFormats.Text)) Then e.Effect = DragDropEffects.Copy Else e.Effect = DragDropEffects.None End If End Subprivate void richTextBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.Text)) e.Effect = DragDropEffects.Copy; else e.Effect = DragDropEffects.None; }private: void richTextBox1_DragEnter(System::Object ^ sender, System::Windows::Forms::DragEventArgs ^ e) { if (e->Data->GetDataPresent(DataFormats::Text)) e->Effect = DragDropEffects::Copy; else e->Effect = DragDropEffects::None; }(Visual C# ve Visual C++) Olay işleyicisini kaydetmek için formun oluşturucus una aşağıdaki kodu ekleyin.
this.richTextBox1.DragEnter += new System.Windows.Forms.DragEventHandler (this.richTextBox1_DragEnter);this->richTextBox1->DragEnter += gcnew System::Windows::Forms::DragEventHandler (this, &Form1::richTextBox1_DragEnter);Olayı işlemek için kod DragDrop yazın. DataObject.GetDataSürüklenen verileri almak için yöntemini kullanın.
Aşağıdaki örnekte kod, denetimin Text özelliğini RichTextBox sürüklenen verilere eşit olarak ayarlar. Denetimde zaten metin RichTextBox varsa, sürüklenen metin ekleme noktasına eklenir.
Private Sub RichTextBox1_DragDrop(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) _ Handles RichTextBox1.DragDrop Dim i As Int16 Dim s As String ' Get start position to drop the text. i = RichTextBox1.SelectionStart s = RichTextBox1.Text.Substring(i) RichTextBox1.Text = RichTextBox1.Text.Substring(0, i) ' Drop the text on to the RichTextBox. RichTextBox1.Text = RichTextBox1.Text + _ e.Data.GetData(DataFormats.Text).ToString() RichTextBox1.Text = RichTextBox1.Text + s End Subprivate void richTextBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) { int i; String s; // Get start position to drop the text. i = richTextBox1.SelectionStart; s = richTextBox1.Text.Substring(i); richTextBox1.Text = richTextBox1.Text.Substring(0,i); // Drop the text on to the RichTextBox. richTextBox1.Text = richTextBox1.Text + e.Data.GetData(DataFormats.Text).ToString(); richTextBox1.Text = richTextBox1.Text + s; }private: System::Void richTextBox1_DragDrop(System::Object ^ sender, System::Windows::Forms::DragEventArgs ^ e) { int i; String ^s; // Get start position to drop the text. i = richTextBox1->SelectionStart; s = richTextBox1->Text->Substring(i); richTextBox1->Text = richTextBox1->Text->Substring(0,i); // Drop the text on to the RichTextBox. String ^str = String::Concat(richTextBox1->Text, e->Data ->GetData(DataFormats->Text)->ToString()); richTextBox1->Text = String::Concat(str, s); }(Visual C# ve Visual C++) Olay işleyicisini kaydetmek için formun oluşturucus una aşağıdaki kodu ekleyin.
this.richTextBox1.DragDrop += new System.Windows.Forms.DragEventHandler (this.richTextBox1_DragDrop);this->richTextBox1->DragDrop += gcnew System::Windows::Forms::DragEventHandler (this, &Form1::richTextBox1_DragDrop);
Uygulamanıza sürükle ve bırak işlevini test etmek için
Uygulamayı kaydedin ve derleme. Çalışırken WordPad'i çalıştırın.
WordPad, sürükle ve bırak işlemlerine izin Windows tarafından yüklenmiş bir metin düzenleyicisidir. Başlat düğmesine tıklar, Çalıştır'ıseçer,Çalıştır iletişim kutusunun metin kutusuna yazarak ve ardından Tamam'a tıklayarak erişilebilir.
WordPad açıldıktan sonra, içinde bir metin dizesi yazın. Fareyi kullanarak metni seçin ve seçilen metni uygulamanıza göre RichTextBox denetimin üzerine Windows.
Farenin denetime işaret eden (ve sonuç olarak olayı yükselten) fare işaretçisinin değiştiklerine ve seçilen metni denetime RichTextBoxDragEnterRichTextBox bırakabilirsiniz.
Fare düğmesini serbest bıraktığınızda, seçilen metin bırakılır (yani olay DragDrop ortaya çıkar) ve denetimin içine RichTextBox eklenir.