'//一个简单的代码生成“器”
'//在比较大的数据库操作时,对字段操作有好多变量、属性、函数的类似的重复性的大量代码
'//如果一个个来重写,确是令人烦躁的事,因此自己弄了一个小程序作这部分工作。
Public Class FrmPropertyCode
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
Initialize()
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents VariableTextBox As System.Windows.Forms.TextBox
Friend WithEvents CodeTextBox As System.Windows.Forms.TextBox
Friend WithEvents PropertyComboBox As System.Windows.Forms.ComboBox
Friend WithEvents PropertyTextBox As System.Windows.Forms.TextBox
Friend WithEvents SavePropertyButton As System.Windows.Forms.Button
Friend WithEvents BuildCodeButton As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Label4 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(FrmPropertyCode))
Me.VariableTextBox = New System.Windows.Forms.TextBox
Me.CodeTextBox = New System.Windows.Forms.TextBox
Me.PropertyComboBox = New System.Windows.Forms.ComboBox
Me.PropertyTextBox = New System.Windows.Forms.TextBox
Me.SavePropertyButton = New System.Windows.Forms.Button
Me.BuildCodeButton = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.Label4 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'VariableTextBox
'
Me.VariableTextBox.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
Me.VariableTextBox.Location = New System.Drawing.Point(8, 48)
Me.VariableTextBox.Multiline = True
Me.VariableTextBox.Name = "VariableTextBox"
Me.VariableTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Both
Me.VariableTextBox.Size = New System.Drawing.Size(168, 176)
Me.VariableTextBox.TabIndex = 0
Me.VariableTextBox.Text = ""
'
'CodeTextBox
'
Me.CodeTextBox.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.CodeTextBox.BackColor = System.Drawing.SystemColors.Info
Me.CodeTextBox.Location = New System.Drawing.Point(192, 48)
Me.CodeTextBox.Multiline = True
Me.CodeTextBox.Name = "CodeTextBox"
Me.CodeTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Both
Me.CodeTextBox.Size = New System.Drawing.Size(344, 176)
Me.CodeTextBox.TabIndex = 1
Me.CodeTextBox.Text = ""
'
'PropertyComboBox
'
Me.PropertyComboBox.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
Me.PropertyComboBox.BackColor = System.Drawing.SystemColors.Info
Me.PropertyComboBox.Location = New System.Drawing.Point(88, 240)
Me.PropertyComboBox.Name = "PropertyComboBox"
Me.PropertyComboBox.Size = New System.Drawing.Size(224, 20)
Me.PropertyComboBox.TabIndex = 2
'
'PropertyTextBox
'
Me.PropertyTextBox.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.PropertyTextBox.BackColor = System.Drawing.SystemColors.Info
Me.PropertyTextBox.Location = New System.Drawing.Point(8, 296)
Me.PropertyTextBox.Multiline = True
Me.PropertyTextBox.Name = "PropertyTextBox"
Me.PropertyTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Both
Me.PropertyTextBox.Size = New System.Drawing.Size(536, 112)
Me.PropertyTextBox.TabIndex = 3
Me.PropertyTextBox.Text = ""
'
'SavePropertyButton
'
Me.SavePropertyButton.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
Me.SavePropertyButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.SavePropertyButton.Location = New System.Drawing.Point(320, 240)
Me.SavePropertyButton.Name = "SavePropertyButton"
Me.SavePropertyButton.Size = New System.Drawing.Size(104, 32)
Me.SavePropertyButton.TabIndex = 4
Me.SavePropertyButton.Text = "保存样式"
'
'BuildCodeButton
'
Me.BuildCodeButton.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.BuildCodeButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.BuildCodeButton.Location = New System.Drawing.Point(432, 240)
Me.BuildCodeButton.Name = "BuildCodeButton"
Me.BuildCodeButton.Size = New System.Drawing.Size(104, 32)
Me.BuildCodeButton.TabIndex = 5
Me.BuildCodeButton.Text = "生成代码"
'
'Label1
'
Me.Label1.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
Me.Label1.Location = New System.Drawing.Point(8, 240)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(80, 23)
Me.Label1.TabIndex = 6
Me.Label1.Text = "样式选择:"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(8, 16)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(88, 23)
Me.Label2.TabIndex = 7
Me.Label2.Text = "变量框:"
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(192, 16)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(96, 23)
Me.Label3.TabIndex = 8
Me.Label3.Text = "代码框:"
'
'Label4
'
Me.Label4.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
Me.Label4.Location = New System.Drawing.Point(8, 272)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(96, 17)
Me.Label4.TabIndex = 9
Me.Label4.Text = "样式框:"
'
'FrmPropertyCode
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(552, 421)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.BuildCodeButton)
Me.Controls.Add(Me.SavePropertyButton)
Me.Controls.Add(Me.PropertyTextBox)
Me.Controls.Add(Me.CodeTextBox)
Me.Controls.Add(Me.VariableTextBox)
Me.Controls.Add(Me.PropertyComboBox)
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.Name = "FrmPropertyCode"
Me.Text = "属性代码生成器"
Me.ResumeLayout(False)
End Sub
#End Region
'//以下为添加代码
Private ds As New DataSet
'//以下两个变量存放当前记录值,在SavePropertyButton函数中用来判断进行相关操作
Private CurrentName As String = ""
Private CurrentText As String = ""
Private WithEvents Manager As CurrencyManager
Private Sub Initialize()
Application.DoEvents()
'//读取XML架构文件
ds.ReadXmlSchema("Property.xsd")
'//读取XML数据文件
ds.ReadXml("Property.xml")
Me.PropertyComboBox.DataSource = ds.Tables(0)
Me.PropertyComboBox.DisplayMember = "Name"
Me.PropertyTextBox.DataBindings.Add(New Binding("Text", ds.Tables(0), "Text"))
Manager = CType(Me.BindingContext(ds.Tables(0)), CurrencyManager)
End Sub
Private Sub SavePropertyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SavePropertyButton.Click
'//改名
If Not Me.PropertyComboBox.Text.Equals(CurrentName) AndAlso Me.PropertyTextBox.Text.Equals(CurrentText) Then
CType(Manager.Current, DataRowView).Row("Name") = Me.PropertyComboBox.Text
End If
'//改内容
If Me.PropertyComboBox.Text.Equals(CurrentName) AndAlso Not Me.PropertyTextBox.Text.Equals(CurrentText) Then
CType(Manager.Current, DataRowView).Row("Text") = Me.PropertyTextBox.Text
End If
'//新增
If Not Me.PropertyComboBox.Text.Equals(CurrentName) AndAlso Not Me.PropertyTextBox.Text.Equals(CurrentText) Then
CType(Manager.Current, DataRowView).Row("Text") = CurrentText
Dim Row As DataRow = ds.Tables(0).NewRow
Row("Name") = Me.PropertyComboBox.Text
Row("Text") = Me.PropertyTextBox.Text
ds.Tables(0).Rows.Add(Row)
End If
ds.Tables(0).AcceptChanges()
'//保存数据
ds.WriteXml("Property.Xml")
Manager.Refresh()
Manager.Position = ds.Tables(0).Rows.Count - 1
End Sub
Private Sub Position_Changed(ByVal sender As Object, ByVal e As EventArgs) Handles Manager.PositionChanged
CurrentName = Me.PropertyComboBox.Text
CurrentText = Me.PropertyTextBox.Text
End Sub
Private Sub BuildCodeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BuildCodeButton.Click
Me.CodeTextBox.Clear()
Dim Line As String
For Each Line In Me.VariableTextBox.Lines
Me.CodeTextBox.AppendText(String.Format(Me.PropertyTextBox.Text, Line.Split(","c)))
Me.CodeTextBox.AppendText(ControlChars.CrLf)
Next
End Sub
End Class
'//以下为架构文件Property.xsd内容
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="Property" targetNamespace="http://tempuri.org/Property.xsd" elementFormDefault="qualified"
xmlns="http://tempuri.org/Property.xsd" xmlns:mstns="http://tempuri.org/Property.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="PropertyCollection">
<xs:complexType>
<xs:sequence>
<xs:element name="Property" type="Property" />
</xs:sequence>
</xs:complexType>
<xs:key name="PropertyCollectionKey1">
<xs:selector xpath=".//mstns:Property" />
<xs:field xpath="mstns:Name" />
</xs:key>
</xs:element>
<xs:complexType name="Property">
<xs:sequence>
<xs:element name="Name" type="xs:string" />
<xs:element name="Text" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
'//以下为数据文件Property.XML内容
<?xml version="1.0" encoding="utf-8" ?>
<PropertyCollection xmlns="http://tempuri.org/Property.xsd">
<Property>
<Name>声明_Private_Integer</Name>
<Text>Private _{0} As Integer</Text>
</Property>
<Property>
<Name>声明_Private_String</Name>
<Text>Private _{0} As String</Text>
</Property>
<Property>
<Name>属性_Protected_String</Name>
<Text>Public ReadOnly Property {0} AS String
Get
Return _{0}
End Get
End Property</Text>
</Property>
</PropertyCollection>
'//比较简单,反正是一时之用,使用效果见http://community.csdn.net/Expert/topic/3424/3424369.xml?temp=.4860041
本文地址:http://com.8s8s.com/it/it43809.htm