-1

I am trying compiling with command line a sample program using MSChart control from here: https://social.msdn.microsoft.com/Forums/vstudio/en-US/95dd2d87-3750-421c-8d3a-cf6c9055acf3/read-text-file-and-plot-xy-chart?forum=vbgeneral

The code is also shown below:

Imports System
Imports System.IO
Imports System.Text
Imports System.ComponentModel
Imports System.Drawing
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports System.Windows.Forms.DataVisualization.Charting


Public Class Form1


    Dim data As List(Of DoublePoint)
    Class DoublePoint
        Property X As Double
        Property Y As Double
    End Class

    Public Shared Function RoundToSignificantDigits(ByVal x As Double, ByVal n As Integer) As Double
        ' adapted from http://stackoverflow.com/questions/202302/rounding-to-an-arbitrary-number-of-significant-digits
        If x = 0 Then
            Return 0
        End If
        Dim d As Double = Math.Ceiling(Math.Log10(Math.Abs(x)))

        Dim power As Integer = n - CInt(d)
        Dim magnitude As Double = Math.Pow(10, power)
        Dim shifted As Double = Math.Round(x * magnitude, MidpointRounding.AwayFromZero)
        Return shifted / magnitude

    End Function

    Sub LoadData(src As String)

        If Not File.Exists(src) Then
            Throw New FileNotFoundException(String.Format("File not found in LoadData: ""{0}""", src))
        End If

        data = New List(Of DoublePoint)
        Dim lineNumber As Integer = 1
        Dim badLines As New StringBuilder

        Using sr = New StreamReader(src)
            Dim parts As String()
            Dim x As Double
            Dim y As Double

            While Not sr.EndOfStream
                parts = sr.ReadLine.Split({" "c}, StringSplitOptions.RemoveEmptyEntries)
                If parts.Count > 0 Then
                    If parts.Count = 3 Then
                        If Double.TryParse(parts(0), x) Then
                            If Double.TryParse(parts(1), y) Then
                                data.Add(New DoublePoint With {.X = x, .Y = y})
                            Else
                                badLines.AppendFormat("Bad line {0}: could not parse y-value {1} as a Double." & vbCrLf, lineNumber, parts(1))
                            End If
                        Else
                            badLines.AppendFormat("Bad line {0}: could not parse x-value {1} as a Double." & vbCrLf, lineNumber, parts(0))
                        End If
                    Else
                        badLines.AppendFormat("Bad line {0}: incorrect number of items ({1})." & vbCrLf, lineNumber, parts.Count())
                    End If
                End If
                lineNumber += 1
            End While
        End Using

        'TODO: inform user of bad lines in a better way
        If badLines.Length > 0 Then
            MsgBox(badLines.ToString())
        End If

    End Sub

    Sub ShowData()
        Dim minX As Double = Double.MaxValue
        Dim maxX As Double = Double.MinValue
        Dim minY As Double = Double.MaxValue
        Dim maxY As Double = Double.MinValue


        Dim Chart1 As New Chart

    Chart1.SuspendLayout()
        Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Point
        For Each p In data
            Chart1.Series(0).Points.AddXY(p.X, p.Y)

            If p.X < minX Then minX = p.X
            If p.X > maxX Then maxX = p.X

            If p.Y < minY Then minY = p.Y
            If p.Y > maxY Then maxY = p.Y
        Next

        ' attempt to get the axis limits as "neater" numbers
        minX = RoundToSignificantDigits(minX, 2)
        maxX = RoundToSignificantDigits(maxX, 2)
        minY = RoundToSignificantDigits(minY, 2)
        maxY = RoundToSignificantDigits(maxY, 2)

        Chart1.Legends.Clear()

        Chart1.ChartAreas(0).AxisX.IsStartedFromZero = False
        Chart1.ChartAreas(0).AxisY.IsStartedFromZero = False

        Chart1.ChartAreas(0).AxisX.Minimum = minX
        Chart1.ChartAreas(0).AxisX.Maximum = maxX
        Chart1.ChartAreas(0).AxisY.Minimum = minY
        Chart1.ChartAreas(0).AxisY.Maximum = maxY

        Chart1.ResumeLayout()

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadData("chartdata.txt")

        ShowData()

    End Sub

<STAThread()> _
    Shared Sub Main()
        'Application.EnableVisualStyles()
        Application.Run(New Form1())

    End Sub

End Class

Using command: C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\vbc.exe Prog.vb

The VB Compiler then throws error that "Type Chart is not Defined" which I already did in above code as "Imports System.Windows.Forms.DataVisualization.Charting" There is also some problem exist with declaration of Chart1 Object.

Any help is appreciated.

