Visual Basic .NET Nightmare or Guide to upgrading a Guru.
At this point I think you have probably gotten a handle on drag and drop form controls, which is very similar to VB6. It's also Nice to see some transient ActiveX components which are now part of form controls under dot net. And some of them seem much improved. For instance the tab control, which now acts as a container for each tab, no more frame additives needed to create a usable instance of this.
As we move forward we want to see how control's become objects of their own under dot net in ways we barely saw under VB6.
Collection is our word for this lesson. And we are going to start out with a control that functions differently under dot net than as seen in such fun programs as Access and Word. You can very easily and readily create a hyperlink in a word document. You can also hyperlink a few different controls under access. A button, a label, or an image control. So what's the first thing that one thinks of when one sees a Link Label as part of dot net's form's controls? One would think it was a quick and easy way of putting a hyperlink into one's project.
This is of' course is not the case, at least as far as having the control handle it all, by first acting as a link and then subsequently taking you to it's destination. In fact clicking the link label doesn't do much more than visibly act like a Hyperlink. Probably about the second time I opened VB .NET I went ahead and tried using it in this context. I was sadly disappointed and of' course generally lost in the new kingdom.
Let's start with a project name it Hyperlink if you like. On our first form we are going to draw one of these lovely linkables on it and then run it and see exactly what it does by itself.
Hmm well it changes color when mouse button is down, but it comes right back to status blue when released. So does this do anything more than a command button does? Yes and no. Our link label has some properties that assist you in treating it like a hyperlink. Such as changing the color to visited. And it has an active color. Whoo Weeee. All this poor control needs is an .AutoEnabled property and a .DefaultLink to make it go Wow. With .AutoEnabled set to false it could continue to act as it does now, lifeless.
So what can we do with this that makes it worthy of your dot net app? Well this is where our lesson word comes into play. For deep within the bowels of this control lies a collection of Links. To get to them you have to add them yourself. Funny weird thing is you can add as many as you want and have your whole form filled up with a single label. Hmmm maybe not.
Okay next let's try one of those new fandangle things you can do with most controls that draw visible on your form. Let's dock it. Find dock on the properties and click the bottom bar just above none. It should now align with the bottom of the form and stretch out, now your only concern here is it's height, the rest takes care of itself even when the form is resized.
Now maybe we can see some possibilities with this. Liken to a menu style contents link group for a web site, which shows up on every page at the bottom. But we're not on a page so we are going to have to be creative in how we make this thing go and still be worthy of our app. First we'll start with a basic link then go from there. So we want to change some of the text displayed. Let's type home. Okay that's pretty.
Now what? Well we want a link for our word home. Let's open the forms designer region and add a line of code under InitializeComponent(). Remember that everything in the InitializeComponent() sub is used by the forms designer and generated by it. Making changes in it's sub may be overwritten the next time you make changes to your form. By adding code after the sub is called we will be setting it up to not be used by the designer. Let's add this line.
|Me.LinkLabel1.Links.Add(0, 4, "www.msn.com")|
We already know that the link label contains an object collection called links, so there for each link you add is an object in of itself. At the moment I think it's excessive, but I may change my mind. As you might guess list boxes and combo boxes are the same way. We have been using collections under VB6 all along, they were a bit more sparse. For instance List Image control contained a collection.
Our numbers here indicate starting position within the text string and the number of characters belonging to the link.
Now that we have our link in there we want to find out how to use it. Let's start by generating a link label LinkClicked event in our code. Select LinkLabel1 in the left drop down and then find LinkClicked event in the right drop down. Now let's add one more line of code and we should now have this.
sender As Object,
ByVal e As
A quick check of our e object yields the link object. Now we are going to call it's data (www.msn.com) and use that as a param for this groovy call to System.Diagnostics.Process.Start.
What the heck is this thing? SDPS, hmmm maybe, maybe not. Don't let the name space name fool you, this is a valid runtime tool for your dot net app. And it's a great thing to have, because it waylays the need for many of the most common API requests under VB6. Controlling other apps and system processes from within your code. Woot! Okay run it and try your link. Did you get MSN? If you didn't it's time to debug and review the steps you took up to this point. If you got it it's time to move on.
Now let's add some more text to our link label. I suggest doing this inside of the code window in the designer region, but you can just as well add it in the properties window. now we should have this.
|Me.LinkLabel1.Text = "Home Search VBnet Mikeyville MSDN"|
VBnet is not to be confused with VB .NET. VBnet has been around for a long time. Now we need to add links for each one. A quick look at the form shows them all linked, but that is because our code doesn't take place inside of the InitializeComponent() sub. Now the numbers are of' course the range of letters in the text we want to signify our individual links. 0,4 is our Home text in the link label. 0 indicates the starting position and 4 is the number of letters we want linked. So now we want to position our other words by putting the right number in. I have 5 spaces between each. With just a little bit of math we now have the designated code in our Sub New.
'This call is required by the Windows Form Designer.
Me.LinkLabel1.Links.Add(0, 4, "www.msn.com")
Me.LinkLabel1.Links.Add(9, 6, "www.google.com")
Me.LinkLabel1.Links.Add(20, 5, "www.mvps.org/vbnet/")
Me.LinkLabel1.Links.Add(30, 10, "www.microsoft.com")
Me.LinkLabel1.Links.Add(45, 4, "www.msdn.microsoft.com")
'Add any initialization after the InitializeComponent() callEnd Sub
|LinkLabel1.Links(LinkLabel1.Links.IndexOf(e.Link)).Visited = True|
We know that what's inside of our first set of Parens is signifying the index of the Links collection. We are getting the index from the method belonging to the Link Object called .IndexOf. This returns the index of the link object passed to it from our passed e.link object. After that is our .Visited property which we are then setting to true, this magically makes the link's color change to the designated visited color.
Maybe this could be useful in the way someone works in your app. Perhaps working with a database and entering each item. What then takes place is the end user clicks a link to signify that a certain portion of their work is done and helps them keep track of what they need to do next or each link could point to helpful information for each task they are working on. As with any app, the possibilities are endless.
Not to be under whelmed by all the link label stuff is the power of the System.Diagnostics.Process Object. In fact let's take a closer look at this thing.
Let's start with a different way of getting this into our app. Bring up your form view. Then from the tools click the components section. Now let's drag and drop a process into our form's area. This is not a visible control so it goes neatly below your form and doesn't get in the way of you designing the look of your window.
Now what do I do with it? Well you want to set it up to handle a process for your app. Go into it's properties and click the plus next to .StartInfo. In the .FileName property type in notepad. then let's name our process MyProcess. Next let's add a button and name it btnNote.
Generate the click event for your button and add this line in for code.
sender As System.Object, ByVal
e As System.EventArgs) Handles
Could this be any simpler? You can launch another application and already be hooked into it with your own drag and drop process object. This is relevant to VB6's API calls to which you can launch apps and retain their process ID or Handle. From within our app we can either kill the process or request it to close (if it's a windowed app.) Or we can simply wait for it to close and then do something further if needs be.
Also we are not restricted to calling just apps, we can call files associated with apps and send parameters to them when we open them. Let's say we have a MyDoc.Doc in our My Documents folder. With the assumption that Word is part of your Kingdom, the we can add the .FileName to our process's .StartInfo. Make sure .UseShellExecute=True. This is probably familiar to you if you have used the same API call to open another program. next time you click the Note button when you start your process the document opens inside of wordomatical. You can alternatively set up your Hyperlink's collection to access a series of related documents to whatever it is your app is doing.
Also we want to note that the .StartInfo property is a collection of properties this is why it's expanded in the properties window.
We now have some good info on how to get around our system via links and process control. This easily extends our app's power to using more than just files in directories, with both combined we are slowly mastering the techniques we need to have a serious application.