Solved

Make xml file in vb.net to populate it...

Posted on 2014-10-21
2
253 Views
Last Modified: 2014-10-21
Question: How can I code in vb.net to audit the controls on a form to enter it in xml file (ControlAccessibilty.xml)  as shown below?
<Forms>
<Form1>
<Controls>
 <TextBox>
    <InUse>True</InUse>
    <Enabled>True</Enabled>
 </TextBox>
 <ComboBox>
    <InUse>True</InUse>
    <Enabled>True</Enabled>
 </ComboBox>
etc.

</Controls>
</Form1>
</Forms>

The following code could be used as starting point:
Dim controlsD As New Dictionary(Of String, String)
        For Each contrl As Control In Me.Controls
            controlsD.Add(contrl.Name, contrl.GetType.ToString)
        Next

        For Each key As String In controlsD.Keys
            Debug.WriteLine("Name = " & key & "; Type = " & controlsD(key))
        Next

Open in new window

0
Comment
Question by:Mike Eghtebas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 34

Accepted Solution

by:
it_saige earned 500 total points
ID: 40395978
Potentially something like this:
Form1.vb -
Imports System.Xml
Imports System.Reflection

Public Class Form1

	Private Sub Button_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
		Using writer As XmlWriter = XmlWriter.Create("Controls.xml", New XmlWriterSettings() With {.Indent = True})
			With writer
				.WriteStartDocument()
				.WriteStartElement("Forms")
				.WriteStartElement(Me.GetType().ToString())
				.WriteElementString("Name", Name)
				For Each control As Control In Controls
					.WriteStartElement(control.GetType().ToString())
					.WriteElementString("Name", control.Name)
					For Each [property] As PropertyInfo In control.GetType().GetProperties()
						If [property].CanRead AndAlso [property].Name.Equals("InUse", StringComparison.InvariantCultureIgnoreCase) OrElse [property].Name.Equals("Enabled", StringComparison.InvariantCultureIgnoreCase) Then
							If Not [property].GetValue(control, Nothing) Is Nothing Then
								.WriteElementString([property].Name, [property].GetValue(control, Nothing).ToString())
							End If
						End If
					Next
					.WriteEndElement()
				Next
				.WriteEndElement()
				.WriteEndElement()
				.WriteEndDocument()
			End With
		End Using
	End Sub
End Class

Open in new window

Form1.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
		Me.CheckBox1 = New System.Windows.Forms.CheckBox()
		Me.TextBox1 = New System.Windows.Forms.TextBox()
		Me.ComboBox1 = New System.Windows.Forms.ComboBox()
		Me.Button1 = New System.Windows.Forms.Button()
		Me.SuspendLayout()
		'
		'CheckBox1
		'
		Me.CheckBox1.AutoSize = True
		Me.CheckBox1.Location = New System.Drawing.Point(13, 13)
		Me.CheckBox1.Name = "CheckBox1"
		Me.CheckBox1.Size = New System.Drawing.Size(81, 17)
		Me.CheckBox1.TabIndex = 0
		Me.CheckBox1.Text = "CheckBox1"
		Me.CheckBox1.UseVisualStyleBackColor = True
		'
		'TextBox1
		'
		Me.TextBox1.Location = New System.Drawing.Point(13, 36)
		Me.TextBox1.Name = "TextBox1"
		Me.TextBox1.Size = New System.Drawing.Size(100, 20)
		Me.TextBox1.TabIndex = 1
		'
		'ComboBox1
		'
		Me.ComboBox1.FormattingEnabled = True
		Me.ComboBox1.Items.AddRange(New Object() {"Apples", "Pears", "Oranges", "Bananas", "Cherries"})
		Me.ComboBox1.Location = New System.Drawing.Point(13, 62)
		Me.ComboBox1.Name = "ComboBox1"
		Me.ComboBox1.Size = New System.Drawing.Size(121, 21)
		Me.ComboBox1.TabIndex = 2
		'
		'Button1
		'
		Me.Button1.Location = New System.Drawing.Point(197, 229)
		Me.Button1.Name = "Button1"
		Me.Button1.Size = New System.Drawing.Size(75, 23)
		Me.Button1.TabIndex = 3
		Me.Button1.Text = "Audit"
		Me.Button1.UseVisualStyleBackColor = True
		'
		'Form1
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(284, 264)
		Me.Controls.Add(Me.Button1)
		Me.Controls.Add(Me.ComboBox1)
		Me.Controls.Add(Me.TextBox1)
		Me.Controls.Add(Me.CheckBox1)
		Me.Name = "Form1"
		Me.Text = "Form1"
		Me.ResumeLayout(False)
		Me.PerformLayout()

	End Sub
	Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
	Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
	Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
	Friend WithEvents Button1 As System.Windows.Forms.Button

End Class

Open in new window

Produces the following output:Capture.JPG
-saige-
0
 
LVL 34

Author Closing Comment

by:Mike Eghtebas
ID: 40395982
Thank you.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question