1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: VB: Adding On_Click event at runtime

Discussion in 'Software Development' started by cc64, Dec 21, 2010.

Thread Status:
Not open for further replies.
Advertisement
  1. cc64

    cc64 Thread Starter

    Joined:
    Dec 21, 2007
    Messages:
    153
    Hello everyone. I'm going to try and explain this as best as possible. Please feel free to ask for more info if I don't explain it well enough.

    I have a desktop that has grown to have too many shortcuts on the desktop to fit on the screen. We use everyone of them regularly so we still wanted to keep them all but obviously, needed a better way. So I made up a quick menu bar with some drop down menus that organizes the shortcuts into categories that, when clicked on, will open a new browser window, and take you to the web link.

    The addresses were hard coded into the program and so are not very flexible. So I came up with a text file that has each links parent drop down item, its name, its address, and a short blurb about what it is that will pop up when you hover over it (the Tool Tip Text property).

    My problem is - since all of the menu items are added at runtime - I don't know how to add the "System.Diagnostics.Process.Start("http://www.google.com")" code to tell the program to open the browser when it is clicked on.

    Problem 2 is that I don't know how to edit the properties in runtime either.

    Thanks for any help and happy holidays to all!
    Curtis
     
  2. Ent

    Ent Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,467
    First Name:
    Josiah
    Interesting problem.
    I haven't done this before, so I'll see if I can figure it out as well.
    In terms of missing info, the version of VB that you're using would be useful.
    Also the format of the text file(s), and if possible the code you have already in a zip file.
     
  3. Ent

    Ent Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,467
    First Name:
    Josiah
    Here is a code to do what you need. It only needs a menustrip and the form, the menustrip having the default name of menustrip1. It opens the text file WebAddresses.txt in your documents, and uses those entries that to populate the menu strip.

    Code:
    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim Code As Integer
            Dim WebAddress As String
            Dim ToolTip As String
    
            Dim TopMenuitem As New ToolStripMenuItem
            Dim branchMenuitem As New ToolStripMenuItem
    
            FileOpen(1, My.Computer.FileSystem.SpecialDirectories.MyDocuments + "\WebAddresses.txt", OpenMode.Input)
            Do
                Input(1, Code)
                Input(1, WebAddress)
                Input(1, ToolTip)
    
                If Code Then
                    branchMenuitem = New ToolStripMenuItem With {.Text = WebAddress, .ToolTipText = ToolTip}
                    AddHandler branchMenuitem.Click, AddressOf OpenAddress
                    TopMenuitem.DropDownItems.Add(branchMenuitem)
                Else
                    TopMenuitem = New ToolStripMenuItem With {.Text = WebAddress, .ToolTipText = ToolTip}
                    MenuStrip1.Items.Add(TopMenuitem)
                End If
            Loop Until EOF(1)
            FileClose(1)
        End Sub
    
        Private Sub OpenAddress(ByVal sender As System.Object, ByVal e As System.EventArgs)
            System.Diagnostics.Process.Start(sender.text)
        End Sub
    
    
    End Class
    
    The format of the text file is as follows:
    Code, Web Address, ToolTip
    0, "Search Engines", "Sites for Searching the Web"
    1, "www.google.com", "10^1000000"
    1, "www.bing.com", "Because it's not google."
    0, "Tech Forums", "Sites for answering computer problems"
    1, "forums.techguy.org", "Tech Support Guy"
    1, "www.microsoft.com", "The old behemoth's site"
    etc

    Code is either 0 or 1. It is 1 if it is a web address itself, and 0 if it's a category. The first one has to be a category, and the program keeps filling the addresses in that category until it reaches another category.



    Thanks for forcing me to learn.
     
  4. cc64

    cc64 Thread Starter

    Joined:
    Dec 21, 2007
    Messages:
    153
    Wow, that was incredibly fast. And surprisingly simple. Do you mind asking what resources you used to find this?

    Thanks so much!
     
  5. cc64

    cc64 Thread Starter

    Joined:
    Dec 21, 2007
    Messages:
    153
    I'm having a slight issue with the OpenAddress method. When I use sender.text it returns an "File not accessible error." So I tried just using the WebAddress variable in there and now, every menu strip item that is added will take me to the last adress added via the text file.

    Suggestions?
     
  6. Ent

    Ent Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,467
    First Name:
    Josiah
    Main Resource was google, search for "VB.net dynamic handle". I think the page that I found most helpful in the end was this one.

    The webaddress variable won't work, it is as you've discovered just a temporary variable to assign the names to the drop down entries. It will be overwritten with the next entry as soon as the previous entry is written to the menustrip. Good practice would actually have been to have it within the subroutine so that it gets disposed of afterwards, but I forgot.

    As to the error you're getting, I didn't get it for any of my initial tests which all happened to be www addresses. Apparently Windows guessed what I wanted from that W3 prefix, because when I tested with the forums.techguy entry I got the same error as you did. Windows must have assumed that I wanted to open an org file named forums.techguy on my own machine, which clearly doesn't exist.

    You could merely incorporate the http:// protocol section into the addresses in the text file. Or if these are all web addresses the easy fix would be to manually insert ""http:\\" +" in front of sender.text in that subroutine.

    A better answer would be to use a fourth property to hold the true path. I would suggest the tag property. The title could then be more user-friendly (google instead of www.google.com). The form could also be used to run programs if you wanted, by specifying their path instead of a web address.

    Here is my new code
    Code:
    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim Code As Integer
            Dim WebAddress As String = ""
            Dim ToolTip As String = ""
            Dim TrueAddress As String = ""
    
            Dim TopMenuitem As New ToolStripMenuItem
            Dim branchMenuitem As New ToolStripMenuItem
    
            FileOpen(1, My.Computer.FileSystem.SpecialDirectories.MyDocuments + "\WebAddresses.txt", OpenMode.Input)
            Do
                Input(1, Code)
                Input(1, WebAddress)
                Input(1, ToolTip)
                Input(1, TrueAddress)
    
                If Code Then
                    branchMenuitem = New ToolStripMenuItem With {.Text = WebAddress, .ToolTipText = ToolTip, .Tag = TrueAddress}
                    AddHandler branchMenuitem.Click, AddressOf OpenAddress
                    TopMenuitem.DropDownItems.Add(branchMenuitem)
                Else
                    TopMenuitem = New ToolStripMenuItem With {.Text = WebAddress, .ToolTipText = ToolTip}
                    MenuStrip1.Items.Add(TopMenuitem)
                End If
            Loop Until EOF(1)
            FileClose(1)
        End Sub
    
        Private Sub OpenAddress(ByVal sender As System.Object, ByVal e As System.EventArgs)
            System.Diagnostics.Process.Start(sender.tag)
        End Sub
    
    
    End Class
    
    And here is the contents of the text file as an example.
     
  7. cc64

    cc64 Thread Starter

    Joined:
    Dec 21, 2007
    Messages:
    153
    I just realized what a simple error I made. I modified the Text property to be the title of the webpage and was trying to open, say "Google." No wonder it couldn't find the file. This was the perfect solution. And I learned a much simpler way of dealing with files other than streamers. Thanks for all your help.

    Curtis
     
  8. Ent

    Ent Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,467
    First Name:
    Josiah
    You're very welcome. And thank you for giving me the problem; the experience of having done it is probably as valuable to me as the solution is to you.
     
  9. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/969776

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice