Resizing Window Form control - vb.net

My problem is that I am unable to resize the form control according to the form size.
I have a form which contains a textbox, a label and a button. On button click I need to open the same form in a particular sized window and I want all the controls of the form should be proportional with the form and the font size inside the control should be proportional too. Can anybody please suggest how to do that with code. Unable to solve this by using the anchor and dock property.
My code:
Private Sub Test_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim widthRatio As Single = Me.Size.Width / 300.0F
Dim heightRatio As Single = Me.Size.Height / 300.0F
Dim scale As New SizeF(widthRatio, heightRatio)
Me.Scale(scale)
For Each control As Control In Me.Controls
control.Scale(scale)
control.Font = New Font(control.Font.FontFamily, control.Font.SizeInPoints * widthRatio * heightRatio)
Next
End Sub
here for width and height ratio I have divided 300.0F because my original form is size(300,300), but I am not getting all the controls expand equally... any suggestion is appreciated.
Thanks
pallavi

Check these two lines
Dim widthRatio As Single = Me.Size.Width / 300.0F
Dim heightRatio As Single = Me.Size.Height / 300.0F
in your case Me.Size.Width = 300 and Me.Size.Height = 300 that's why its not working, change width or height then check it

Related

Resize text to fit the text into label - vb.net

I want to automatically change the size of font depends on the length of text that fit into fixed size of label in vb.net
Add this event for Label.Paint
Private Sub Label1_Paint(sender As Object, e As PaintEventArgs) Handles Label1.Paint
Dim ratio = Label1.Width / Windows.Forms.TextRenderer.MeasureText(Label1.Text, Label1.Font).Width
Dim newWidth = Math.Floor(Label1.Font.Size * ratio)
Label1.Font = New Font(Label1.Font.FontFamily, CInt(newWidth))
End Sub
It measures the text and resizes it to fit the Label. This will be called when the label is drawn - when the text is changed, label resized, shown, etc.

My VB form size and executed form size differ? How do i fix this problem?

i created a form in visual studio with a width of 89 but when i execute the form, the form width is more than 89. Can anyone show me how i can resolve this? below is my VB code when the form is loaded.
Its a specific problem.
Private Sub Show_form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim show_form As show_form = Me
Dim y As Integer = Screen.PrimaryScreen.WorkingArea.Height -show_form.Height
Dim x As Integer = Screen.PrimaryScreen.WorkingArea.Width - show_form.Width
show_form.TopMost = True
show_form.Location = New Point(x, y)
End Sub
My form width in design mode:
My form width when loaded.
I expect the form width in design mode to be same as in play mode.

VB: How can I make buttons over a picture box transparent?

in my program I have a picture box with a specific width x and height y. Then the user can enter an elementsize (e.g. 10) and then the programm will create buttons with the width and height of the elementsize over the picture box. The thing is that these buttons need to be transparent so only their border is visible. That means that through the buttons you can see the content of the picture box.
So this is my code for creating these buttons when I click on my start button:
Imports System.Math
Public Class Form1
Dim x As Double
Dim y As Double
Dim elsize As Integer
Dim numberofbuttons As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
x = 400
y = 200
elsize = 20
numberofbuttons = Round(x / elsize) * Round(y / elsize)
Dim i As Integer
Dim j As Integer
For j = 1 To Round(y / elsize)
For i = 1 To Round(x / elsize)
Dim Btn As New Button
Btn.Width = elsize
Btn.Height = elsize
Btn.Location = New Point(elsize * (i - 1), elsize * (j - 1))
Btn.BackColor = Color.Transparent
PictureBox1.Controls.Add(Btn)
Next
Next
End Sub
End Class
I use
Btn.BringToFront()
to put these buttons in front of the picture box and I wanted to use
Btn.BackColor = Drawing.Color.Transparent
to make them transparent but this won't work. Has anybody any ideas how to do this? Also I wanted to put these buttons in the coordinate system of the picture box and not of the form1. I thought that this is possible through
Btn.Parent = PictureBox1
but the buttons always use the coordinate system of the form.
Thanks in advance for your help.
This:
Me.Controls.Add(Btn)
is setting the Parent of the Button to be the form. You don't Add to one parent and assign a different parent explicitly. Do one or the other. In short, get rid of that Add call and you should be good to go. Alternatively, get rid of the Parent assignmnent and Add to the Controls collection of the PictureBox instead. I'd suggest the latter.
Either way, DO NOT display the Button until you have configured it, i.e. set the Parent or call Controls.Add last.
EDIT: I tested this code and it worked as expected:
Dim btn As New Button
With btn
.Size = mySize
.Location = myLocation
.BackColor = Color.Transparent
.FlatStyle = FlatStyle.Flat
PictureBox1.Controls.Add(btn)
End With

