drag & drop

08/07/2004 - 21:02 por Sinuhé Leines | Informe spam
Necesito arrastrar los elementos de un listbox a otro listbox.!!

1.- La propiedad allowDrop de ambos listbox está en TRUE
2.- He colocado líneas de código en todos los métodos referentes (DragDrop,
DragEnter, DragLeave, DragOver) en ambas listas para ver en qué momento se
ejecutan pero nunca entra a ningún método.

HELP!

WindowsForms, C#, VS2003.

Preguntas similare

Leer las respuestas

#1 Octavio Hernandez
09/07/2004 - 00:53 | Informe spam
Sinuhé,

Mira este ejemplo de lo que quieres hacer:

http://www.codeproject.com/cs/combo...ndDrop.asp

Slds - Octavio

"Sinuhé Leines" escribió en el mensaje
news:
Necesito arrastrar los elementos de un listbox a otro listbox.!!

1.- La propiedad allowDrop de ambos listbox está en TRUE
2.- He colocado líneas de código en todos los métodos referentes


(DragDrop,
DragEnter, DragLeave, DragOver) en ambas listas para ver en qué momento se
ejecutan pero nunca entra a ningún método.

HELP!

WindowsForms, C#, VS2003.


Respuesta Responder a este mensaje
#2 Alejandro Perez
09/07/2004 - 20:32 | Informe spam
Saludos Sinuhé!. El problema es que no estas trabajando
con los eventos del mouse. Aqui te dejo un buen ejemplo
de arrastre de items de un Listbox a otro:

Cuidate!
Alejandro Perez
MCSD/MCDBA/MCT
Caracas - Venezuela



