分享
 
 
 

一个简单的代码生成“器”

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

'//一个简单的代码生成“器”

'//在比较大的数据库操作时,对字段操作有好多变量、属性、函数的类似的重复性的大量代码

'//如果一个个来重写,确是令人烦躁的事,因此自己弄了一个小程序作这部分工作。

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有