Scroll in treeview

J

Jeff Johnson

hi all, I have a treeview control and for dragdrop operation I need
automatic scroll up or down as needed. Example: when I drag an item over
the superior limit I want to scroll up and when drag far the bottom I want
to scroll down..

If no one else answers you're question I'll try to find some code I THINK I
have and post it later in the day. (I HOPE I have .NET code for this; I
could be thinking of a VB6 project....)
 
M

Michael Soza

hi all, I have a treeview control and for dragdrop operation I need
automatic scroll up or down as needed. Example: when I drag an item over the
superior limit I want to scroll up and when drag far the bottom I want to
scroll down..
this is my code....I don't know how to do this scroll thing..
public class DragDropTreeview : TreeView
{


private TreeNode srcNodeForDrag;

public DragDropTreeview()
{
this.AllowDrop = true;
}
protected override void OnDragDrop(DragEventArgs e)
{
Point pt = PointToClient(new Point(e.X, e.Y));
TreeNode DestinationNode = this.GetNodeAt(pt);

// Ensure that the list item index is contained in the data.
if (e.Data.GetDataPresent(typeof(TreeNode)))
{
// Perform drag-and-drop, depending upon the effect.
if (e.Effect == DragDropEffects.Copy)
{
TreeNode tmpTN = (TreeNode)srcNodeForDrag.Clone();
DestinationNode.Nodes.Add(tmpTN);
}
else if (e.Effect == DragDropEffects.Move)
{
TreeNode tmpTN = (TreeNode)srcNodeForDrag.Clone();
DestinationNode.Nodes.Add(tmpTN);
srcNodeForDrag.Remove();
}
}
}


protected override void OnDragOver(DragEventArgs e)
{
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)
{
e.Effect = DragDropEffects.Move;
}
}

protected override void OnItemDrag(ItemDragEventArgs e)
{
srcNodeForDrag = (TreeNode)e.Item;
this.DoDragDrop(e.Item, DragDropEffects.All);
}
 
M

Michael Soza

I found the way!!!

Point pt = PointToClient(new Point(e.X, e.Y));
TreeNode DestinationNode = this.GetNodeAt(pt);
DestinationNode.PrevVisibleNode.EnsureVisible();
DestinationNode.NextVisibleNode.EnsureVisible();

in the DragOver Method...

I have own more issue: scroll speed, it just scroll at light speed, and that
is not too user friendly, :), do you know how can I control the speed??
 
J

Jeff Johnson

I found the way!!!

Point pt = PointToClient(new Point(e.X, e.Y));
TreeNode DestinationNode = this.GetNodeAt(pt);
DestinationNode.PrevVisibleNode.EnsureVisible();
DestinationNode.NextVisibleNode.EnsureVisible();

in the DragOver Method...

I have own more issue: scroll speed, it just scroll at light speed, and
that is not too user friendly, :), do you know how can I control the
speed??

When you decide you need to scroll, enable a timer and let the scrolling
code happen in the timer event. Then, when the mouse moves out of scroll
range, disable the timer.
 
J

Jeff Johnson

im trying but it isn't work.....I don't know how pass the dragEventArgs to
the MakeVisible methods

You can't pass them. Store them in a module-level variable (I guess the .NET
term is "field"; regardless, it's basically a global) and then simply
reference that variable in the timer event handler.
 
M

Michael Soza

im trying but it isn't work.....I don't know how pass the dragEventArgs to
the MakeVisible methods

public class DragDropTreeview : TreeView
{
private TreeNode srcNodeForDrag;
Timer temporizador = new Timer();

public DragDropTreeview()
{
this.AllowDrop = true;
temporizador.Interval = 250;
temporizador.Tick += MakeVisible;
}


protected void MakeVisible(object sender, EventArgs a)
{
DragEventArgs e = a as DragEventArgs;
if (e != null) //_>ALWAYS NULL, HELP HERE
{
Point pt = PointToClient(new Point(e.X, e.Y));
TreeNode DestinationNode = this.GetNodeAt(pt);
DestinationNode.PrevVisibleNode.EnsureVisible();
DestinationNode.NextVisibleNode.EnsureVisible();
}
temporizador.Enabled = false;
}

protected override void OnDragOver(DragEventArgs e)
{

temporizador.Enabled = true;

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)
{
e.Effect = DragDropEffects.Move;
}

}


protected override void OnDragDrop(DragEventArgs e)
{
Point pt = PointToClient(new Point(e.X, e.Y));
TreeNode DestinationNode = this.GetNodeAt(pt);

// Ensure that the list item index is contained in the data.
if (e.Data.GetDataPresent(typeof(TreeNode)))
{
// Perform drag-and-drop, depending upon the effect.
if (e.Effect == DragDropEffects.Copy)
{
TreeNode tmpTN = (TreeNode)srcNodeForDrag.Clone();
DestinationNode.Nodes.Add(tmpTN);
}
else if (e.Effect == DragDropEffects.Move)
{
TreeNode tmpTN = (TreeNode)srcNodeForDrag.Clone();
DestinationNode.Nodes.Add(tmpTN);
srcNodeForDrag.Remove();
}
}
}


protected override void OnDragOver(DragEventArgs e)
{

temporizador.Enabled = true;

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)
{
e.Effect = DragDropEffects.Move;
}

}

protected override void OnItemDrag(ItemDragEventArgs e)
{
srcNodeForDrag = (TreeNode)e.Item;
this.DoDragDrop(e.Item, DragDropEffects.All);
}
 
M

Michael Soza

I try other approach, use the timer to slow down the things. When is
disabled I do the EnsureVisible for the sibblings nodes. The tick handler
disable the timer and when I call EnsureVisible I enabled the timer. The
interval is 250 miliseconds, so the scroll only occurs at this speed,
:)....thanks for tellme about the timer!!

The Final Working Code is:
public class DragDropTreeview : TreeView
{
#region Fields & Properties
private TreeNode srcNodeForDrag;
Timer temporizador = new Timer();
#endregion

#region Constructors
public DragDropTreeview()
{
this.AllowDrop = true;
temporizador.Interval = 250;
temporizador.Tick += (x, y) => { temporizador.Enabled =
false; };
}
#endregion

protected override void OnDragOver(DragEventArgs e)
{
if (!temporizador.Enabled)
{
Point pt = PointToClient(new Point(e.X, e.Y));
TreeNode DestinationNode = this.GetNodeAt(pt);
DestinationNode.PrevVisibleNode.EnsureVisible();
DestinationNode.NextVisibleNode.EnsureVisible();
temporizador.Enabled = true;
}


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)
{
e.Effect = DragDropEffects.Move;
}

}
..... the rest remains unchanged.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top