VB.net Errors in converting C# to VB.net

Hi

I get these errors when converting the C# code at the bottom to the VB.net code just above

Error      4      Single-line statement lambdas must include exactly one statement.      C:\Users\murbro\Documents\Visual Studio 2010\Projects\Infra_WinForms\Infra_WinForms\ControlMover.vb      36      68      Infra_WinForms
Error      2      'Public Event MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.      C:\Users\murbro\Documents\Visual Studio 2010\Projects\Infra_WinForms\Infra_WinForms\ControlMover.vb      31      13      Infra_WinForms
Error      3      'Public Event MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.      C:\Users\murbro\Documents\Visual Studio 2010\Projects\Infra_WinForms\Infra_WinForms\ControlMover.vb      36      4      Infra_WinForms
Error      1      'Public Event MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.      C:\Users\murbro\Documents\Visual Studio 2010\Projects\Infra_WinForms\Infra_WinForms\ControlMover.vb      25      13      Infra_WinForms
Error      8      'Public Event LocationChanged(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.      C:\Users\murbro\documents\visual studio 2010\Projects\Infra_WinForms\Infra_WinForms\Form1.vb      22      9      Infra_WinForms
Error      7      'End If' must be preceded by a matching 'If'.      C:\Users\murbro\Documents\Visual Studio 2010\Projects\Infra_WinForms\Infra_WinForms\ControlMover.vb      43      4      Infra_WinForms
Error      5      'e' is not declared. It may be inaccessible due to its protection level.      C:\Users\murbro\Documents\Visual Studio 2010\Projects\Infra_WinForms\Infra_WinForms\ControlMover.vb      38      46      Infra_WinForms
Error      6      'e' is not declared. It may be inaccessible due to its protection level.      C:\Users\murbro\Documents\Visual Studio 2010\Projects\Infra_WinForms\Infra_WinForms\ControlMover.vb      41      45      Infra_WinForms




Imports System.Drawing
Imports System.Windows.Forms


Namespace Helper
    Class ControlMover
        Public Enum Direction
            Any
            Horizontal
            Vertical
        End Enum

        Public Shared Sub Init(control As Control)
            Init(control, Direction.Any)
        End Sub

        Public Shared Sub Init(control As Control, direction As Direction)
            Init(control, control, direction)
        End Sub

        Public Shared Sub Init(control As Control, container As Control, direction__1 As Direction)
            Dim Dragging As Boolean = False
            Dim DragStart As Point = Point.Empty
            control.MouseDown += Sub(sender As Object, e As MouseEventArgs)
                                     Dragging = True
                                     DragStart = New Point(e.X, e.Y)
                                     control.Capture = True

                                 End Sub
            control.MouseUp += Sub(sender As Object, e As MouseEventArgs)
                                   Dragging = False
                                   control.Capture = False

                               End Sub
                  control.MouseMove += Sub(sender As Object, e As MouseEventArgs) If Dragging Then
            If direction__1 <> Direction.Vertical Then
                container.Left = Math.Max(0, e.X + container.Left - DragStart.X)
            End If
            If direction__1 <> Direction.Horizontal Then
                container.Top = Math.Max(0, e.Y + container.Top - DragStart.Y)
            End If
                  End If
        End Sub
    End Class
End Namespace

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

namespace Helper
{
      class ControlMover
      {
            public enum Direction
            {
                  Any,
                  Horizontal,
                  Vertical
            }

            public static void Init(Control control)
            {
                  Init(control, Direction.Any);
            }

            public static void Init(Control control, Direction direction)
            {
                  Init(control, control, direction);
            }

