vb.net怎么用代码创建一个Form,再添加一个退出按钮?
怎么用代码创建一个Form,再添加一个退出按钮?感觉这样写类代码,对类的理解和控件属性、方法的理解,会加深很多。
就像我以前问的VFP问题那样:
https://bbs.bccn.net/thread-459302-1-1.html
程序代码:
public class from1
Private WithEvents myForm As Form
Private WithEvents btn As Button
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
myForm = New Form
btn = New Button
With btn
.Text = "退出"
.Height = 30
.Width = 60
.Top = 100
.Left = 120
End With
With myForm
.Text = "新建窗体"
.Controls.Add(btn)
.ShowDialog()
End With
End Sub
Private Sub btn_Click(sender As System.Object, e As System.EventArgs) Handles btn.Click
MsgBox("这是我用代码新建的Form")
myForm.Close()
End Sub
End Class
程序代码:Module Module1
Sub Main()
Dim fm As New Form
fm.ShowDialog()
End Sub
End Module
程序代码:Module Module1
Sub Main()
Dim fm As New MyForm
fm.ShowDialog()
End Sub
Public Class MyForm
Inherits System.Windows.Forms.Form
'Private Sub InitializeComponent()
'Me.Text = "用类的方式学习窗体和控件"
'Me.Controls.Add(Me.Button1)
'Me.Button1 = New System.Windows.Forms.Button
'End Sub
'Friend WithEvents Button1 As System.Windows.Forms.Button
End Class
End Module