Monday, June 29, 2009

Resize the form according to System's resolution

If the .NET windows forms developed in 1280 x 800 resolution, it may not display properly in a pc with 800 x 600 resolution.

To resolve this issue, we need to resize the Form's controls and Font Size as well.


Just call Resize_Form in Form's Load Event

[CODE]
Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

'Change Form's Width and height to Workable area of the screen
Me.Width = Screen.PrimaryScreen.Bounds.Width
Me.Height = Screen.PrimaryScreen.Bounds.Height

Me.Bounds = Screen.PrimaryScreen.Bounds

'Resize the form
Resize_Form()
End Sub

[/CODE]

[CODE]
Private Sub Resize_Form()
Dim cControl As Control
If (Screen.PrimaryScreen.Bounds.Height = 800) And (Screen.PrimaryScreen.Bounds.Width = 1280) Then
Exit Sub
End If


With Me
For i_i = 0 To .Controls.Count - 1
If TypeOf .Controls(i_i) Is ComboBox Then ' cannot change Height
.Controls(i_i).Left = .Controls(i_i).Left / 1280 * Me.Width
.Controls(i_i).Top = .Controls(i_i).Top / 800 * Me.Height
.Controls(i_i).Width = .Controls(i_i).Width / 1280 * Me.Width

ElseIf TypeOf .Controls(i_i) Is Panel Then
.Controls(i_i).Left = .Controls(i_i).Left / 1280 * Me.Width
.Controls(i_i).Top = .Controls(i_i).Top / 800 * Me.Height
.Controls(i_i).Width = .Controls(i_i).Width / 1280 * Me.Width
.Controls(i_i).Height = .Controls(i_i).Height / 800 * Me.Height
'Do the same for Panel's Children
For Each cControl In .Controls(i_i).Controls

cControl.Left = cControl.Left / 1280 * Me.Width
cControl.Top = cControl.Top / 800 * Me.Height
cControl.Width = cControl.Width / 1280 * Me.Width
cControl.Height = cControl.Height / 800 * Me.Height
cControl.Font = New Font(cControl.Font.Name, cControl.Font.Size / 1280 * Me.Width)

If TypeOf cControl Is PictureBox Then 'Make it stretch
cControl.BackgroundImageLayout = ImageLayout.Stretch
End If

Next
ElseIf TypeOf .Controls(i_i) Is System.Windows.Forms.TabControl Then
.Controls(i_i).Left = .Controls(i_i).Left / 1280 * Me.Width
.Controls(i_i).Top = .Controls(i_i).Top / 800 * Me.Height
.Controls(i_i).Width = .Controls(i_i).Width / 1280 * Me.Width
.Controls(i_i).Height = .Controls(i_i).Height / 800 * Me.Height

ElseIf TypeOf .Controls(i_i) Is GroupBox Then
.Controls(i_i).Left = .Controls(i_i).Left / 1280 * Me.Width
.Controls(i_i).Top = .Controls(i_i).Top / 800 * Me.Height
.Controls(i_i).Width = .Controls(i_i).Width / 1280 * Me.Width
.Controls(i_i).Height = .Controls(i_i).Height / 800 * Me.Height

Else
.Controls(i_i).Left = .Controls(i_i).Left / 1280 * Me.Width
.Controls(i_i).Top = .Controls(i_i).Top / 800 * Me.Height
.Controls(i_i).Width = .Controls(i_i).Width / 1280 * Me.Width
.Controls(i_i).Height = .Controls(i_i).Height / 800 * Me.Height
End If

.Controls(i_i).Font = New Font(.Controls(i_i).Font.Name, .Controls(i_i).Font.Size / 1280 * Me.Width)
Next i_i
End With

End Sub
[/CODE]

No comments:

Post a Comment