Solved

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

Posted on 2014-02-02
6
560 Views
Last Modified: 2014-02-02
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);
                        }
                  };
            }
      }
}
0
Comment
Question by:murbro
6 Comments
 
LVL 20

Accepted Solution

by:
TheAvenger earned 500 total points
Comment Utility
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
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
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
 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
Oops looks like TheAvenger beat me to it!
0
 

Author Closing Comment

by:murbro
Comment Utility
Thanks very much
0
 

Author Comment

by:murbro
Comment Utility
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now