user3051677
  • 147
  • 14
  • chartdata.txt contains data like: 0.0000E+0 -144.0000E-3 -151.5304E-3 24.9938E-12 -148.0000E-3 -151.6275E-3 21.78E-12 -148.0000E-3-151.6275E-3 21.78E-12-148.0000E-3-151.6275E-3 49.9875E-12 -150.0000E-3 -151.7454E-3 74.9813E-12 -150.0000E-3 -151.8030E-3 – user3051677 Oct 10 '16 at 19:38
  • You appear to be doing things the hard way - there are IDEs available, e.g. Visual Studio 2015 Community Edition, for free, which make it *much* easier to develop. – Andrew Morton Oct 10 '16 at 19:41
  • Right Now I don't have an IDE on my system. Doing this in an IDE will take the same above codes for the task. – user3051677 Oct 10 '16 at 19:43
  • The point of using the IDE is not to write different code, it is to take care of the grunt work. The kind that is repetitive, boring and so easy to get wrong. You'd never get this wrong when you use the IDE, it won't let you drop a Chart control on the form without also referencing the assembly. Which you did not do. – Hans Passant Oct 10 '16 at 20:10
  • @HansPassant IDE work best for larger projects, the Andrew Morton Answer requires linking to many unnecessary dll references e.g.System.Net.Http.dll and others. Using command line for one or two file project you can control linking to assemblies very easily. BTW VB originally was not intended to work as CLI rather it was complete IDE. – user3051677 Oct 12 '16 at 06:54

2 Answers2

1

You will need more parameters to vbc.exe.

I copied your program into VS2015 as "WindowsApplication3" and compiled it for .NET Framework 4.5.2, Any CPU, debug mode, and got the following command line to compile it:

C:\Program Files (x86)\MSBuild\14.0\bin\vbc.exe /noconfig /imports:Microsoft.VisualBasic,System,System.Collections,System.Collections.Generic,System.Data,System.Drawing,System.Diagnostics,System.Windows.Forms,System.Linq,System.Xml.Linq,System.Threading.Tasks /optioncompare:Binary /optionexplicit+ /optionstrict+ /nowarn:42016,41999,42017,42018,42019,42032,42036,42020,42021,42022 /optioninfer+ /nostdlib /platform:anycpu32bitpreferred /rootnamespace:WindowsApplication1 /sdkpath:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5.2" /preferreduilang:en-US /highentropyva+ /doc:obj\Debug\WindowsApplication3.xml /define:"CONFIG=\"Debug\",DEBUG=-1,TRACE=-1,_MyType=\"WindowsForms\",PLATFORM=\"AnyCPU\"" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5.2\System.Core.dll","C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5.2\System.Data.DataSetExtensions.dll","C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5.2\System.Data.dll","C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5.2\System.Deployment.dll","C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5.2\System.dll","C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5.2\System.Drawing.dll","C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5.2\System.Net.Http.dll","C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5.2\System.Windows.Forms.DataVisualization.dll","C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5.2\System.Windows.Forms.dll","C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5.2\System.Xml.dll","C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5.2\System.Xml.Linq.dll" /main:WindowsApplication1.My.MyApplication /debug+ /debug:full /filealign:512 /out:obj\Debug\WindowsApplication3.exe /ruleset:"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\Rule Sets\MinimumRecommendedRules.ruleset" /subsystemversion:6.00 /resource:obj\Debug\WindowsApplication1.Form1.resources /resource:obj\Debug\WindowsApplication1.Resources.resources /target:winexe /utf8output ApplicationEvents.vb Form1.vb Form1.Designer.vb "My Project\AssemblyInfo.vb" "My Project\Application.Designer.vb" "My Project\Resources.Designer.vb" "My Project\Settings.Designer.vb" "C:\Users\Andrew\AppData\Local\Temp.NETFramework,Version=v4.5.2.AssemblyAttributes.vb"

You should be able to see which parts you need, or refer to Visual Basic Command-Line Compiler for details.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
0

Finally I figured out how to compile using command line as: C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\vbc.exe /target:exe /out:MyChart /reference:System.Windows.Forms.dll,System.Windows.Forms.DataVisualization.dll Form2.vb

I also modified the code as below:

Imports System
Imports System.IO
Imports System.Text
Imports System.ComponentModel
Imports System.Drawing
'Imports System.Collections.Generic

Imports System.Windows.Forms
Imports System.Windows.Forms.DataVisualization.Charting

Public Class Form2
    Inherits Form

    Dim Chart1 As Chart

    Dim i As Integer = 0
    Dim j As Integer = 0
    Dim WithEvents Button1 As Button
    Dim Dp As DataPoint


    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Text = "Testing"
        Me.CenterToScreen()
    Me.Button1 = New Button()
    Me.Chart1 = New Chart()
    Button1.Text = "My Button Text"
    Chart1.Left = 200
    Me.Controls.Add(Chart1)
    Me.Controls.Add(Button1)
    End Sub


    Private Sub Button1_Click(sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    MsgBox("Button Clicked!")
    Chart1.Width = 500
    Chart1.Height = 500
    Chart1.Series.Add("Series1")
    Chart1.Series("Series1").Points.AddXY(1, 2)
    Chart1.Series("Series1").Points.AddXY(3, 2)

    Chart1.Series("Series1").Points.AddXY(4, 2)
    Chart1.Series("Series1").Points.AddXY(5, 0)
    Chart1.Series(0).IsVisibleInLegend = True
    Chart1.Update
    Chart1.Visible = true



    End Sub

<STAThread()> _
    Shared Sub Main()
        Application.EnableVisualStyles()
        Application.Run(New Form2())

    End Sub

End Class

But the problem still exist It compile successfully but don't show the plotted points, just white space....

user3051677
  • 147
  • 14
  • I resolved above issue, actually you have to setup the Chart Area as well to show the chart, I will post the working code soon. – user3051677 Oct 12 '16 at 06:49