Resizing controls in a form - vba

I have 3 controls and wanted to resize and reposition them based on what percentage increase or decrease the form WindowWidth property is. I came up with two formulas
One for increasing the .width and .Left properties and the other is for reducing the .width and .Left properties.
The form consists of a subform which is anchored to stretch down and across. All the textboxes are anchored to the left.
The initial size of the form is 20.5cm = 11624 twips
The anchored and maximised size of the form becomes 36.01cm = 20415 twips
These values are found using Me.Width and Me.WindowWidth properties respectively.
I want to be able to use the Me.Width and Me.WindowWidth properties to manipulate the position and width of the controls when the form gets resized.
I have the below code which works on form load but does not work if a user tries to resize using the access window. Any help or direction is highly welcome.
Private Sub Form_Resize()
Dim widthFactor As Double
If Me.WindowWidth = Me.Width Then
Exit Sub
widthFactor = 1 - ((Me.WindowWidth - Me.Width) / Me.WindowWidth) 'for decreasing the controls size and position
Me.txtFirst.Left = Me.txtFirst.Left * widthFactor
Me.txtSecond.Left = Me.txtSecond.Left * widthFactor
Me.txtThird.Left = Me.txtThird.Left * widthFactor
Me.txtFirst.Width = Me.txtFirst.Width * widthFactor
Me.txtSecond.Width = Me.txtSecond.Width * widthFactor
Me.txtThird.Width = Me.txtThird.Width * widthFactor
ElseIf Me.Width < Me.WindowWidth Then
widthFactor = ((Me.WindowWidth - Me.Width) / Me.Width) + 1 ' for increasing the controls size and position
Me.txtFirst.Left = Me.txtFirst.Left * widthFactor
Me.txtSecond.Left = Me.txtSecond.Left * widthFactor
Me.txtThird.Left = Me.txtThird.Left * widthFactor
Me.txtFirst.Width = Me.txtFirst.Width * widthFactor
Me.txtSecond.Width = Me.txtSecond.Width * widthFactor
Me.txtThird.Width = Me.txtThird.Width * widthFactor
End If
End Sub

Related

How to get rid of white space between picture boxes in VB.NET?

For fun I'm trying to recreate the first level of one my favorite games, Fire Emblem 7. I got a picture of the map online. I've broken down the image into "squares" with each square assigned a picture box to display the image. This is because each square needs to have certain properties such as terrain values, units inside them, etc.
The actual image is quite small (240 x 160), so I want to be able to scale it to any user defined value. The size of each square should be 16c x 16c with a scaler of c (all map dimensions are divisible by 16). For some reason, when c > 1, white lines appear between the squares. I've check the code and it looks like the squares should be adjacent with no empty spaces regardless of c.
I have provided a piece of my code and links to the images of different values of c below. Thank you for you help.
'This Sub Creates The Map From Initial Image And Assigns Part Of Image to Each Square
Public Sub New(Name As String, Image As Image)
Dim cropRect As Rectangle
Dim cropImage As Bitmap
Me.Name = Name
Me.Image = Image
Height = Me.Image.Height / 16
Width = Me.Image.Width / 16
ReDim Squares(Height - 1, Width - 1)
For i = 0 To Height - 1
For j = 0 To Width - 1
cropRect = New Rectangle(16 * j, 16 * i, 16, 16)
cropImage = New Bitmap(16, 16)
Graphics.FromImage(cropImage).DrawImage(Me.Image, 0, 0, cropRect, GraphicsUnit.Pixel)
Squares(i, j) = New Square(cropImage)
Next
Next
End Sub
'This Sub Sizes Each Square With User Defined Scale Value
Public Sub Draw(Scale As Double)
For i = 0 To Height - 1
For j = 0 To Width - 1
With Squares(i, j).Box
.Size = New Size(16 * Scale, 16 * Scale)
.Location = New Point(16 * j * Scale, 16 * i * Scale)
.SizeMode = PictureBoxSizeMode.StretchImage
End With
Next
Next
End Sub

Do I have to DISPOSE and how should I do it?

