Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-10-21
2
Medium Priority
?
272 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 35

Accepted Solution

by:
it_saige earned 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

876 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