Solved

VB.net Errors after converting from C#

Posted on 2014-09-09
7
290 Views
Last Modified: 2014-09-09
Hi
I am trying to convert the code at the following link to VB.net but I get the error 'ERROR Expression does not produce a value
This is shown at the three lines marked with ERROR
http://www.codeproject.com/Tips/709121/Move-and-Resize-Controls-on-a-Form-at-Runtime-With

The code I get follows. The
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Globalization
Imports System.Linq
Imports System.Windows.Forms

Public Class ControlMoverOrResizer

    Private Shared _moving As Boolean
    Private Shared _cursorStartPoint As Point
    Private Shared _moveIsInterNal As Boolean
    Private Shared _resizing As Boolean
    Private Shared _currentControlStartSize As Size
    Friend Shared Property MouseIsInLeftEdge() As Boolean
        Get
            Return m_MouseIsInLeftEdge
        End Get
        Set(value As Boolean)
            m_MouseIsInLeftEdge = value
        End Set
    End Property
    Private Shared m_MouseIsInLeftEdge As Boolean
    Friend Shared Property MouseIsInRightEdge() As Boolean
        Get
            Return m_MouseIsInRightEdge
        End Get
        Set(value As Boolean)
            m_MouseIsInRightEdge = value
        End Set
    End Property
    Private Shared m_MouseIsInRightEdge As Boolean
    Friend Shared Property MouseIsInTopEdge() As Boolean
        Get
            Return m_MouseIsInTopEdge
        End Get
        Set(value As Boolean)
            m_MouseIsInTopEdge = value
        End Set
    End Property
    Private Shared m_MouseIsInTopEdge As Boolean
    Friend Shared Property MouseIsInBottomEdge() As Boolean
        Get
            Return m_MouseIsInBottomEdge
        End Get
        Set(value As Boolean)
            m_MouseIsInBottomEdge = value
        End Set
    End Property
    Private Shared m_MouseIsInBottomEdge As Boolean

    Friend Enum MoveOrResize
        Move
        Resize
        MoveAndResize
    End Enum

    Friend Shared Property WorkType() As MoveOrResize
        Get
            Return m_WorkType
        End Get
        Set(value As MoveOrResize)
            m_WorkType = value
        End Set
    End Property
    Private Shared m_WorkType As MoveOrResize

    Friend Shared Sub Init(control As Control)
        Init(control, control)
    End Sub

    Friend Shared Sub Init(control As Control, container As Control)
        _moving = False
        _resizing = False
        _moveIsInterNal = False
        _cursorStartPoint = Point.Empty
        MouseIsInLeftEdge = False
        MouseIsInLeftEdge = False
        MouseIsInRightEdge = False
        MouseIsInTopEdge = False
        MouseIsInBottomEdge = False
        WorkType = MoveOrResize.MoveAndResize
        AddHandler control.MouseDown, Function(sender, e) StartMovingOrResizing(control, e) 'ERROR Expression does not produce a value
        AddHandler control.MouseUp, Function(sender, e) StopDragOrResizing(control) 'ERROR Expression does not produce a value
        AddHandler control.MouseMove, Function(sender, e) MoveControl(container, e) 'ERROR Expression does not produce a value
    End Sub

    Private Shared Sub UpdateMouseEdgeProperties(control As Control, mouseLocationInControl As Point)
        If WorkType = MoveOrResize.Move Then
            Return
        End If
        MouseIsInLeftEdge = Math.Abs(mouseLocationInControl.X) <= 2
        MouseIsInRightEdge = Math.Abs(mouseLocationInControl.X - control.Width) <= 2
        MouseIsInTopEdge = Math.Abs(mouseLocationInControl.Y) <= 2
        MouseIsInBottomEdge = Math.Abs(mouseLocationInControl.Y - control.Height) <= 2
    End Sub

    Private Shared Sub UpdateMouseCursor(control As Control)
        If WorkType = MoveOrResize.Move Then
            Return
        End If
        If MouseIsInLeftEdge Then
            If MouseIsInTopEdge Then
                control.Cursor = Cursors.SizeNWSE
            ElseIf MouseIsInBottomEdge Then
                control.Cursor = Cursors.SizeNESW
            Else
                control.Cursor = Cursors.SizeWE
            End If
        ElseIf MouseIsInRightEdge Then
            If MouseIsInTopEdge Then
                control.Cursor = Cursors.SizeNESW
            ElseIf MouseIsInBottomEdge Then
                control.Cursor = Cursors.SizeNWSE
            Else
                control.Cursor = Cursors.SizeWE
            End If
        ElseIf MouseIsInTopEdge OrElse MouseIsInBottomEdge Then
            control.Cursor = Cursors.SizeNS
        Else
            control.Cursor = Cursors.[Default]
        End If
    End Sub

    Private Shared Sub StartMovingOrResizing(control As Control, e As MouseEventArgs)
        If _moving OrElse _resizing Then
            Return
        End If
        If WorkType <> MoveOrResize.Move AndAlso (MouseIsInRightEdge OrElse MouseIsInLeftEdge OrElse MouseIsInTopEdge OrElse MouseIsInBottomEdge) Then
            _resizing = True
            _currentControlStartSize = control.Size
        ElseIf WorkType <> MoveOrResize.Resize Then
            _moving = True
            control.Cursor = Cursors.Hand
        End If
        _cursorStartPoint = New Point(e.X, e.Y)
        control.Capture = True
    End Sub

    Private Shared Sub MoveControl(control As Control, e As MouseEventArgs)
        If Not _resizing AndAlso Not _moving Then
            UpdateMouseEdgeProperties(control, New Point(e.X, e.Y))
            UpdateMouseCursor(control)
        End If
        If _resizing Then
            If MouseIsInLeftEdge Then
                If MouseIsInTopEdge Then
                    control.Width -= (e.X - _cursorStartPoint.X)
                    control.Left += (e.X - _cursorStartPoint.X)
                    control.Height -= (e.Y - _cursorStartPoint.Y)
                    control.Top += (e.Y - _cursorStartPoint.Y)
                ElseIf MouseIsInBottomEdge Then
                    control.Width -= (e.X - _cursorStartPoint.X)
                    control.Left += (e.X - _cursorStartPoint.X)
                    control.Height = (e.Y - _cursorStartPoint.Y) + _currentControlStartSize.Height
                Else
                    control.Width -= (e.X - _cursorStartPoint.X)
                    control.Left += (e.X - _cursorStartPoint.X)
                End If
            ElseIf MouseIsInRightEdge Then
                If MouseIsInTopEdge Then
                    control.Width = (e.X - _cursorStartPoint.X) + _currentControlStartSize.Width
                    control.Height -= (e.Y - _cursorStartPoint.Y)

                    control.Top += (e.Y - _cursorStartPoint.Y)
                ElseIf MouseIsInBottomEdge Then
                    control.Width = (e.X - _cursorStartPoint.X) + _currentControlStartSize.Width
                    control.Height = (e.Y - _cursorStartPoint.Y) + _currentControlStartSize.Height
                Else
                    control.Width = (e.X - _cursorStartPoint.X) + _currentControlStartSize.Width
                End If
            ElseIf MouseIsInTopEdge Then
                control.Height -= (e.Y - _cursorStartPoint.Y)
                control.Top += (e.Y - _cursorStartPoint.Y)
            ElseIf MouseIsInBottomEdge Then
                control.Height = (e.Y - _cursorStartPoint.Y) + _currentControlStartSize.Height
            Else
                StopDragOrResizing(control)
            End If
        ElseIf _moving Then
            _moveIsInterNal = Not _moveIsInterNal
            If Not _moveIsInterNal Then
                Dim x As Integer = (e.X - _cursorStartPoint.X) + control.Left
                Dim y As Integer = (e.Y - _cursorStartPoint.Y) + control.Top
                control.Location = New Point(x, y)
            End If
        End If
    End Sub

    Private Shared Sub StopDragOrResizing(control As Control)
        _resizing = False
        _moving = False
        control.Capture = False
        UpdateMouseCursor(control)
    End Sub

