IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Forms Discussion :

Problème évènement DragDrop jamais produit - DragDrop not fired - not raised [Débutant]


Sujet :

Windows Forms

  1. #1
    Membre régulier
    Homme Profil pro
    autre
    Inscrit en
    Janvier 2015
    Messages
    142
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Morbihan (Bretagne)

    Informations professionnelles :
    Activité : autre

    Informations forums :
    Inscription : Janvier 2015
    Messages : 142
    Points : 108
    Points
    108
    Par défaut Problème évènement DragDrop jamais produit - DragDrop not fired - not raised
    Bonjour,

    J'ai passé la journée dessus.
    Je cherche à sélectionner un élément dans une Lisbox pour le déplacer comme sélection (ou texte) dans un combobox (mais avec un textbox ça ne marche pas non plus).

    Voici le code. Une listbox, ListExtraire et un combobox, comboNat.
    L'évènement "comboNat_DragDrop" ne se produit JAMAIS.
    Tout fonctionne sauf ça.
    Je réussi à glisser-déplacer l'item sélectionné uniquement en utilisant comboNat_DragEnter (ce qui n'est pas satisfaisant).

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
     
    private void ListExtraire_MouseDown(object sender, MouseEventArgs e)
            {
                 ListBox lb = ((ListBox)sender);
                Point pt = new Point(e.X, e.Y);
                int index = lb.IndexFromPoint(pt);
     
                // Starts a drag-and-drop operation with that item.
                if (index >= 0)
                {
                    lb.DoDragDrop(lb.Items[index].ToString(), DragDropEffects.All);
                }
     
            }
     
            private void comboNat_DragDrop(object sender, DragEventArgs e)
            {
                comboNat.Text = e.Data.GetData(DataFormats.Text).ToString();
            }
     
            private void comboNat_DragEnter(object sender, DragEventArgs e)
            {
                if (e.Data.GetDataPresent(DataFormats.Text))
                    e.Effect = DragDropEffects.Copy;
                else
                    e.Effect = DragDropEffects.None;
            }
    Je précise : Propriété AllowDrop = true pour tous les contrôles.

    Mes recherches sur le net ne m'ont pas apporté de pistes concluantes.

    Merci pour votre aide.

  2. #2
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut
    bonjour

    voici un exemple code qui permet de dragguer un élément de ListBox vers ComboBox :
    code behind.cs:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
     
    namespace WinDragDrogListboc
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     
            private void Form1_Load(object sender, EventArgs e)
            {
                // ListDragSource
                this.listBoxSrc.Items.AddRange(new object[] {"one", "two", "three", "four", 
                                                                    "five", "six", "seven", "eight","nine", "ten"});
                this.listBoxSrc.MouseDown += new MouseEventHandler(listBoxSrc_MouseDown);
                this.listBoxSrc.MouseMove += new MouseEventHandler(listBoxSrc_MouseMove);
                this.listBoxSrc.MouseUp += new MouseEventHandler(listBoxSrc_MouseUp);
     
     
                this.comboBoxDst.AllowDrop = true;// combo autorise le drop
                this.comboBoxDst.DragDrop += new DragEventHandler(comboBoxDst_DragDrop);
                this.comboBoxDst.DragEnter += new DragEventHandler(comboBoxDst_DragEnter);
     
     
     
            }
     
     
     
     
     
     
            private Point screenOffset; 
            private int indexItem;
            private Rectangle dragBox; 
              void listBoxSrc_MouseDown(object sender, MouseEventArgs e)
            {
                indexItem = listBoxSrc.IndexFromPoint(e.X, e.Y);
     
                if (indexItem != ListBox.NoMatches)
                {
     
                    Size dragSize = SystemInformation.DragSize;
     
                    dragBox = new Rectangle(new Point(e.X - (dragSize.Width / 2),
                                                                   e.Y - (dragSize.Height / 2)), dragSize);
                }
                else
     
                    dragBox = Rectangle.Empty;
     
     
            }
     
            void listBoxSrc_MouseMove(object sender, MouseEventArgs e)
            {
                if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
                {
     
                    // If the mouse moves outside the rectangle, start the drag.
                    if (dragBox != Rectangle.Empty && 
                        !dragBox.Contains(e.X, e.Y)) 
                    {
     
     
                            screenOffset = SystemInformation.WorkingArea.Location;
     
                            DragDropEffects dropEffect = listBoxSrc.DoDragDrop(listBoxSrc.Items[indexItem], DragDropEffects.Move);
     
                            // If the drag operation was a move then remove the item.
                            if (dropEffect == DragDropEffects.Move)
                            {                        
                                listBoxSrc.Items.RemoveAt(indexItem);
     
                                if (indexItem > 0)
                                    listBoxSrc.SelectedIndex = indexItem -1;
     
                                else if (listBoxSrc.Items.Count > 0)
                                    listBoxSrc.SelectedIndex = 0;
                            }
     
                        }
                    }
     
            }
     
            void listBoxSrc_MouseUp(object sender, MouseEventArgs e)
            {
                //reset rectangle
                dragBox = Rectangle.Empty;
            }
     
            void comboBoxDst_DragEnter(object sender, DragEventArgs e)
            {
                if (e.Data.GetDataPresent(typeof(System.String)))
                    e.Effect = DragDropEffects.Move;
            }
     
            void comboBoxDst_DragDrop(object sender, DragEventArgs e)
            {
                if (e.Data.GetDataPresent(typeof(System.String)))
                {
     
                    Object item = (object)e.Data.GetData(typeof(System.String));
     
     
                    if (e.Effect == DragDropEffects.Copy ||
                        e.Effect == DragDropEffects.Move)
                    {
     
                        // Insert or Add the item .
                        int index = this.comboBoxDst.Items.Count - 1;
                        if (index == -1)
                           this.comboBoxDst.Items.Add(item);
                        else
                            this.comboBoxDst.Items.Insert(index+1, item);
     
     
                    }
                }
     
            }
        }
    }
    bon code...

  3. #3
    Membre régulier
    Homme Profil pro
    autre
    Inscrit en
    Janvier 2015
    Messages
    142
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Morbihan (Bretagne)

    Informations professionnelles :
    Activité : autre

    Informations forums :
    Inscription : Janvier 2015
    Messages : 142
    Points : 108
    Points
    108
    Par défaut
    Merci beaucoup. Ça fonctionne.
    Je ne comprends toujours pas pourquoi avec mon code l'évènement DragDrop ne se produisait pas, mais tant pis.
    Merci merci !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [C#] Problème évènement formClosing jamais levée
    Par dharkan dans le forum Débuter
    Réponses: 1
    Dernier message: 27/01/2014, 18h24
  2. Problème de DragDrop
    Par Totanne dans le forum Windows Forms
    Réponses: 3
    Dernier message: 22/04/2008, 08h28
  3. Réponses: 21
    Dernier message: 01/02/2006, 09h17
  4. [servlet]problème de variable jamais nulle
    Par omega dans le forum Servlets/JSP
    Réponses: 2
    Dernier message: 18/03/2004, 09h31
  5. Problème évènements clavier
    Par julie20 dans le forum Langage
    Réponses: 3
    Dernier message: 24/09/2003, 12h39

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo