Solved

how to turn vb6 code into VB.net Code?

Posted on 2011-09-16
11
890 Views
Last Modified: 2012-05-12
Im converting a vb6 app to VB.net, but im having problem with drawing my line in VB.net because CurrentX, CurrentY and ScaleLeft and ScaleWidth are no longer used.

heres my vb6 code

CurrentX = ScaleLeft
CurrentY = .RegVal(CDbl(ScaleLeft))

    For i = ScaleLeft To ScaleWidth Step R
          Line -(i, .RegVal(CDbl(i))), vbBlue \ 2
    Next i

Open in new window


This is what I have in VB.net so far...and its not working at all, my line is way off

For i = Me.Left To Me.Width Step R
            g.DrawLine(Pens.MidnightBlue, 0, CInt(Reg.RegVal(CDbl(Me.Left))), i, CInt(Reg.RegVal(CDbl(i))))
        Next i

Open in new window


is there an equivalent in VB.net to the VB6 coding methods?
0
Comment
Question by:ezdrt
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 36552372
have a look at here

http://home.wordpress.com/2010/05/20/vb6-scaleleft-scaletop-to-vb-net-conversion/

TextBox1.ScaleTop
TextBox1.ScaleLeft
TextBox1.ScaleHeight
TextBox1.ScaleWidth

-->

TextBox1.ClientRectangle.Top
TextBox1.ClientRectangle.Left
TextBox1.ClientRectangle.Height
TextBox1.ClientRectangle.Width
0
 

Author Comment

by:ezdrt
ID: 36552525
oh thanks:)

That will work, but what about the CurrentX and CurrentY properties? Is there a work around for those?

basically im clicking the screen and it draws a circle and a regression line. everytime i click another circle on the screen the line is suppossed to adjust itself.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36552564
Can you show a screenshot and/or screencast of it in action?
0
 

Author Comment

by:ezdrt
ID: 36552611
Sure thing :)

heres the Form code

Option Explicit On

Imports System.Data
Imports System.Math
Imports System.Drawing.Graphics
Imports System.Drawing.Printing
Imports System.Windows.Forms


Public Class Form1

    Private Structure PointType
        Dim X As Double
        Dim Y As Double
    End Structure

    Dim Reg As New RegressionObject
    Private LastEndPoint As New Point(0, 0)

    Const R = 100
    Dim P(0 To 200) As PointType

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Reg.Degree = 3
    End Sub

    Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        Dim X, Y As Single

        X = e.X
        Y = e.Y
        With Reg
            .XYAdd(X, Y)
            P(.XYCount).X = X
            P(.XYCount).Y = Y
            Redraw()
        End With
    End Sub

    Private Sub Redraw()
        Dim g As System.Drawing.Graphics
        g = CreateGraphics()
        Dim i&

        With Reg
            For i = 1 To .XYCount
                DrawACircle(g, New Point(P(i).X, P(i).Y), R / 2)
            Next i

            LastEndPoint = New Point(P(i).X, P(i).Y)

            LastEndPoint.X = Me.ClientRectangle.Left
            LastEndPoint.Y = .RegVal(CDbl(Me.ClientRectangle.Left))
            For i = Me.ClientRectangle.Left To Me.ClientRectangle.Width Step R
                g.DrawLine(Pens.BlueViolet, LastEndPoint.X, LastEndPoint.Y, i, CInt(.RegVal(CDbl(i))))
            Next i
        End With
    End Sub

    Private Sub DrawACircle(ByRef g As Graphics, ByRef center As Point, ByVal radius As Integer)
        ' Select a pen object and make it red
        Dim pn As New Pen(Color.Red)
        pn.Width = 5
        ' Create a bounding rectangle and make its center the center of our point
        ' Then make its width 2 * the radius
        ' Then draw our ellipse
        Dim rect As New Rectangle(center.X - 12, center.Y - 12, radius, radius)
        g.DrawEllipse(pn, rect)
    End Sub

End Class

Open in new window


SCREEN
http://postimage.org/image/1s2wynrhg/



HERES THE VB6 VERSION....this is what its supposed to do
http://postimage.org/image/1s4dvuvac/


0
 

Author Comment

by:ezdrt
ID: 36552622
Oh I almost forgot, heres more of the VB6 code...

'WHEN THE MOUSE IS DOWN IT GET THE COORDINATES
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  With Reg
    .XYAdd X, Y
    P(.XYCount).X = X
    P(.XYCount).Y = Y
    Redraw
  End With
End Sub


'THIS IS WHERE THE CIRCLE AND LINES ARE DRAWN
Private Sub Redraw()
Dim i&
  Cls
  With Reg
    For i = 1 To .XYCount
      Circle (P(i).X, P(i).Y), R
    Next i
    CurrentX = ScaleLeft
    CurrentY = .RegVal(CDbl(ScaleLeft))
    For i = ScaleLeft To ScaleWidth Step R
      Line -(i, .RegVal(CDbl(i))), vbBlue
    Next i
  End With


