运行 VB.Net 图表示例未使用 .NET v4.0.30319 编译
Running VB.Net Chart Example Not Compiling with .NET v4.0.30319
我正在尝试使用命令行从此处使用 MSChart 控件编译示例程序:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/95dd2d87-3750-421c-8d3a-cf6c9055acf3/read-text-file-and-plot-xy-chart?forum=vbgeneral
代码也如下所示:
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
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
使用命令:
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\vbc.exe Prog.vb
VB 编译器然后抛出错误 "Type Chart is not Defined" 我已经在上面的代码中做了 "Imports System.Windows.Forms.DataVisualization.Charting"
Chart1 对象的声明也存在一些问题。
感谢任何帮助。
您需要更多参数才能 vbc.exe。
我将您的程序复制到 VS2015 中作为 "WindowsApplication3" 并针对 .NET Framework 4.5.2、任何 CPU、调试模式编译它,并获得以下命令行来编译它:
C:\Program Files (x86)\MSBuild.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"
您应该能够看到您需要哪些部件,或参考Visual Basic Command-Line Compiler了解详情。
最后我想出了如何使用命令行进行编译:
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
我也修改了代码如下:
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
但是问题依旧存在 编译成功但是不显示标点,只有白色space.....
我正在尝试使用命令行从此处使用 MSChart 控件编译示例程序: https://social.msdn.microsoft.com/Forums/vstudio/en-US/95dd2d87-3750-421c-8d3a-cf6c9055acf3/read-text-file-and-plot-xy-chart?forum=vbgeneral
代码也如下所示:
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
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
使用命令: C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\vbc.exe Prog.vb
VB 编译器然后抛出错误 "Type Chart is not Defined" 我已经在上面的代码中做了 "Imports System.Windows.Forms.DataVisualization.Charting" Chart1 对象的声明也存在一些问题。
感谢任何帮助。
您需要更多参数才能 vbc.exe。
我将您的程序复制到 VS2015 中作为 "WindowsApplication3" 并针对 .NET Framework 4.5.2、任何 CPU、调试模式编译它,并获得以下命令行来编译它:
C:\Program Files (x86)\MSBuild.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"
您应该能够看到您需要哪些部件,或参考Visual Basic Command-Line Compiler了解详情。
最后我想出了如何使用命令行进行编译: 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
我也修改了代码如下:
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
但是问题依旧存在 编译成功但是不显示标点,只有白色space.....