【问题标题】:Writing VB.NET Windows Forms Application using CodeDom使用 CodeDom 编写 VB.NET Windows 窗体应用程序
【发布时间】:2012-03-24 03:08:32
【问题描述】:

如何通过 CodeDom 编写 VB.NET Windows 窗体应用程序?

我已经尝试了所有方法,最接近它的是下面的代码,它首先显示了不好的命令提示符窗口,然后显示了大约一秒钟的表单,一切都消失了。 还有另一种正确的方法吗?非常感谢一个例子。

Public Module MyApp
    Public Sub main()
        Dim NewForm As New System.Windows.Forms.Form
        NewForm.Name = "Form1"
        NewForm.Text = "Form1"
        NewForm.Width = 300
        NewForm.Height = 300
        NewForm.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
        NewForm.ControlBox = True
        NewForm.MaximizeBox = False
        NewForm.MinimizeBox = True
        NewForm.Show()
    End Sub
End Module

【问题讨论】:

  • 您可能希望避免一些麻烦,并使用 Visual Studio Visual Basic Express 2010 而不是记事本。 microsoft.com/visualstudio/en-us/products/2010-editions/…
  • @shanabus,我只是不明白 CodeDom 是如何在我的应用程序中编译代码的,我想将记事本作为替代解决方案。
  • @mello702,谢谢你的建议,不过我已经在用了。

标签: .net vb.net winforms codedom


【解决方案1】:

它不起作用,因为您没有调用Application.Run()。没有它,没有什么可以停止主线程,它会退出,这就是程序和表单的结束。 NewForm.ShowDialog() 是另一个便宜的解决方案。

正确的咒语是:

Imports System.Windows.Forms

Public Module MyApp
    Public Sub Main()
        Application.EnableVisualStyles()
        Application.SetCompatibleTextRenderingDefault(False)
        Dim NewForm As New Form
        '' Set properties
        ''...
        Application.Run(NewForm)
    End Sub
End Module

要停止显示控制台窗口,您需要更改 EXE 类型。项目 + 属性,应用程序选项卡,将“应用程序类型”设置从控制台应用程序更改为 Windows 窗体应用程序。对于 CodeDom,您需要设置 CompilerOptions 以指定 /target。

【讨论】:

  • 我已经阅读了两遍,但仍然无法弄清楚为什么它被否决了。也许是因为您没有为只想让他的应用程序运行的 VB.NET 程序员提供消息循环的彻底实现或其他基本上不相关的信息?来自我的 +1。
【解决方案2】:

要创建 Windows 应用(与控制台应用相对),您必须在传递给编译器的 CompilerParameters 中指定 .CompilerOptions = "/target:winexe"

【讨论】:

  • 在网上搜索了很多之后,我在这方面取得了一些进展,但是当我设置 CompilerOptions = "/target:winexe" 时,它说 Sub Main 没有找到。
【解决方案3】:

经过大量在线研究,我得出以下结论,似乎工作得很好。 感谢大家对此主题的任何意见。

首先打开一个新项目,将以下代码添加到一个按钮中。 此代码编译您在下一步创建的文本文件中编写的代码。

  Private Sub CompilerButton_Click(sender As System.Object, e As System.EventArgs) Handles CompilerButton.Click
        Dim objCodeCompiler As System.CodeDom.Compiler.ICodeCompiler = New VBCodeProvider().CreateCompiler() ' We create object of the compiler

        Dim objCompilerParameters As New System.CodeDom.Compiler.CompilerParameters()
        ' Add reference
        objCompilerParameters.ReferencedAssemblies.Add("System.dll")
        objCompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll")
        objCompilerParameters.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll")

        'Compile in memory
        Dim Output1 As String = "OutputApp"
        objCompilerParameters.GenerateExecutable = True
        objCompilerParameters.OutputAssembly = Output1
        objCompilerParameters.CompilerOptions = "/target:winexe"

        Dim strCode As String = My.Resources.TextFile1.ToString
        Dim objCompileResults As System.CodeDom.Compiler.CompilerResults = _
        objCodeCompiler.CompileAssemblyFromSource(objCompilerParameters, strCode)

        If objCompileResults.Errors.HasErrors Then
            ' If an error occurs
            MsgBox("Error: Line>" & objCompileResults.Errors(0).Line.ToString & ", " & _
            objCompileResults.Errors(0).ErrorText)
            Exit Sub
        End If

    End Sub

然后在项目资源中添加一个文本文件并在其中添加以下代码。 此代码是您要编译为独立 EXE 的应用程序。您可以将其更改为您想要的方式。

Option Strict On
Imports System
Imports System.Windows.Forms
Imports System.Windows.Forms.Form
Imports Microsoft.VisualBasic

Namespace MyApp
    Public Class EntryPoint
        Public Shared Sub Main(args As [String]())
            Dim FrmMain As New Form1
            System.Windows.Forms.Application.Run(FrmMain)
        End Sub
    End Class
    Public Class Form1
        Inherits System.Windows.Forms.Form
        Private WithEvents Button1 As New Button
        Sub New()
            Application.EnableVisualStyles()
            Me.Text = "Form1"
            Button1.Text = "Click Me!"
            Button1.Top = 100
            Button1.Left = 100
            Me.Controls.Add(Button1)
        End Sub
        Private Sub Button1_Click(Sender As Object, E As EventArgs) Handles Button1.Click
            MsgBox("You Clicked Me!")
        End Sub
    End Class
End Namespace

如果您已完成上述所有操作,则单击编译后,它应该在项目 \bin\Debug 中创建一个独立的 EXE,名称为 OutputApp。

再次感谢大家。 希望上面的代码对尝试做同样事情的人有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-20
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多