I zoom an image with the following code
While PictureBox1.Image.Height < ScreenHeight
PictureBox1.Image = New Bitmap(Image1, PictureBox1.Image.Width * 1.003, PictureBox1.Image.Height * 1.003)
Me.PictureBox1.Update()
End While
Do I need to dispose within the loop (but how do I preserve my image then?) or is it sufficient to dispose only once outside the loop?
Do I use Picturebox1.Image.Dispose()
Do I also need to use PictureBox1.Image = Nothing
Thanks for any useful information
This seems to work well
Dim Width, Height As Integer
Width = PictureBox1.Image.Width
Height = PictureBox1.Image.Height
While Height < ScreenHeight And (Microsoft.VisualBasic.DateAndTime.Timer - StartTime) < ScreenSaverDuration
PictureBox1.Image = New Bitmap(Image1, Width * 1.003, Height * 1.003)
Width = PictureBox1.Image.Width
Height = PictureBox1.Image.Height
Me.PictureBox1.Update()
PictureBox1.Image.Dispose()
End While
Dim Height As Integer = PictureBox1.Image.Height
Dim Width As Integer = PictureBox1.Image.Width
While PictureBox1.Image.Height < ScreenHeight
Height *= 1.003
Width *= 1.003
End While
PictureBox1.Image = New Bitmap(Image1, Width, Height)
PictureBox1.Update()

Minimum Vb.net Form size

Can we shrink the width of form size less than 132 and height less than 38 Please help me about it.
Here is the code:
For i As Integer = 0 To meWidth
If (Me.Width > 0) Then
Me.Width = Me.Width - 20
Me.Refresh()
For FadeCount = 40 To 40 Step 20
Me.Opacity = FadeCount / 100
Threading.Thread.Sleep(10)
Next
Else
Exit For
End If
Next
You can define your minimun form size with the property Form.MinimumSize. Check the MSDN Documentation about it.
Basically:
This property enables you to limit the size of a form to a specified minimum size. You can use this feature to prevent a user from sizing a window to an undesirable size. If this property is set to a Size object that is 0 in height and 0 in width, the form will have no minimum size beyond the limits set by Windows.
I couldn't find this answer anywhere and I would like to share my solution:
Me.Text = ""
Me.ControlBox = False
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Me.MinimumSize = New System.Drawing.Size(1, 1) 'HERE IS MY FIX
Me.Size = New System.Drawing.Size(200, 23)
By not allowing the MinimumSize property to remain at (0,0) by default, it seems to have cleared this bug up for me.
U can Control Minimum Size : Example
Dim MinWidth As Integer = 396
Dim MinHeigh As Integer = 369
If Me.Width <= MinWidth Then
Me.Width = MinWidth
End If
If Me.Height <= MinHeigh Then
Me.Height = MinHeigh
End If

VB.NET - Create An Image - Resize But Add Background To Remainding Space