#Region "Save And Load"

    Private Shared Function GetAllChildControls(control As Control, list As List(Of Control)) As List(Of Control)
        Dim controls As List(Of Control) = control.Controls.Cast(Of Control)().ToList()
        list.AddRange(controls)
        Return controls.SelectMany(Function(ctrl) GetAllChildControls(ctrl, list)).ToList()
    End Function

    Friend Shared Function GetSizeAndPositionOfControlsToString(container As Control) As String
        Dim controls As New List(Of Control)()
        GetAllChildControls(container, controls)
        Dim cultureInfo As New CultureInfo("en")
        Dim info As String = String.Empty
        For Each control As Control In controls
            info += control.Name & ":" & control.Left.ToString(cultureInfo) & "," & control.Top.ToString(cultureInfo) & "," & control.Width.ToString(cultureInfo) & "," & control.Height.ToString(cultureInfo) & "*"
        Next
        Return info
    End Function
    Friend Shared Sub SetSizeAndPositionOfControlsFromString(container As Control, controlsInfoStr As String)
        Dim controls As New List(Of Control)()
        GetAllChildControls(container, controls)
			Dim controlsInfo As String() = controlsInfoStr.Split(New () {"*"}, StringSplitOptions.RemoveEmptyEntries)
        Dim controlsInfoDictionary As New Dictionary(Of String, String)()
        For Each controlInfo As String In controlsInfo
				Dim info As String() = controlInfo.Split(New () {":"}, StringSplitOptions.RemoveEmptyEntries)
            controlsInfoDictionary.Add(info(0), info(1))
        Next
        For Each control As Control In controls
            Dim propertiesStr As String
            controlsInfoDictionary.TryGetValue(control.Name, propertiesStr)
				Dim properties As String() = propertiesStr.Split(New () {","}, StringSplitOptions.RemoveEmptyEntries)
            If properties.Length = 4 Then
                control.Left = Integer.Parse(properties(0))
                control.Top = Integer.Parse(properties(1))
                control.Width = Integer.Parse(properties(2))
                control.Height = Integer.Parse(properties(3))
            End If
        Next
    End Sub