            public static void Init(Control control, Control container, Direction direction)
            {
                  bool Dragging = false;
                  Point DragStart = Point.Empty;
                  control.MouseDown += delegate(object sender, MouseEventArgs e)
                  {
                        Dragging = true;
                        DragStart = new Point(e.X, e.Y);
                        control.Capture = true;
                  };
                  control.MouseUp += delegate(object sender, MouseEventArgs e)
                  {
                        Dragging = false;
                        control.Capture = false;
                  };
                  control.MouseMove += delegate(object sender, MouseEventArgs e)
                  {
                        if (Dragging)
                        {
                              if (direction != Direction.Vertical)
                                    container.Left = Math.Max(0, e.X + container.Left - DragStart.X);
                              if (direction != Direction.Horizontal)
                                    container.Top = Math.Max(0, e.Y + container.Top - DragStart.Y);
                        }
                  };
            }
      }
}
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
 
TheAvengerConnect With a Mentor Commented:
Here is the compilable code. Please check if the logic is what you want.
Imports System.Drawing
Imports System.Windows.Forms


Namespace Helper
	Class ControlMover
		Public Enum Direction
			Any
			Horizontal
			Vertical
		End Enum

		Public Shared Sub Init(control As Control)
			Init(control, Direction.Any)
		End Sub

		Public Shared Sub Init(control As Control, direction As Direction)
			Init(control, control, direction)
		End Sub

		Public Shared Sub Init(control As Control, container As Control, direction__1 As Direction)
			Dim Dragging As Boolean = False
			Dim DragStart As Point = Point.Empty
			AddHandler control.MouseDown, Sub(sender As Object, e As MouseEventArgs)
													   Dragging = True
													   DragStart = New Point(e.X, e.Y)
													   control.Capture = True

												   End Sub
			AddHandler control.MouseUp, Sub(sender As Object, e As MouseEventArgs)
													 Dragging = False
													 control.Capture = False

												 End Sub
			AddHandler control.MouseMove, Sub(sender As Object, e As MouseEventArgs)
													   If Dragging Then
														   If direction__1 <> Direction.Vertical Then
															   container.Left = Math.Max(0, e.X + container.Left - DragStart.X)
														   End If
														   If direction__1 <> Direction.Horizontal Then
															   container.Top = Math.Max(0, e.Y + container.Top - DragStart.Y)
														   End If
													   End If
												   End Sub
		End Sub
	End Class
End Namespace

Open in new window

0
 
Éric MoreauSenior .Net ConsultantCommented:
To declare anonymous delegate in VB.Net, you need to use this syntax:

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AddHandler Button1.Click,
            Sub()
                MessageBox.Show("Click")
            End Sub
    End Sub
0
 
MacroShadowCommented:
Try this (NOT tested):
Imports System.Drawing
Imports System.Windows.Forms


Namespace Helper
    Class ControlMover
        Public Enum Direction
            Any
            Horizontal
            Vertical
        End Enum

        Public Shared Sub Init(control As Control)
            Init(control, Direction.Any)
        End Sub

        Public Shared Sub Init(control As Control, direction As Direction)
            Init(control, control, direction)
        End Sub

        Public Shared Sub Init(control As Control, container As Control, direction__1 As Direction)
            Dim Dragging As Boolean = False
            Dim DragStart As Point = Point.Empty
            AddHandler control.MouseUp, Sub(sender As Object, e As MouseEventArgs)
                                            Dragging = True
                                            DragStart = New Point(e.X, e.Y)
                                            control.Capture = True
                                        End Sub

            AddHandler control.MouseUp, Sub(sender As Object, e As MouseEventArgs)
                                            Dragging = False
                                            control.Capture = False
                                        End Sub

            AddHandler control.MouseUp, Sub(sender As Object, e As MouseEventArgs)
                                            If Dragging Then
                                                If direction__1 <> Direction.Vertical Then
                                                    container.Left = Math.Max(0, e.X + container.Left - DragStart.X)
                                                End If
                                                If direction__1 <> Direction.Horizontal Then
                                                    container.Top = Math.Max(0, e.Y + container.Top - DragStart.Y)
                                                End If
                                            End If
                                        End Sub
        End Sub

    End Class
End Namespace

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
MacroShadowCommented:
Oops looks like TheAvenger beat me to it!
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.