vb.net 如何在主函数中添加表单(Visual Basic)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27745820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:36:49  来源:igfitidea点击:

how to add form in main function (Visual Basic)

vb.netformsmain

提问by mightyWOZ

i am trying to make a simple application in VB using main function , to show a form and with a text label on it . i can do it using form and adding label control to it , but for my project i need to do it using main .. its like i want to write code for whole application, my teacher said not to use graphical interface to develop ..please help

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

here is my code ... it shows empty form , no label....please tell me how to add controls in main function.....

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

    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

回答by Crowcoder

You need to add the label to the "f" instance, not Form1.Controls.

您需要将标签添加到“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

回答by Aram Tchekrekjian

You should not use Application.Run unless you know what it does, check this comprehensive answer for its uses Application.Run(Form) vs. Form.Show()?

你不应该使用 Application.Run 除非你知道它是做什么的,检查这个综合答案它的用途Application.Run(Form) vs. Form.Show()?

Try this instead to make your Form show with your label inside it:

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

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

Also you can always programmatically access all the design properties of the controls, the below, for example, can also be added before adding the control to the form to define the location of your label control

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

myLabel.Location = New Point(20, 20)

myLabel.Location = New Point(20, 20)

回答by Jason Faulkner

You are close with your attempt, but a few key parts are missing/out of order.

您的尝试已接近尾声,但有几个关键部件缺失/故障。

I modified your code and added some comments to help get you started.

我修改了您的代码并添加了一些注释以帮助您入门。

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

One thing you might want to consider is wrapping the form (f) in a Usingblockwhich is a good practice to get into since it will automatically handle proper disposal of the object. If you do this, your code now looks like:

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

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