【问题标题】:how to add form in main function (Visual Basic)如何在主函数中添加表单(Visual Basic)
【发布时间】:2015-01-02 17:11:55
【问题描述】:

我正在尝试使用 main 函数在 VB 中制作一个简单的应用程序,以显示一个表单并在其上带有一个文本标签。我可以使用表单并为其添加标签控件,但对于我的项目,我需要使用 main .. 就像我想为整个应用程序编写代码一样,我的老师说不要使用图形界面来开发..请帮助

这是我的代码......它显示空表单,没有标签......请告诉我如何在主函数中添加控件......

    Module Module1
    Sub Main()
        Dim f As New Form
        Application.Run(New Form1())
        Dim z As Label
        z = New Windows.Forms.Label
        Form1.Controls.Add(z)
        z.Text = "Hello"
        z.Show()
    End Sub
End Module

【问题讨论】:

  • Application.Run 之后的所有代码都应该是格式
  • 将对 Application.Run 的调用移动到代码的末尾,替换对 z.Show() 的调用。

标签: vb.net forms main


【解决方案1】:

除非您知道 Application.Run 的用途,否则不应使用它,请查看此综合答案以了解其用途Application.Run(Form) vs. Form.Show()?

试试这个,让你的表单在里面显示你的标签:

Module Module1
    Sub Main()
        Dim myForm as Form = New Form
        Dim myLabel As Label = New Windows.Forms.Label
        myLabel.Text = "Hello"
        myForm.Controls.Add(myLabel)

        myForm.Show()

    End Sub
End Module

您还可以随时以编程方式访问控件的所有设计属性,例如,还可以在将控件添加到表单之前添加以下内容,以定义标签控件的位置

myLabel.Location = New Point(20, 20)

【讨论】:

    【解决方案2】:

    您的尝试已接近尾声,但一些关键部分丢失/出现故障。

    我修改了您的代码并添加了一些 cmets 来帮助您入门。

    Module Module1
        Sub Main()
            ' Create a new form object, but don't display it yet.
            Dim f As New Form
    
            ' Create a new Label.
            ' It will not be added to the form automatically.
            Dim z As New Label
            z.Text = "Hello"
            ' Now add the label to the form.
            f.Controls.Add(z)
    
            ' Open the form and wait until the user closes it before continuing.
            f.ShowDialog()
        End Sub
    End Module
    

    您可能要考虑的一件事是将表单 (f) 包装在 Using block 中,这是一个很好的做法,因为它会自动处理对象的正确处置。如果你这样做,你的代码现在看起来像:

    Module Module1
        Sub Main()
            ' Create a new form object, but don't display it yet.
            Using f As New Form
    
                ' Create a new Label.
                ' It will not be added to the form automatically.
                Dim z As New Label
                z.Text = "Hello"
                ' Now add the label to the form.
                f.Controls.Add(z)
    
                ' Open the form and wait until the user closes it before continuing.
                f.ShowDialog()
    
            End Using ' Now all the "garbage" of Form f is cleaned up.
    
        End Sub
    End Module
    

    【讨论】:

      【解决方案3】:

      您需要将标签添加到“f”实例,而不是 Form1.Controls。

      Module Module1
      Sub Main()
          Dim f As New Form
          Dim z As Label
          z = New Windows.Forms.Label
          z.Text = "Hello"
      
          f.Controls.Add(z)        
          f.ShowDialog()
      
      End Sub
      End Module
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-12
        相关资源
        最近更新 更多