Novices to Visual Basic may appreciate that you can visually build your application via the Visual Studio (or Visual Basic Express) interface, but what about when you create applications where you want the controls to dynamically resize to fit the size of the window. First, we need to logically think how this will be done:
- First, we need to set the initial dynamic size as the application is loaded.
- Then, handle an event where when the window is resized we further dynamically resize the controls to fit the new resized size.
And that’s all there is to it! Let’s get started
Here’s an example application I’m creating – a simple Web browser. I’ve added two controls on the form, and changed the Location property for each control to be in the top left corner in order for the resizing to work correctly. The first is a textbox and the second being a WebBrowser control.
For my application I’m going to set an event against the TextBox to see when the Enter key has been pressed. To do this I go to the Events section of the Properties window (the lightning bolt) and scroll until I find KeyPress and add the code that detects when the Enter key has been pressed:
Public Class Form1
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If AscW(e.KeyChar) = 13 Then
I’ll explain what’s happening here. The AscW function returns an Integer representation of a character – such as the Enter key, A, Q, etc – each key on a keyboard has such an integer value – called an ASCII value, technically. We simply compare the key entered to the ASCII value to see if the Enter key was pressed. However, in order to compare it to an integer value we first need to get an integer representation of the key entered, as if we just did
e.KeyChar = 13 it would be comparing a char to an integer – and that’s going to result in an error. The
e variable is declared through the subroutines argument (if you look carefully) against the
KeyPressEventArgs class – and this has the KeyChar property which holds the character entered.
We then set the
Navigate subroutine to TextBox1.Text, and of course, the Navigate subroutine is responsible for navigating the WebBrowsr control to a new website. There’s also the
Url property that does the exact same thing but with a slightly different implementation since it is set as a “data type” of System.Uri while
Navigate is of data type String, hence we use that for simplicity.
Now let’s get going to resize the controls as the form (application/window) is first loaded. So go back to the form and double click the title bar of the form window to get the Load event code up.
In the event subroutine, we’ll enter the following:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowser1.Size = Me.ClientSize
WebBrowser1.Height = WebBrowser1.Height - TextBox1.Height
TextBox1.Width = Me.ClientSize.Width
So what’s happening here? The Size property of the WebBrowser class (“WebBrowser1″) allows us to dynamically set the size of both the width and height at the same time, and then we assign it to Me.ClientSize that is the size of the client area or what I tend to call, the content area.
However, it doesn’t take into account any other controls on the form, so we need to subtract the height of the TextBox from the WebBrowser control.
Finally, we change the width of the TextBox too.
Now, we need to set the dynamic size as the form is resized, as right now when we resize the form the controls stay in the same static position. All we do is get the Resize event in the Events section of the Properties window against the Form, and add the same code we just added to it – and we’re done!
Like this post ?
Share on your Social Networking Profile ( Facebook, Twitter & Google+ ) and get a flat 10% Recurring discount on our VPS Hosting and Dedicated Servers.
Email us the shared link at : firstname.lastname@example.org or speak to our live chat operator now, by clicking on the “Live Chat” Scroller on the left-hand side of this page and we will provide you with the discount Coupon right away!