2012-03-24 45 views
2

如何通過CodeDom編寫VB.NET Windows窗體應用程序?使用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 
+0

我應該問爲什麼? – shanabus 2012-03-24 03:17:08

+0

您可能想要節省一些頭痛的問題,並使用Visual Studio Visual Basic Express 2010而不是記事本。 http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-basic-express – mellodev 2012-03-24 03:17:36

+0

@shanabus,我似乎沒有得到CodeDom的工作方式來編譯我的應用程序內的代碼,認爲做記事本是一種替代解決方案。 – NetInfo 2012-03-24 04:54:56

回答

2

經過大量的在線研究,我得出以下結論,似乎工作得很好。 感謝大家對這個問題的任何意見。

首先打開一個新項目,將下面的代碼添加到按鈕中。 此代碼編譯您在將在下一步中創建的文本文件中編寫的代碼。

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 

如果你做了上述的一切,你點擊編譯它應該的名字OutputApp下創建的項目\ BIN \調試一個獨立的EXE之後。

再次感謝大家。 希望上面的代碼對任何試圖做同樣事情的人都有用。

5

它不起作用,因爲您不打電話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。

+0

我已經閱讀了兩次,仍然無法弄清楚爲什麼這是downvoted。也許是因爲你沒有提供一個徹底的實現消息循環或其他基本上不相關的信息的VB.NET程序員,只是想讓他的應用程序運行?從我+1。 – 2012-03-24 11:39:17

3

要創建Windows應用程序(而不是控制檯應用程序),必須在傳遞給編譯器的CompilerParameters中指定.CompilerOptions = "/target:winexe"

+0

經過大量的在線搜索之後,我對此有了一些進展,但是當我設置CompilerOptions =「/ target:winexe」時,它表示沒有找到Sub Main。 – NetInfo 2012-03-29 11:16:58

相關問題