2012-03-24 2 views
2

CodeDom을 통해 VB.NET Windows Forms 응용 프로그램을 작성하려면 어떻게해야합니까?CodeDom을 사용하여 VB.NET Windows Forms 응용 프로그램 작성

나는 모든 것을 시도해 보았다. 가장 가까운 것은 다음과 같은 코드이다. 처음에는 좋지 않은 명령 프롬프트 창을 보여 주며, 잠시 동안 양식을 보여 주며 모든 것이 사라진다. 다른 적절한 방법이 있습니까? 예를 들어 주시면 감사하겠습니다.

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

+0

메모장이 아닌 비주얼 스튜디오 Visual Basic Express 2010을 사용하고 싶을 수도 있습니다. http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-basic-express – mellodev

+0

@shanabus, CodeDom이 내 응용 프로그램 내에서 코드를 컴파일하는 방법을 모르는 것 같습니다. , 메모장 일을 대안으로 생각했습니다. – NetInfo

답변

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로 컴파일하려는 응용 프로그램입니다. 그리고 원하는 방식으로 변경할 수 있습니다. 당신이 이름 OutputApp에서 프로젝트 \ 빈 \ 디버그에서 독립 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 

.

다시 한 번 모두에게 감사드립니다. 위의 코드가 같은 것을하기 위해 노력하는 사람에게 유용 할 것입니다.

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 Forms 응용 프로그램으로 "응용 프로그램 유형"설정을 변경하십시오. CodeDom의 경우/target을 지정하도록 CompilerOptions를 설정해야합니다.

+0

나는이 글을 두 번 읽었으며 왜 아직도 다운 voted되었는지 알 수 없다. 어쩌면 당신은 단지 자신의 앱을 실행시키고 자하는 VB.NET 프로그래머를 위해 메시지 루프 나 기타 관련성이없는 정보를 완벽하게 구현하지 않았기 때문일 수 있습니다. +1. –

3

콘솔 응용 프로그램과 달리 Windows 응용 프로그램을 만들려면 컴파일러에 전달하는 CompilerParameters.CompilerOptions = "/target:winexe"을 지정해야합니다.

+0

온라인으로 많은 검색을 한 후 그 중 일부를 진행했지만 CompilerOptions = "/ target : winexe"로 설정하면 Sub Main을 찾을 수 없다고 표시됩니다. – NetInfo

관련 문제