How can i quickly resize a form after screen rotation?

I'm writing a full screen program for windows tablets that needs to resize all of its controls to fit the screen, regardless of rotation.
right now i'm using the following:
Private Sub CreateOrder_Resize(sender As Object, e As EventArgs) Handles MyBase.Resize
Dim widthMultiplier As Decimal = (Me.Width * 0.99) / itemsPanel.Width 'this panel is 99% as tall as the form initially, and 80% as tall
Dim heightMultiplier As Decimal = (Me.Height * 0.8) / itemsPanel.Height
resizeEverything(Me.Controls, widthMultiplier, heightMultiplier)
End Sub
Private Sub resizeEverything(ByRef container As Object, ByVal widthMultiplier As Decimal, ByVal heightMultiplier As Decimal)
For Each screenItem In container
screenItem.location = New Point(screenItem.location.x * widthMultiplier, screenItem.location.y * heightMultiplier)
screenItem.size = New Size(screenItem.width * widthMultiplier, screenItem.height * heightMultiplier)
checkIfFont(screenItem, widthMultiplier)
If screenItem.GetType() = (New Panel).GetType Then
resizeEverything(screenItem.controls, widthMultiplier, heightMultiplier)
End If
Next
End Sub
Private Sub checkIfFont(ByRef viewObject As Object, ByVal multiplier As Decimal)
Dim fontCarriers() As Object = {New RichTextBox, New Label, New TextBox, New ListBox, New DateTimePicker, New ComboBox}
For Each controlType In fontCarriers
If viewObject.GetType = controlType.GetType Then
controlType = viewObject
Dim myFont As Font = controlType.font
Dim newFont As Font = New Font(myFont.Name, myFont.Size * multiplier, myFont.Style)
viewObject.font = newFont
Exit For
End If
Next
End Sub
This has proven to be far too resource intensive for some of the tablets I've had to put it on.
I havent used Autoscale because the form needs to 'squish' a bit when the tablet is used in portrait mode, and as far as i'm aware, autoscale maintains aspect ratio.
So how can i do this without grinding through and resizing each control?

Resize form on listview height

I have form with only docked.full listview on it. Listview show computer drives so it's content is changeable on runtime. On listview column headers are allways visible.
I would like my form to resize with listview regarding of filled items.
This is my code:
Dim rc As Rectangle = lvDriveInfo.Items(0).GetBounds(ItemBoundsPortion.Entire)
Me.Height = (rc.Height * lvDriveInfo.Items.Count) +
SystemInformation.CaptionHeight +
SystemInformation.BorderSize.Height
But something misses here or is incorrect.
How to get exact height of listview with headers regarding on items.count and properly set height of form with this value?
ListView metrics are rather convoluted, what you are trying to do is most definitely not easy. The one thing you overlooked is the space required by the column headers. That however is not enough, ListView also requires a bit of extra elbow room at the bottom to avoid displaying the scrollbar.
The most straight-forward code that works on my machine is:
Private Sub ResizeView()
If ListView1.Items.Count = 0 Then Exit Sub
Dim last = ListView1.Items(ListView1.Items.Count - 1)
Me.ClientSize = New Size(Me.ClientSize.Width, _
ListView1.Top + last.Bounds.Bottom + 4)
End Sub
The +4 is the rub, I can't give you a warranty that this will work on every Windows version at every video DPI. It is independent of the item height so that's encouraging. But testing required to be sure. If you can guarantee that there will never be too many items in the list then you can avoid the problem by setting the list view's Scrollable property to False.
Try the following code
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lvDriveInfo.BorderStyle = BorderStyle.None
lvDriveInfo.Dock = DockStyle.Fill
With lvDriveInfo
.View = View.Details
.GridLines = True
.Columns.Add("Drive")
End With
SetFormHeight()
End Sub
Private Sub SetFormHeight()
lvDriveInfo.Items.Clear()
For Each Drive In IO.DriveInfo.GetDrives
lvDriveInfo.Items.Add(Drive.Name)
Next
Dim ListViewHeaderHeight As Integer = lvDriveInfo.Items(0).Bounds.Top
Dim ListViewRowHeight As Integer = lvDriveInfo.Items(0).Bounds.Height
Dim ListViewRowsCount As Integer = lvDriveInfo.Items.Count
Dim NewHeight As Integer = ListViewHeaderHeight + (ListViewRowHeight * ListViewRowsCount)
Me.ClientSize = New Size(Me.ClientSize.Width, NewHeight)
End Sub