加任何一个控件都显示
System.Windows.Forms.Control.Tag.get 返回 Nothing。。请问该如何处理。
代码如下
Public Class Form1
Dim x As Single = 0
Dim y As Single = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'获取当前窗体尺寸
x = Me.Width
y = Me.Height
SetTag(Me)
Dim s As Screen
s = Screen.PrimaryScreen
Me.Size = New Size(s.Bounds.Width, s.Bounds.Height)
Me.Top = s.Bounds.Height / 2 - Me.Height / 2
Me.Left = s.Bounds.Width / 2 - Me.Width / 2
End Sub
#Region "窗体"
Private Sub SetTag(ByVal obj As Object)
For Each con As Control In obj.Controls
con.Tag = con.Width & ":" & con.Height & ":" & con.Left & ":" & con.Top & ":" & con.Font.Size
If con.Controls.Count > 0 Then
SetTag(con)
End If
Next
End Sub
Private Sub SetControls(ByVal newx As Single, ByVal newy As Single, ByVal obj As Object)
For Each con As Control In obj.Controls
con.AutoSize = False
Dim mytag() As String = con.Tag.ToString.Split(":")
con.Width = mytag(0) * newx
con.Height = mytag(1) * newy
con.Left = mytag(2) * newx
con.Top = mytag(3) * newy
Dim currentSize As Single = (mytag(1) * newy * mytag(4)) / mytag(1)
con.Font = New Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit)
If con.Controls.Count > 0 Then
SetControls(newx, newy, con)
End If
Next
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
Dim newx As Single = Me.Width / x
Dim newy As Single = Me.Height / y
SetControls(newx, newy, Me)
End Sub
#End Region
End Class