Menu Structure to textbox

bk292

New member
Joined
Nov 2, 2006
Messages
3
Location
Canada
Programming Experience
Beginner
I'm new to Visual studio.Net, I'm following the book "Mastering Visual Basic .Net" by Evangelos Petroutsos. In the following code of MapMenu Project
{Its a form with File Menu, Edit Menu, Format Menu etc., A button (bttnMap_Menu) and a text box. The click of button prints all the Menu's structure in output box. I tried few things, but couldn't make it print the structure in Text box}

Can some body suggest me what is the right way to do it?
VB.NET:
[SIZE=2][COLOR=#0000ff]Protected[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] bttnMapMenu_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] bttnMapMenu.Click[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] itm [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] MenuItem[/SIZE]
[SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] itm [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Menu.MenuItems[/SIZE]
[SIZE=2]Console.WriteLine(itm.Text)[/SIZE]
[SIZE=2]PrintSubMenu(itm)[/SIZE]
[SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] PrintSubMenu([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] MItem [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] MenuItem)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] itm [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] MenuItem()[/SIZE]
[SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] itm [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] MItem.MenuItems[/SIZE]
[SIZE=2]Console.WriteLine(itm.Text)[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] itm.MenuItems.Count > 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2] PrintSubMenu(itm)[/SIZE]
[SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
Thanx in advance
 
Last edited by a moderator:
If I understand you right your just trying to display it in a textbox right. MultiLine right?

This this is all you need... Set the textbox to multiline.

VB.NET:
[SIZE=2][COLOR=#0000ff][COLOR=black]For [/COLOR][/COLOR][/SIZE][COLOR=black][SIZE=2]Each[/SIZE][SIZE=2] itm [/SIZE][SIZE=2]In [/SIZE][SIZE=2]Me[/SIZE][/COLOR][SIZE=2][COLOR=black].Menu.MenuItems[/COLOR][/SIZE]
[SIZE=2][COLOR=black]TextBox1.Text += (itm.Text & vbCrLf)[/COLOR][/SIZE]
[SIZE=2][COLOR=black]Next[/COLOR][/SIZE]

Also, this line is calling it's own method, you'll get stuck in a loop! Just let the for loop do it's work.

VB.NET:
[SIZE=2][COLOR=black][SIZE=2]Sub[/SIZE][SIZE=2] PrintSubMenu([/SIZE][SIZE=2]ByVal[/SIZE][SIZE=2] MItem [/SIZE][SIZE=2]As[/SIZE][SIZE=2] MenuItem)[/SIZE][/COLOR]
[COLOR=black][SIZE=2]Dim[/SIZE][SIZE=2] itm [/SIZE][SIZE=2]As[/SIZE][SIZE=2]New[/SIZE][SIZE=2] MenuItem()[/SIZE][/COLOR]
[COLOR=black][SIZE=2]For[/SIZE][SIZE=2]Each[/SIZE][SIZE=2] itm [/SIZE][SIZE=2]In[/SIZE][SIZE=2] MItem.MenuItems[/SIZE][/COLOR]
[COLOR=black][SIZE=2]Console.WriteLine(itm.Text)[/SIZE][/COLOR]
[COLOR=black][B][I][SIZE=2]If[/SIZE][SIZE=2] itm.MenuItems.Count > 0 [/SIZE][SIZE=2]Then[/SIZE][/I][/B][SIZE=2][B][I] PrintSubMenu(itm)[/I][/B][/SIZE][/COLOR]
[COLOR=black][SIZE=2]Next[/SIZE][/COLOR]
[COLOR=black][SIZE=2]End [/SIZE][SIZE=2]Sub[/SIZE][/COLOR][/SIZE]
 
Also, this line is calling it's own method, you'll get stuck in a loop!
That is called a recursive method, and is correct. Since each child menu item can hold its own collection of sub menu items you call this same method with the sub collection as parameter and 'let it do its job' with that too until there are no more sub-sub-sub-sub-sub-sub-sub... menu items. It will not get stuck, because eventually it hits leaf nodes without child menus.
 
menu Items to Textbox

Thanx a lot Guys. That works right now. The text box is already Multiline. I was trying

For Each itm In Me.Menu.MenuItems
TextBox1.Text = (itm.Text)
Next

As I was not adding "& VbCrLf" (which is Visual Basic Carriage Return Line Feed) and it was only printing one item.


But this is working now.
For Each itm In Me.Menu.MenuItems
TextBox1.Text += (itm.Text & vbCrLf)
Next

Thanx Again for the help.
Bharti​
 

Latest posts

Back
Top