'//一個簡單的代碼生成「器」
'//在比較大的數據庫操作時,對字段操作有好多變量、屬性、函數的類似的重複性的大量代碼
'//如果一個個來重寫,確是令人煩躁的事,因此自己弄了一個小程序作這部分工作。
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
|
|