窗体?对,现在总算开始有点像在看VB的文章了。不过我要说:大虾请重新来过。我们把Visual Studio.net自动生成的代码丢一边去,从零开始。其实到现在为止我一直都在用SnippetCompiler来写代码。干脆打开WindowsForm.vb好了。

首先是导入必要的命名空间:
imports System
imports System.Drawing
imports Microsoft.VisualBasic
imports System.Collections
imports System.ComponentModel
imports System.Windows.Forms


然后要定义主窗体。主窗体是一个类,从System.Windows.Forms.Form中继承:
public class WindowsForm : inherits System.Windows.Forms.Form
    public sub New()     '在VB中,一行写上两句或多句话的时候要用冒号隔开。
    end sub  
end class

然后是main函数:
public module MyModule
     sub Main()
         Application.Run(new WindowsForm())
     end sub
end module

运行Debug菜单中的Start as WinForm,一个简单的窗体就出现了:


但是这个窗体太简单。要想加上标题,设置边框样式等等的话,你可以到.net Framework SDK文档中找找System.Windows.Forms.Form类有哪些属性和方法。比如我们在代码中的New过程中加入:
me.Text = "Hello!"

窗体的标题就变成“Hello!”了。


接下来就是为窗体的事件写代码。前面说过将代码与事件绑定用AddHandler,其实还有另一种方法,就是在过程后面加上handles子句。比如我们在New过程后面加上一个过程来响应窗体关闭的事件:
private sub ByeBye(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
handles MyBase.closing                        '上一行的最后有一个空格加下划线,这是换行的标志。一
    MessageBox.Show("Byebye!","See You")      '行中写不满,使用换行标志,就可以在下一行继续。
end sub

在这里MyBase是VB.net的关键字,表示当前对象的父类。

当运行的时候,我们关闭窗体时,就会弹出对话框来跟我们说“ByeBye!”


那么Form类有多少事件可以接收呢?这个同样要到.net Framework SDK文档中找找。这里就不赘述了。


接下来是添加控件。添加控件有三个步骤:定义控件,设置控件属性,然后是在窗体中添加它。我们专门写一个过程来添加控件,过程名字叫InitializeComponent()。以添加一个按钮为例。

首先在WindowsForm类中添加一个成员Button1:
dim WithEvents Button1 as System.Windows.Forms.Button     '定义控件
这里withevents是VB.net关键字,表示声明的这个变量是带有事件的。

然后我们定义InitializeComponent过程:
private sub InitializeComponent()
    Button1 = new System.Windows.Forms.Button
    Button1.location=new point(10,10)                     '设置控件属性。这里是按钮的位置
    Button1.size = new size(100,40)                       '大小
    Button1.text = "&Click Me!"                           '按钮上的文字
    me.Controls.Add(Button1)                              '在窗体中添加控件
end sub


这样,窗体上就多了一个按钮。


然后我们为这个按钮的单击事件添加代码:
private sub ButtonClicked(Byval sender as Object, Byval e as System.eventArgs) _
handles Button1.Click
    me.text = "You Clicked Button1."
end sub


这样我们单击按钮的时候,窗体的标题就变了:


这就是比较简单的添加控件的方式。好了,这个例子的完整代码如下:
imports System
imports System.Drawing
imports Microsoft.VisualBasic
imports System.Collections
imports System.ComponentModel
imports System.Windows.Forms

public class WindowsForm : inherits System.Windows.Forms.Form
    dim WithEvents Button1 as System.Windows.Forms.Button
   
 public sub New()
        InitializeComponent()
        me.Text = "Hello!"
 end sub
   
    private sub ByeBye(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) handles MyBase.closing
        MessageBox.Show("Byebye!","See You")
    end sub
   
    private sub ButtonClicked(Byval sender as Object, Byval e as System.eventArgs) handles Button1.Click
        me.text = "You Clicked Button1."
    end sub

    private sub InitializeComponent()
        Button1 = new System.Windows.Forms.Button
        Button1.location=new point(10,10)
        Button1.size = new size(100,40)
        Button1.text = "&Click Me!"
        me.Controls.Add(Button1)
    end sub
end class

public module MyModule
 sub Main()
  Application.Run(new WindowsForm())
 end sub
end module

到这里,你可能开始理解Visual Studio.net中窗体设计器的自动生成代码了。哪些代码看起来复杂,其实也就这些东西,但是它写得更规范些。对了,这里还有个小窍门:你不觉得那个按钮看起来不太顺眼吗?对,XP下的按钮不应该是这样的。我们要在InitializeComponent()当中添加一句:
Button1.FlatStyle = FlatStyle.System

然后在窗体的New过程中的第一句加上:

Application.EnableVisualStyles()

生成的窗体就具有XP的外观了:



不管你的窗体是什么样的布局,有哪些控件,能做些什么,请牢牢记住一点:窗体是一个类。类能做什么,窗体就能做什么。要想掌握窗体,请先掌握类。

下面是:
VB.net入门(9):多窗体:完了~~