locked
A Blend behavior in Visual Basic RRS feed

  • Question

  • Can someone write the code below in Visual Basic?
    All the programs i read in the Internet are in C#..

    namespace dragdropinCSharp
    {
      public class DraggableBehavior : Expression.Behavior<FrameworkElement>
      {
       
        bool isDragging;
        Point capturePoint;
       
        public DraggableBehavior()
        {
        }
       
        protected override void OnAttached()
        {
          base.OnAttached();
          this.AssociatedObject.MouseLeftButtonDown += OnMouseDown;
          this.AssociatedObject.MouseLeftButtonUp += OnMouseUp;
          this.AssociatedObject.MouseMove += OnMouseMove;    
        }
        void OnMouseMove(object sender, MouseEventArgs e)
        {
          if (isDragging)
          {
              dragStart = e.GetPosition(dragElement);
          }
        }
        void OnMouseUp(object sender, MouseButtonEventArgs e)
        {
          this.AssociatedObject.ReleaseMouseCapture();
          isDragging = false;
        }
       
       
        void OnMouseDown(object sender, MouseButtonEventArgs e)
        {
          if (!isDragging)
          {
            isDragging = true;
            this.AssociatedObject.CaptureMouse();
          }
        }
       
        protected override void OnDetaching()
        {
          base.OnDetaching();
          this.AssociatedObject.MouseLeftButtonDown -= OnMouseDown;
          this.AssociatedObject.MouseLeftButtonUp -= OnMouseUp;
          this.AssociatedObject.MouseMove -= OnMouseMove;
        }
       
      }
    }
    Wednesday, November 25, 2009 2:40 PM

Answers

  • Yes it worked!!! Thanks to the AddHandler and RemoveHandler.
    Thank you for your help
    • Marked as answer by Angelica_gr Friday, December 4, 2009 10:44 AM
    Friday, December 4, 2009 10:43 AM

All replies

  • Here is a very handy tool for conversions.  It does not work 100% of the time, but almost always and is very useful

    http://www.developerfusion.com/tools/convert/csharp-to-vb/

    The result it produced for me is:

    Namespace dragdropinCSharp
        Public Class DraggableBehavior
            Inherits Expression.Behavior(Of FrameworkElement)
           
            Private isDragging As Boolean
            Private capturePoint As Point
           
            Public Sub New()
            End Sub
           
            Protected Overloads Overrides Sub OnAttached()
                MyBase.OnAttached()
                Me.AssociatedObject.MouseLeftButtonDown += OnMouseDown
                Me.AssociatedObject.MouseLeftButtonUp += OnMouseUp
                Me.AssociatedObject.MouseMove += OnMouseMove
            End Sub
            Private Sub OnMouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
                If isDragging Then
                    dragStart = e.GetPosition(dragElement)
                End If
            End Sub
            Private Sub OnMouseUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
                Me.AssociatedObject.ReleaseMouseCapture()
                isDragging = False
            End Sub
           
           
            Private Sub OnMouseDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
                If Not isDragging Then
                    isDragging = True
                    Me.AssociatedObject.CaptureMouse()
                End If
            End Sub
           
            Protected Overloads Overrides Sub OnDetaching()
                MyBase.OnDetaching()
                Me.AssociatedObject.MouseLeftButtonDown -= OnMouseDown
                Me.AssociatedObject.MouseLeftButtonUp -= OnMouseUp
                Me.AssociatedObject.MouseMove -= OnMouseMove
            End Sub
           
        End Class
    End Namespace

    Wednesday, November 25, 2009 3:01 PM
  • I try it but it gives me errors in AssosiatedObject methods!!!!
    1) MouseLeftButtonDown  is an event and cannot be called directly.Use a RaiseEvent Statement to raise an event
    2) argument not specified for parameter 'e' / 'sender' on sub OnMouseDown, OnMouseUp, OnMouseMove
    Why C# differs so much from VB????I can't find VB code for anything in Expression blend!!!!

    Me.AssociatedObject.MouseLeftButtonDown += OnMouseDown
    Me.AssociatedObject.MouseLeftButtonUp += OnMouseUp
    Me.AssociatedObject.MouseMove += OnMouseMove

    Me.AssociatedObject.MouseLeftButtonDown -= OnMouseDown
    Me.AssociatedObject.MouseLeftButtonUp -= OnMouseUp
    Me.AssociatedObject.MouseMove -= OnMouseMove
    Thursday, November 26, 2009 1:34 PM
  • This c# code is dynamically adding (using the += syntax) event handlers to the events and then removing them (using the -= syntax).

    I normally code in c# but I think the equivalent vb.net syntax is

      AddHandler Me.AssociatedObject.MouseLeftButtonDown, AddressOf OnMouseDown

    and

      RemoveHandler Me.AssociatedObject.MouseLeftButtonDown, AddressOf OnMouseDown

    etc

    You need to delete the incorrect code from the above conversion and replace it with the above

    Me.AssociatedObject.MouseLeftButtonDown += OnMouseDown
    Me.AssociatedObject.MouseLeftButtonDown -= OnMouseDown

    etc

    So the events point to a method to handle in this case the dragging behaviour. Normally the handlers are hooked up in xaml using the usual Click="Name of Handler" syntax.

    Hope this helps.

    regards Andrew
    • Proposed as answer by Andrew 99 Friday, December 4, 2009 1:00 PM
    Thursday, November 26, 2009 2:23 PM
  • As I said, the converter works well most of the time.  I apologize for not checking the resulting code for obvious errors.
    Thursday, November 26, 2009 3:41 PM
  • Yes it worked!!! Thanks to the AddHandler and RemoveHandler.
    Thank you for your help
    • Marked as answer by Angelica_gr Friday, December 4, 2009 10:44 AM
    Friday, December 4, 2009 10:43 AM
  • Hi Anjelica, glad it helped - I would be grateful if you could mark my reply as the answer if your in agreement I think you inadvertently marked your own last answer instead.
    Regards
    Andrew
    Friday, December 4, 2009 12:59 PM