End Sub

Open in new window

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 51

Expert Comment

by:HainKurt
ID: 36552677
you shoudl not start with 0 all the time... but dont know where to start :) maybe you should start from previous point

g.DrawLine(Pens.MidnightBlue, 0, CInt(Reg.RegVal(CDbl(Me.Left))), i, CInt(Reg.RegVal(CDbl(i))))
-->
g.DrawLine(Pens.MidnightBlue, ?, CInt(Reg.RegVal(CDbl(Me.Left))), i, CInt(Reg.RegVal(CDbl(i))))
0
 
LVL 15

Expert Comment

by:x77
ID: 36553100
1 - g - Graphics - is a Disposable object, You need use dispose
            or use "Using Sintax to ensure object dispose:

      Using g = Me.CreateGraphics
          ....
      End Using

2 - Vb clasic Line method is like g.DrawLine method but:      Lie x1,y1,x2,y2
      it support abreviated syntax                                           Line -x2,y2
      Equivalent to:                                                                  Line Currentx,Currenty,x2,y2

      Line method  set the CurrentX and Current y for final Point.
       
To do same on Vb.Net, Declare CurrentX,Currenty and set it:

            Dim CurrentX = Me.ClientRectangle.Left
            Dim CurrentY = .RegVal(CDbl(Me.ClientRectangle.Left))
            For i = CurrentX+R To Me.ClientRectangle.Width Step R
                Dim V = CInt(.RegVal(CDbl(i)))
                g.DrawLine(Pens.BlueViolet, CurrentX, CurrentY, i, v)
                CurrentX=i
                CurrentY=V
            Next i



Note that I Increment the Initial Point In Foor loop:      CurrentX+R  
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 36553101
Don't use CreateGraphics() like that.  Instead, use the Graphics supplied by the "e" parameter in the Paint() event of the Form.  To trigger a repaint, use the Refresh() method like this "Me.Refresh()".

See if this works: *Lots of changes in here...look closely*
Option Explicit On

Imports System.Data
Imports System.Math
Imports System.Drawing.Graphics
Imports System.Drawing.Printing
Imports System.Windows.Forms

Public Class Form1

    Private Structure PointType
        Dim X As Double
        Dim Y As Double
    End Structure

    Dim Reg As New RegressionObject
    Private LastEndPoint As New Point(0, 0)

    Const R = 100
    Dim P(0 To 200) As PointType

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Reg.Degree = 3
    End Sub

    Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Dim pt As New Point(e.X, e.Y)
            With Reg
                .XYAdd(pt.X, pt.Y)
                P(.XYCount).X = pt.X
                P(.XYCount).Y = pt.Y
            End With
            Me.Refresh()
        End If
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim i As Integer
        Dim g As System.Drawing.Graphics = e.Graphics
        With Reg
            Using pn As New Pen(Color.Red, 5)
                For i = 1 To .XYCount
                    DrawACircle(g, pn, New Point(P(i).X, P(i).Y), R / 2)
                Next i
            End Using
            
            LastEndPoint = New Point(0, Me.ClientRectangle.Height)
            Dim CurrentPoint As Point
            For i = 0 To Me.ClientRectangle.Width Step R
                CurrentPoint = New Point(i, CInt(.RegVal(CDbl(i))))
                g.DrawLine(Pens.BlueViolet, LastEndPoint, CurrentPoint)
                LastEndPoint = CurrentPoint
            Next i
        End With
    End Sub

    Private Sub DrawACircle(ByVal g As Graphics, ByVal P As Pen, ByVal center As Point, ByVal radius As Integer)
        ' Create a bounding rectangle and make its center the center of our point
        ' Then make its width 2 * the radius
        ' Then draw our ellipse
        Dim rect As New Rectangle(center, New Size(0, 0))
        rect.Inflate(radius, radius)
        g.DrawEllipse(P, rect)
    End Sub

End Class

Public Class RegressionObject

    Private _regVal As Integer

    Property Degree As Integer

    Property XYCount As Object

    Property RegVal(ByVal p1 As Double) As Integer
        Get
            Return _regVal
        End Get
        Set(ByVal value As Integer)
            _regVal = value
        End Set
    End Property

    Sub XYAdd(ByVal X As Single, ByVal Y As Single)
        Throw New NotImplementedException
    End Sub

End Class

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36553103
*Ignore my lines 69 thru 90...that was just to make my compiler shut up.  ;)
0
 

Author Comment

by:ezdrt
ID: 36561254
Thanks guys Im gonna give these suggestions a shot and ill let you know what I come up with:)

0
 

Author Closing Comment

by:ezdrt
ID: 36561457
Awesome, thank you sir :)
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

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

19 Experts available now in Live!

Get 1:1 Help Now