TextBoxDragDrop.cs
Description du code
TextBoxDragDrop.cs est un fichier du projet BrolDev.Ce fichier est situé dans /var/www/bin/sniplets/bibliobrol/broldev/src/.
Projet BrolDev : Librairie de composants réutilisables pour les applications BrolDev en CSharp.
Code source ou contenu du fichier
Code c# (TextBoxDragDrop.cs) (254 lignes)
using System; using System.ComponentModel; using System.Drawing; using System.IO; using System.Text; using System.Windows.Forms; using be.gaudry.model; using be.gaudry.model.config; using be.gaudry.observer; namespace be.gaudry.view.controls { public partial class TextBoxDragDrop : TextBox { private int lastSelStart = 0; private int lastSelLength = 0; private bool candidateToDragDrop = false; public TextBoxDragDrop() { InitializeComponent(); init(); } public TextBoxDragDrop(IContainer container) { container.Add(this); InitializeComponent(); init(); } private void init() { this.AllowDrop = true; "drag and drop", "TextBoxDragDrop", "http://msdn2.microsoft.com/fr-fr/library/system.windows.forms.drageventargs(VS.80).aspx", "Gestion des touches pour le drag and drop" ) ); } protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); if (lastSelLength == 0) { lastSelLength = SelectionLength; lastSelStart = SelectionStart; } else { lastSelLength = 0; lastSelStart = 0; candidateToDragDrop = false; } } protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); //todo test if cursor is over the selected text if (lastSelLength > 0) { candidateToDragDrop = true; } } protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); //todo : test if cursor is over the selected text if (candidateToDragDrop) { DragDropEffects dr; if (lastSelLength == Text.Length)//we may use move of a entire string { if (!TextHelper.isKeyDown(TextHelper.KEY.CONTROL)) { dr = DoDragDrop(Text, DragDropEffects.Move); if (dr == DragDropEffects.Move) Text = ""; } else { DoDragDrop(Text, DragDropEffects.Copy); } } else { //get selected text try { string str = Text.Substring(lastSelStart, lastSelLength); //we may not use DragDropEffects.All or move because we drag only a part of string dr = DoDragDrop(str, DragDropEffects.Copy); //remove selected text to simulate move if (dr == DragDropEffects.Copy && !TextHelper.isKeyDown(TextHelper.KEY.CONTROL)) { Text = Text.Remove(lastSelStart, lastSelLength); } } catch (Exception ex) { Notification.VERBOSE.lowError, string.Format("{0} : {1}",this.GetType().ToString(),this.Name), "Erreur de drag and drop", ex, this )); } } //reinit lastSelLength = 0; lastSelStart = 0; candidateToDragDrop = false; } } /* protected override void OnDragEnter(DragEventArgs e) { base.OnDragEnter(e); e.Effect = DragDropEffects.All; } protected override void OnDragDrop(DragEventArgs e) { base.OnDragDrop(e); if (e.Effect != DragDropEffects.None) //e.Data.GetDataPresent(typeof(string))) { Text = Text.Insert( SelectionStart, //(string)e.Data.GetData(typeof(string)) getDropObjectAsText(e) ); } this.SelectionLength = 0; this.SelectionStart = Text.Length; this.Focus(); } */ private void onTextBox_DragEnter(object sender, DragEventArgs e) { //if (e.Data.GetDataPresent(typeof(string))) //{ //e.Effect = DragDropEffects.Copy; //} //e.Effect = DragDropEffects.All; if ((e.KeyState & 4) == 4 && (e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) { // SHIFT KeyState for move. e.Effect = DragDropEffects.Move; } else if ((e.KeyState & 8) == 8 && (e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) { // CTL KeyState for copy. e.Effect = DragDropEffects.Copy; } else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) { // By default, the drop action should be move, if allowed. e.Effect = DragDropEffects.Move; } else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) { e.Effect = DragDropEffects.Copy; } else e.Effect = DragDropEffects.None; } private void onTextBox_DragDrop(object sender, DragEventArgs e) { //allow drop for other types (file, etc) if (e.Effect!= DragDropEffects.None) //e.Data.GetDataPresent(typeof(string))) { Text = Text.Insert( SelectionStart, //(string)e.Data.GetData(typeof(string)) getDropObjectAsText(e) ); this.SelectionLength = 0; this.SelectionStart = Text.Length; this.Focus(); } } protected override void OnDragOver(DragEventArgs e) { base.OnDragOver(e); //get insertion point SelectionStart = TextHelper.CharFromPos(this, pt); } /*private void setEffect(DragEventArgs e) { if((e.KeyState & 4) == 4 && (e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) { // SHIFT KeyState for move. e.Effect = DragDropEffects.Move; } else if ((e.KeyState & 8) == 8 && (e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) { // CTL KeyState for copy. e.Effect = DragDropEffects.Copy; } else e.Effect = DragDropEffects.None; }*/ protected virtual string getDropObjectAsText(DragEventArgs e) { { } if (e.Data.GetDataPresent(DataFormats.FileDrop)) { string[] DropFiles = (string[])e.Data.GetData(DataFormats.FileDrop); foreach (string s in DropFiles) { if (str.Length > 0) str.Append(", "); str.Append(Path.GetFileNameWithoutExtension(s)); } return str.ToString(); } //MessageBox.Show("Impossible de déposer l'élément ici"); e.Effect = DragDropEffects.None; return ""; } } }
Structure et Fichiers du projet
Afficher/masquer...Icône | Nom | Taille | Modification |
Pas de sous-répertoires. | |||
Icône | Nom | Taille | Modification |
| _ | Répertoire parent | 0 octets | 1732452652 24/11/2024 13:50:52 |
Utilisation de l'explorateur de code
- Navigation :
- Un clic sur une icône de répertoire ouvre ce répertoire pour en afficher les fichiers.
- Lorsque le répertoire en cours ne contient pas de sous-répertoires il est possible de remonter vers le répertoire parent.
- La structure de répertoires en treetable (tableau en forme d'arborescence) n'est plus possibledans cette version.
- Un clic sur une icône de fichier ouvre ce fichier pour en afficher le code avec la coloration syntaxique adaptée en fonction du langage principal utilisé dans le fichier.
- Affichage :
- Il est possible de trier les répertoires ou les fichiers selon certains critères (nom, taille, date).
- Actions :
- Les actions possible sur les fichiers dépendent de vos droits d'utilisateur sur le site. Veuillez activer le mode utilisateur pour activer les actions.
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 16/10/2009, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/cs-broldev-source-rf-view/controls/TextBoxDragDrop.cs.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.