I am attempting to resize an image to specific dimensions but I do not want to stretch the image at all if it is smaller than my chosen dimensions. Instead I want to add a black background around the image area that is not in use.
I think that the easiest way to do this would be to create a new image of my desired dimensions & set a background color & then add & center the image over top of this background.
I have created a Bitmap using:
Dim bmp As New Drawing.Bitmap(500, 500)
Dim grap As Drawing.Graphics = Drawing.Graphics.FromImage(bmp)
grap.Clear(Drawing.Color.Black)
From this point I got a bit lost on how to complete the process, all that is needed is to add an image to the Bitmap & center.
Any ideas would be much appretiated
I haven't tested this but it looks like you've pretty much got what you want but:
grap.Clear(Drawing.Color.Black)
Will surely just wipe the entire graphic back to black.
Try doing the clear prior to drawing image:
Graphics pic = this.CreateGraphics();
pic.Clear(Color.Black);
pic.DrawImage(img, new Point(center));
Ended up using:
' Load Image
Dim FilePath As String = "testimage.jpg"
Dim OriginalImage As New Bitmap(FilePath)
' Resize Image While Maintaining Aspect Ratio
Dim aspectRatio As Double
Dim newHeight As Integer
Dim newWidth As Integer
Dim maxWidth As Integer = 500
Dim maxHeight As Integer = 500
' Calculate Size
If OriginalImage.Width > maxWidth Or OriginalImage.Height > maxHeight Then
If OriginalImage.Width >= OriginalImage.Height Then ' image is wider than tall
newWidth = maxWidth
aspectRatio = OriginalImage.Width / maxWidth
newHeight = CInt(OriginalImage.Height / aspectRatio)
Else ' image is taller than wide
newHeight = maxHeight
aspectRatio = OriginalImage.Height / maxHeight
newWidth = CInt(OriginalImage.Width / aspectRatio)
End If
Else ' if image is not larger than max then increase size
If OriginalImage.Width > OriginalImage.Height Then
newWidth = maxWidth
aspectRatio = OriginalImage.Width / maxWidth
newHeight = CInt(OriginalImage.Height / aspectRatio)
Else
newHeight = maxHeight
aspectRatio = OriginalImage.Height / maxHeight
newWidth = CInt(OriginalImage.Width / aspectRatio)
End If
' Below keeps original height & width instead of resizing to fit new height / width
' newWidth = OriginalImage.Width
' newHeight = OriginalImage.Height
End If
Dim newImg As New Bitmap(OriginalImage, CInt(newWidth), CInt(newHeight)) '' blank canvas
' Create New Bitmap
Dim bmp As New Drawing.Bitmap(500, 500)
Dim grap As Drawing.Graphics = Drawing.Graphics.FromImage(bmp)
grap.Clear(Drawing.Color.Black)
Dim g As Graphics = Graphics.FromImage(bmp)
' Calculate Points To Insert Resized Image
Dim InsertX As Integer
Dim InsertY As Integer
' Calculate Y Axis Point
If newImg.Height >= 500 Then
InsertY = 0
Else
InsertY = CInt(((500 - newImg.Height) / 2))
End If
' Calculate X Axis Point
If newImg.Width >= 500 Then
InsertX = 0
Else
InsertX = CInt(((500 - newImg.Width) / 2))
End If
' Add Resized Image To Canvas
g.DrawImage(newImg, New Point(InsertX, InsertY))
By using just the ratio of the larger axis, you can fail in the situation that the other ratio forces you to exceed the other axis.
i.e. 1400x1000 ->( i want to fit into 300x200) -> but just with 1400/300 ratio (4.6) the result will be 300x214.
I think it would be useful to check both ratios and continue with the bigger

Referencing screen height and width in vb.net

How do I reference the screen height and width in vb.net? For example, the bottom right corner's locations, the top right corner's locations, etc.
I tried My.Computer.Screen but couldnt find anything that told me the size.
You can use:
My.Computer.Screen.Bounds
or:
Screen.PrimaryScreen.Bounds
Bounds is a rectangle that provides the size. Alternatively, you can look at the WorkingArea, which will not include the task bar and docked windows.
You can use something like:
My.Computer.Screen.Bounds.Size.Width
My.Computer.Screen.Bounds.Size.Height
For WPF you can use:
System.Windows.SystemParameters.PrimaryScreenWidth
System.Windows.SystemParameters.PrimaryScreenHeight
dim height as integer = Screen.PrimaryScreen.Bounds.Height
dim width as integer = Screen.PrimaryScreen.Bounds.Width
Insert this code into form_load. I put some resolutions...
Dim dw As Double
Dim dh as Double
Width = Screen.PrimaryScreen.Bounds.Width
If (Width = 1366) Then
dw = 1
ElseIf (Width = 1920) Then
dw = 1.4055
ElseIf (Width = 1280) Then
dw = 0.9379
End If
For Each c As Control In Me.Controls
c.Width = CInt(CDbl(c.Width * dw))
Next
Height = My.Computer.Screen.Bounds.Size.Height
If (Height = 768) Then
dh = 1
ElseIf (Height = 1080) Then
dh = 1.4062
ElseIf (Height = 1024) Then
dh = 1.3333
End If
For Each g As Control In Me.Controls
g.Height = CInt(CDbl(g.Height * dh))
Next