Solved

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

Posted on 2014-10-21
2
248 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
2 Comments
 
LVL 33

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

830 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