Hi all,
I'm brand new to daniweb and fairly new to vb.net and am trying to get my head wrapped around looping through an XML file to create toolstrip buttons in VS 2013 vb.net.
i have searched all over google and youtube and now im totally confused.

I'd like to ask if anyone is willing to create some sample code that will create a few toolstrip buttons within a toolstrip/toolstrip container.

lets assume the windows form already has the toolstrip container and the toolstrip (with default names) and all we need to do is loop through the XML and create the buttons.

the end result would be a form with 4-5 buttons named something like button1, button2, button3 etc...

im thinking if i can see the XML structure and how it relates to the loop that creates the buttons, I will finally pick up on whatever im missing.

Id like to be able to just paste your sample code into a new project and then study it and figure out to how incorporate it into my project.
thank you in advance for taking hte time to help a newb.
Chris.

How about posting the xml and an example of what you want the tool strip to look like?

im not sure how the XML should be written as im new to it also but ithink something like this

<Buttons>
 <button>button name = button1</button>
 <button>button name = button2</button>
 <button>button name = button3</button>
 <button>button name = button4</button>
 </Buttons>

the tool strip would look like this
Capture.JPG

is that what you were looking for?
thanks,
chris

You can't get that toolstrip from the given pseudo-xml. You might as well have posted

<Buttons>
    <tag>Some Buttons</tag>
</Buttons>

I figured out how to do it and thought I would post it here for others who are trying to figure out the same thing.

Here is the XML file

<?xml version="1.0" encoding="utf-8" ?>

<buttons>

<button>
<buttonname>1</buttonname>
</button>

<button>
<buttonname>2</buttonname>
</button>

<button>
<buttonname>3</buttonname>
</button>

<button>
<buttonname>4</buttonname>
</button>

<button>
<buttonname>5</buttonname>
</button>

</buttons>

and here is the code for the toolstrip

Imports System.Xml
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim xmlDoc As New XmlDocument()
        xmlDoc.Load("*[your path and file name go here]*")
        Dim nodes As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("/buttons/button")
        Dim ButtonName As String = "", bName As String

        For Each node As XmlNode In nodes
            bName = node.SelectSingleNode("buttonname").InnerText

            Dim RTBtn As New ToolStripButton
            Dim Sep As New ToolStripSeparator
            With RTBtn
                'set properties
                .Text = bName
                .AccessibleName = bName
                .Name = bName
                .DisplayStyle = ToolStripItemDisplayStyle.Text
            End With
            TS1.Items.Add(RTBtn)
            TS1.Items.Add(Sep)

        Next
    End Sub
End Class

Here is what you get when you run the program
Capture.JPG

Note

the toolstrip conatiner and toolstrip are not created at runtime, I already added them to the form.

Hope this helps!

Chris

I may or mat not need to mention that the toolstrip is named TS1.....

Here is a slightly easier way

        Dim xmldoc As XElement = XElement.Load("your path here")

        For Each itm As XElement In xmldoc...<buttonname>
            Dim btnName As String = itm.Value

            Dim RTbtn As New ToolStripButton
            Dim Sep As New ToolStripSeparator
            With RTbtn
                'set properties
                .Text = btnName
                .AccessibleName = btnName
                .Name = btnName
                .DisplayStyle = ToolStripItemDisplayStyle.Text
            End With
            'Create a Handler for a Click Event
            AddHandler RTbtn.Click, AddressOf TS1clickevent
            TS1.Items.Add(RTbtn)
            TS1.Items.Add(Sep)
        Next

What code did you use to create the XML file?

All things considered, I don't see what use that xml file is. It doesn't have any information as to button text, placement or function. You really can't create anything useful without a lot of information that has to come from somewhere else. All that the file really tells you is that you need five buttons and you could replace all of the xml text with a file that contains "5" and nothing more.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.