VB.NET Uncovered: Big Changes, Page 3
Now this is the fun bit.
So perhaps I shouldn't have dedicated a whole page to these things they call 'Anchor' and 'Dock'... but hey, I'm feeling rebellious. Oh, what a daring nerd I really am.
Anyway, we've all been stuck in Resize Hell. And I've personally written code here at VB-World to help automatically fix the problem. But let's be realistic - it can get incredibly messy.
That's why most VB.NET controls include Anchor and Dock properties. These will help you solve all of those sticky sizing situations.
First off, the Anchor property. This forces one or more of your controls' borders to remain at a constant distance from the forms' border.
Let's take a peek at this:
- Create a new Windows Application
Imagine you're creating a Web application and want a TextBox at the bottom of your form. You want it to stay there, stretching out with the form.
- Add a Text Box to your form, so it looks like this:
- Click on TextBox1 and view its Anchor property (under Layout)
By default, the control 'anchors' to TopLeft. In other words, it remains at a constant distance from the top and left sides of the form.
Let's change that:
- Change the Anchor property so Top is unselected and Left, Right and Bottom are selected
The property text should change to 'BottomLeftRight'. This means your control will remain at a constant distance from the bottom, from the left and from the right.
- Press F5 to run your application
- Resize the form and bask in your glory
The Dock property is also pretty important, though not as revolutionary. It's similar to the Align property certain VB6 controls had. It forces the particular control to 'stick' to a side of the form - or with the 'Fill' option, cover the whole form.
- Add a new Text Box to Form1
- Experiment with the Dock property
And now, as Bush told his followers, for something completely different... <ahem>### Visual Inheritance ###
Death, eh? Tsk! What a bummer.
Still, life's overrated I say.
But sometimes popping the ol' clogs can be a good thing, so long as (a) it ain't you, (b) the person in question leaves you a tonne of money.
Yes, oh yes, it's yet another smooth link to a technical topic. Oh yes. Ohhh yes. Bet you didn't even notice that one coming.
Now, inheritance in the above instance is where you get something, where something is added to yourself. And you're usually talking about a wad of cash.
In VB.NET, you also get inheritance. But that wad of cash is unfortunately replaced by the likes of code and Combo Boxes.
Later in this series, we'll be dealing with real code inheritance - but right now, let's look at Visual Inheritance. This is where one form 'inherits' its layout (and maybe even its code) from another'master' form.
Let's pretend you have a common set of features on more than one form. Perhaps I'm talking about a 'OK' button, a Web site hyperlink, a form backdrop, data entry TextBox controls, the layout of that Wizard -whatever common form elements you have, you can share them and their related logic with visual inheritance.
- Create a new Windows Application
- Add a few common elements to Form1
For my 'common elements', I've added a LinkLabel that 'anchors' to BottomRight, an 'OK' Button that anchors to BottomLeft. Oh yes - and a simple Label bearing the name of my application. And it all looks alittle like this:
Now before you can 'inherit' a form, it needs to be 'built' (sort of a mini-compile). Don't look at me, but you have to do it. Let's go:
- From the Build menu, select Build
Next up, let's inherit that form:
- Click Project, Add Inherited Form
- Ensure 'Inherited Form' is selected, then click OK
- In the list that appears, select your Form1 and click OK
- If Form2 does not automatically appear, double-click on it in the Solution Explorer
- Add a few TextBox controls to Form2
Let's see Form2 in action. To do this, we'll have to change the Project Properties:
- Right-click on your Windows Application in the Solution Explorer
- Select Properties from the popup menu
Your screen should look like this - if not, you've selected the wrong item in the Solution Explorer:
Have a quick browse around. This is where you can change the Assembly name (what we used to call the 'Project' name), the version number, application icon - and hey-hey, the Startup Object. That's our stop.
- Change the Startup Object to Form2 and click OK
Now let's test out your inherited form:
- Hit F5 to test your application
Notice how it all works together? Try resizing Form2 - if you've used the Anchor properties on the inherited controls, they'll move along with the new form.
And if you'd perhaps put code behind Form1, it'd be automatically inherited. Hmm, love that centralisation. In fact, you could even allow Form2 to 'override' certain pieces of functionality - for example, the code behind the 'OK' Button could be determined by Form2, even though the visuals come from Form1.
If you're eager to learn more, check out the help.
Page 3 of 7