using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsApplication1
{
public class drag : System.Windows.Forms.Form
{
private Rectangle recIcono;
private Point pntAreaDeTrabajo;

private int intIndexItemDrag;
private int intIndexItemDrop;

private System.Windows.Forms.ListBox
ListFuente;
private System.Windows.Forms.ListBox
ListDestino;

public drag()
{
this.ListFuente = new
System.Windows.Forms.ListBox();
this.ListDestino = new
System.Windows.Forms.ListBox();

this.SuspendLayout();

// ListFuente
this.ListFuente.Items.AddRange
(new object[] {"Item 0", "Item 1", "Item 2", "Item 3",


"Item 4", "Item 5", "Item 6"});
this.ListFuente.Location = new
System.Drawing.Point(10, 17);
this.ListFuente.Size = new
System.Drawing.Size(120, 225);
this.ListFuente.MouseDown += new
System.Windows.Forms.MouseEventHandler
(this.ListFuente_MouseDown);
this.ListFuente.QueryContinueDrag
+= new System.Windows.Forms.QueryContinueDragEventHandler
(this.ListFuente_QueryContinueDrag);
this.ListFuente.MouseUp += new
System.Windows.Forms.MouseEventHandler
(this.ListFuente_MouseUp);
this.ListFuente.MouseMove += new
System.Windows.Forms.MouseEventHandler
(this.ListFuente_MouseMove);

// ListDestino
this.ListDestino.AllowDrop = true;
this.ListDestino.Location = new
System.Drawing.Point(154, 17);
this.ListDestino.Size = new
System.Drawing.Size(120, 225);
this.ListDestino.DragOver += new
System.Windows.Forms.DragEventHandler
(this.ListDestino_DragOver);
this.ListDestino.DragDrop += new
System.Windows.Forms.DragEventHandler
(this.ListDestino_DragDrop);

// Form1
this.AutoScaleBaseSize = new
System.Drawing.Size(5, 13);
this.ClientSize = new
System.Drawing.Size(292, 270);
this.Controls.AddRange(new
System.Windows.Forms.Control[] {this.ListFuente,


this.ListDestino});
this.Text = "Drag and Drop
Example";

this.ResumeLayout(false);

}

private void ListFuente_MouseDown(object
sender, System.Windows.Forms.MouseEventArgs e)
{
// Index del item que esta abajo
de donde se presiono el mouse
intIndexItemDrag =
ListFuente.IndexFromPoint(e.X, e.Y);

if (intIndexItemDrag !=
ListBox.NoMatches)
{

// Recuerda el punto
donde se presiono el mouse. El DragSize indica
// el tamaño en el que
mouse se puede mover antes de que un evento drag
inicie.
Size dragSize =
SystemInformation.DragSize;

// Crea un rectangulo
utilizando el DragSize, con la posicion del mouse
// en el centro del
rectangulo.
recIcono = new Rectangle
(new Point(e.X - (dragSize.Width /2),
e.Y -
(dragSize.Height /2)), dragSize);
}
else
// Quita el rectangulo si
el mouse no esta sobre un item en el ListBox.
recIcono =
Rectangle.Empty;

}

private void ListFuente_MouseUp(object
sender, System.Windows.Forms.MouseEventArgs e)
{
// Quita el rectangulo cuando se
levanta el boton del mouse.
recIcono = Rectangle.Empty;
}

private void ListFuente_MouseMove(object
sender, System.Windows.Forms.MouseEventArgs e)
{
if ((e.Button &
MouseButtons.Left) == MouseButtons.Left)
{
// Si el mouse se mueve
fuera del rectangulo, se comienza el arrastre.
if (recIcono !=
Rectangle.Empty &&
!recIcono.Contains
(e.X, e.Y))
{
// El
pntAreaDeTrabajo es usado para registrar cualquier area
inutil
// que puede
estar alrededor de la pantalla cuando se va a determinar
// cuando
cancelar la operacion drag-drop.
pntAreaDeTrabajo
= SystemInformation.WorkingArea.Location;

// Se procede al
drag-drop pasandole el item list.
DragDropEffects
dropEffect = ListFuente.DoDragDrop(ListFuente.Items
[intIndexItemDrag], DragDropEffects.All |
DragDropEffects.Link);

// Si la
operacion de arrastre fue "move", se remueve el item.
if (dropEffect ==
DragDropEffects.Move)

{

ListFuente.Items.RemoveAt(intIndexItemDrag);

// Se
selecciona el item previo en la lista, mientras que tenga
items.
if
(intIndexItemDrag > 0)

ListFuente.SelectedIndex = intIndexItemDrag -1;

else if
(ListFuente.Items.Count > 0)

// Selecciona el primer item.

ListFuente.SelectedIndex =0;
}
}
}
}

private void ListFuente_QueryContinueDrag
(object sender,
System.Windows.Forms.QueryContinueDragEventArgs e)
{
// Cancela el arrastre si el
mouse se mueve fuera del formulario
ListBox lb = sender as ListBox;

if (lb != null)
{

Form f = lb.FindForm();

// Cancela el arrastre si
el mouse se mueve fuera del form.
if
(((Control.MousePosition.X - pntAreaDeTrabajo.X) <
f.DesktopBounds.Left) ||

((Control.MousePosition.X - pntAreaDeTrabajo.X) >
f.DesktopBounds.Right) ||

((Control.MousePosition.Y - pntAreaDeTrabajo.Y) <
f.DesktopBounds.Top) ||

((Control.MousePosition.Y - pntAreaDeTrabajo.Y) >
f.DesktopBounds.Bottom))
{
e.Action =
DragAction.Cancel;
}
}
}


private void ListDestino_DragOver(object
sender, System.Windows.Forms.DragEventArgs e)
{

// Determina si la data existe.
Si no, entonces el efecto drop
// indicara que el drop no puede
ocurrir.
if (!e.Data.GetDataPresent(typeof
(System.String)))
{

e.Effect =
DragDropEffects.None;
return;
}

// Se establece el efecto basado
en las teclas presionadas
// para el momento.
if ((e.KeyState & (8+32)) ==
(8+32) &&
(e.AllowedEffect &
DragDropEffects.Link) == DragDropEffects.Link)
{
// KeyState 8 + 32 = CTL
+ ALT

e.Effect =
DragDropEffects.Link;

}
else if ((e.KeyState & 32) == 32
&&
(e.AllowedEffect &
DragDropEffects.Link) == DragDropEffects.Link)
{

// ALT KeyState para link.
e.Effect =
DragDropEffects.Link;

}
else if ((e.KeyState & 4) == 4 &&
(e.AllowedEffect &
DragDropEffects.Move) == DragDropEffects.Move)
{

// SHIFT KeyState para
mover.
e.Effect =
DragDropEffects.Move;

}
else if ((e.KeyState & 8) == 8 &&
(e.AllowedEffect &
DragDropEffects.Copy) == DragDropEffects.Copy)
{

// CTL KeyState para
copiar.
e.Effect =
DragDropEffects.Copy;

}
else if ((e.AllowedEffect &
DragDropEffects.Move) == DragDropEffects.Move)
{

// Por defecto, la accion
drop es mover.
e.Effect =
DragDropEffects.Move;

}
else
e.Effect =
DragDropEffects.None;

// Se obtiene el index del item
que esta abajo de donde se
// va a soltar el nuevo item.

// La ubicacion del mouse es
relativa a la pantalla, por lo que debe
// ser convertida a coordenadas
clientes.

intIndexItemDrop =
ListDestino.IndexFromPoint
(ListDestino.PointToClient(new Point(e.X, e.Y)));

}
private void ListDestino_DragDrop(object
sender, System.Windows.Forms.DragEventArgs e)
{
// Asegurarse que el item list
esta contenido en la data.
if (e.Data.GetDataPresent(typeof
(System.String)))
{

Object item = (object)
e.Data.GetData(typeof(System.String));

// Realiza el drag-drop
dependiendo del efecto.
if (e.Effect ==
DragDropEffects.Copy ||
e.Effect ==
DragDropEffects.Move)
{

// Inserta el
item.
if
(intIndexItemDrop != ListBox.NoMatches)

ListDestino.Items.Insert(intIndexItemDrop, item);
else

ListDestino.Items.Add(item);

}
}
}


}
}




Necesito arrastrar los elementos de un listbox a otro


listbox.!!

1.- La propiedad allowDrop de ambos listbox está en TRUE
2.- He colocado líneas de código en todos los métodos


referentes (DragDrop,
DragEnter, DragLeave, DragOver) en ambas listas para ver


en qué momento se
ejecutan pero nunca entra a ningún método.

HELP!

WindowsForms, C#, VS2003.


.

email Siga el debate Respuesta Responder a este mensaje
Ads by Google
Help Hacer una preguntaRespuesta Tengo una respuesta
Search Busqueda sugerida