VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Page 1 of 1
  • You cannot start a new topic
  • Reply Reply

Adding Click function to menu items generated at runtime Rate Topic: -----

#1 Orann  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-May 07


Dream Kudos: 0

Share |

Adding Click function to menu items generated at runtime

Post icon  Posted 17 September 2007 - 09:02 PM

Greetings all,
I've just recently run into a problem when programming a basic web-browser, when trying to make a bookmarks or favorites function.

To do this, the plan was to follow this structure:

1: A user visits the page they wish to bookmark and clicks "Bookmark This Page"

2: The Program takes the URL of the page and the document title (So it looks neat) and stores it in a 2D array.

3: The program then calls a function to refresh the favorites menu, which will clear it, then populate itself with the contents of the array.

This all works correctly, using the code below:

  Private Sub BookmarkThisPageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BookmarkThisPageToolStripMenuItem.Click
		Dim Looprunner As Integer
		For Looprunner = 1 To UBound(BookMarksArray, 1)
			If BookMarksArray(Looprunner, 1) = tbURL.Text Then
				GoTo Duplicate
			Else
			End If
		Next

Add:	BookmarksNum = BookmarksNum + 1
		ReDim Preserve BookMarksArray(1, (BookmarksNum))
		BookMarksArray(0, BookmarksNum) = BookmarksNum
		BookMarksArray(1, (BookmarksNum)) = tbURL.Text
		GoTo Complete
		'MsgBox(MenuBar.Items.Count)

		'Get the next available index in the menu
		' Load(mnuBookmark(mnuBookmark.ubound + 1))
		'Write the Caption of the new item as the URL
		' mnuBookmark(mnuBookmark.ubound).Caption = Bookmark

Duplicate: MsgBox("There is Already a Bookmark Of this page")
Complete: Call UpdateFavourites()
	End Sub


 Sub UpdateFavourites()
		On Error GoTo 0

		For Loopcount = 1 To BookMarksArray.GetUpperBound(0)

			'MnuFavourites.DropDownItems.Add(BookMarksArray(Loopcount, 1))
			MnuFavourites.DropDownItems.Add(WB1.DocumentTitle)

		Next Loopcount

	End Sub



(Sorry about the messy format and Rem'd out lines, it's a work in progress).

Anywho, my problem is that, as you might imagine, clicking the items does nothing. As the menu items are generated at runtime, I cant seem to work out how to bind a click function to them (to navigate to their correct URL).

Thanks for the consideration.
Was This Post Helpful? 0
  • +
  • -


#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • Icon

Reputation: 1336
  • View blog
  • Posts: 8,175
  • Joined: 18-April 07


Dream Kudos: 0

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

Re: Adding Click function to menu items generated at runtime

Posted 17 September 2007 - 09:57 PM

Took me a second to figure out if you were even using VB 6 or .NET because it looks like you are mixing here. I am just going to assume both here so you can choose whichever you are using.

Visual basic 6

1. Create a control array with your menus. The code below shows you how to do this. You first create the menu item in menu and call it mnuItem and give it an index of zero.

Dim x As Integer
For x = 1 To 5
		Dim menu As menu
		Load mnuItem(x)
		mnuItem(x).Caption = "Item " & x
		mnuItem(x).Visible = True
Next



This code will then add several menu items (you could create the subitems and such). Then once you have that, double click on the original menu item to create a mnuItem_Click event. You will notice the parameter sent to this procedure is the "index" of the item in the control array that triggers the event. You then write the code there to first detect the index and execute the appropriate code based on that index.


VB.NET

2. In VB.NET you can simply add a handler to the item after it is created. You just need the "AddHandler" statement to attach the function that will be called to "handle" the event from your dynamic menu item. You supply AddHandler the address of the function. It is pretty simple really. Below is a quick blog entry that discusses it and shows you the code to do it all.

Handles, AddHandler and RemoveHandler

Enjoy!
Was This Post Helpful? 0
  • +
  • -

#3 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • Icon

Reputation: 137
  • View blog
  • Posts: 4,622
  • Joined: 26-November 04


Dream Kudos: 2825

Expert In: J2ME, 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

Re: Adding Click function to menu items generated at runtime

Posted 18 September 2007 - 01:30 AM

Quote

Private Sub BookmarkThisPageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BookmarkThisPageToolStripMenuItem.Click

He's using .NET, isn't he?
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • iHater.Init(this);
  • Icon

Reputation: 1364
  • View blog
  • Posts: 19,696
  • Joined: 26-July 07


Dream Kudos: 12925

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

Re: Adding Click function to menu items generated at runtime

Posted 18 September 2007 - 03:16 AM

Good answer Martyr2!

@born2c0de - Yes thats most definitely VB.Net, so it needs to be moved, I just have no power in this forum
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • iHater.Init(this);
  • Icon

Reputation: 1364
  • View blog
  • Posts: 19,696
  • Joined: 26-July 07


Dream Kudos: 12925

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

Re: Adding Click function to menu items generated at runtime

Posted 18 September 2007 - 09:38 PM

Moved to VB.Net :)
Was This Post Helpful? 0
  • +
  • -

#6 m2s87  Icon User is offline

  • D.I.C Regular
  • Icon

Reputation: 18
  • View blog
  • Posts: 390
  • Joined: 28-November 06


Dream Kudos: 1225