#End Region

Open in new window

0
Comment
Question by:murbro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40311777
I think it should be simply like this:
        AddHandler control.MouseDown, StartMovingOrResizing(control, e) 
        AddHandler control.MouseUp, StopDragOrResizing(control) 
        AddHandler control.MouseMove, MoveControl(container, e) 

Open in new window

0
 

Author Comment

by:murbro
ID: 40311791
Hi. Thanks. I get 'e is not declared'
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 40311843
See example of anonymous function with addhandler at the following page

http://10rem.net/blog/2010/04/16/tip-anonymous-event-handlers-in-vbnet


Public Sub LoadPolicyDetail()
    Dim client As New Services.Service1Client()
 
    AddHandler client.GetPolicyDetailCompleted,
        Sub(s As Object, e As GetPolicyDetailCompletedEventArgs)
            If e.Result <> Nothing Then
                MessageBox.Show("Awesome!")
            End If
        End Sub
 
    client.GetPolicyDetailAsync()
End Sub

Open in new window

0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40311848
can you try this:

AddHandler control.MouseDown, AddressOf  StartMovingOrResizing
AddHandler control.MouseUp, AddressOf  StopDragOrResizing
AddHandler control.MouseMove, AddressOf  MoveControl

Open in new window

0
 

Author Comment

by:murbro
ID: 40311887
Thanks Eric. The top and bottom lines work but I get the following error for the middle line
Error      4      Method 'Private Shared Sub StopDragOrResizing(control As System.Windows.Forms.Control)' does not have a signature compatible with delegate 'Delegate Sub MouseEventHandler(sender As Object, e As System.Windows.Forms.MouseEventArgs)'.
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 40311900
change:
Private Shared Sub StopDragOrResizing(control As Control)

Open in new window


for:
Private Shared Sub StopDragOrResizing(control As Control, e As System.Windows.Forms.MouseEventArgs)

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 40311977
Thanks very much Eric
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Change to file doesn't show up 16 74
VB.net -  GroupBox / Panel DataBinding and behavior 3 23
VB.net Code to make a TaskPane float in Excel Add-in 2 25
C# XML Get Values 4 33
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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