Re: Adding Click function to menu items generated at runtime

Posted 19 September 2007 - 12:16 AM

Yea. Basic syntax for it is
AddHandler <object>.<event>, AddressOf <procedure>

By itself, it just says if that object<object> has an event<event>, then execute procedure<procedure>. And the procedure has to have the same parameters as that objects event would have. Note that one parameter will be the <object> itself, it is usually called sender, and it is passed by type of object and not the control itself.

You can hard type in everything you need to do with the object, but to get Intellisense working you need to convert that object type control to it's original form before using it (If you have a Option Strict on, this is a MUST). But before that you should check if you have the right control. Easiest way to do that would be to check if you have right control, and pass the converted object to a function that as the type of control you need(an not object). [look previous posts]

.Net also introduced VB lists and arraylists. The major advantage over array is that, that you can add/delete elements in it as elements in it can and is spread over memory. [look wikipedia]
I have written a example in my blog about using control lists with events an it can be found here (you might be interested in the first example)

If you to check if you have an element in list that already has the same website as it's textbox you can do that like:
dim add_it as boolean = true
dim nv as string= <new value to add>
for each element as <object type> in <list name>
	select case element.text
		case nv, "www." & nv, "http://" & nv, "https://" & nv, _
"http://www." & nv,  "https://www." & nv 'etc
		case else
			add_it=false
			exit for
	end select
next


But if you have an extra array/list to store the data of the links you can loop it instead. If you have stored the links with full path, you can sort the list and if you find a duplicate, by searching if element(i)=element(i+1), then just remove it(and do not add a control).

Simple as that :D
Was This Post Helpful? 0
  • +
  • -

#7 Orann  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-May 07


Dream Kudos: 0

Re: Adding Click function to menu items generated at runtime

Posted 13 October 2007 - 07:24 PM

Thanks all for the support. Truth be told, I posted here and then forgot I had done (which is why I haven't been replying). Sorry about that.
I still have the problem, and thank you all for the posts. Adding a handler to this seems to be the best way to go.

There are problems though:

When I add the AddHandler <object>.<event>, AddressOf <procedure> line, It doesn't recognize anything past the comma, as if "Adressof' was foreign.

Even then, if it was possible, I'm not sure what should go into the object field. There is the favourites menu in the proposed fields, but not anything that might refer to an item inside of that. On top of that, how do I make it do it for every item?

And finally, Even if it was possible, and I had it running a procedure on each click, how would I get it to return a specific value based on which item it is? I.E, how would I get it to tell me that I clicked Bookmark # 4, so I could then get the specific information out of an array.

Thanks for your help so far everyone. I'm beginning to think that this feature was a bit ambitious for my current programming knowledge though.

This post has been edited by Orann: 13 October 2007 - 07:28 PM

Was This Post Helpful? 0
  • +
  • -

#8 PsychoCoder  Icon User is offline

  • iHater.Init(this);
  • Icon

Reputation: 1364
  • View blog
  • Posts: 19,696
  • Joined: 26-July 07


Dream Kudos: 12925

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

Re: Adding Click function to menu items generated at runtime

Posted 13 October 2007 - 10:01 PM

Sorry Orann but I had to clean that code up. If you're going to use VB.Net, then use VB.Net, don't mix the 2. Old VB6 methods are only there for backwards compatibility and should be avoided like the plague. Even in VB6 using GoTo was considered bad programming, so they're really frowned upon in the .Net languages. Here is the cleaned up code

Private Sub BookmarkThisPageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BookmarkThisPageToolStripMenuItem.Click
		Dim Looprunner As Integer
		For Looprunner = 1 To BookMarksArray.GetUpperBound(0)
			If BookMarksArray(Looprunner, 1) = tbURL.Text Then
				MessageBox.Show("There is already a bookmark of this page", "Duplicate Entry", MessageBoxButtons.OK)
			Else
				BookmarksNum += 1
			End If
		Next

		'Get the next available index in the menu
		' Load(mnuBookmark(mnuBookmark.ubound + 1))
		'Write the Caption of the new item as the URL
		' mnuBookmark(mnuBookmark.ubound).Caption = Bookmark

		ReDim Preserve BookMarksArray(1, BookmarksNum)
		BookMarksArray(0, BookmarksNum) = BookmarksNum
		BookMarksArray(1, BookmarksNum) = tbURL.Text
		Call UpdateFavourites()
	End Sub


	Private Sub UpdateFavourites()
		For Loopcount = 0 To BookMarksArray.GetUpperBound(0) - 1
			'MnuFavourites.DropDownItems.Add(BookMarksArray(Loopcount, 1))
			AddHandler MnuFavorites.Click, AddressOf MnuFavororites_Click
			MnuFavourites.DropDownItems.Add(WB1.DocumentTitle)
		Next Loopcount
	End Sub



If you will look at the updated UpdateFavorites Sub you will see the line AddHandler MnuFavorites.Click, AddressOf MnuFavororites_Click, that is the line that adds a click event handler to your dynamically created menu items.

AddHandler is the statement used to add an event handler to an item, in this case we want to add a ClickEvent handler to your menu items, so we add the MnuFavorites_Click handler to the newly added items.

Now if someone removes an item from their favorites, you would use RemoveHandler to remove the event handler from that item before removing it.

Hope that helps a little :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1
  • You cannot start a new topic
  • Reply Reply


Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users