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

原创 2004年10月04日 11:56:00

'//一个简单的代码生成“器”<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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

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

 

 

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

 

一个最简单的监听器实现

这里我们将演示如何实现一个最简单的监听器。 目的1)通过最简单的例子来掩饰监听器模式的使用。2)减少重复编码。3)学习如何从简单到复杂的设计。目标1)扩展简单的监听器为更实用的复杂监听器。2)不用每次...
  • zxh5187406
  • zxh5187406
  • 2008年03月20日 01:27
  • 907

ROS教程(十一):编写简单的消息发布器和订阅器

编写简单的消息发布器和订阅器 (C++) Description: 本教程将介绍如何用 C++ 编写发布器节点和订阅器节点。 Tutorial Level: BEGINNER N...
  • zeuseign
  • zeuseign
  • 2017年06月22日 19:25
  • 348

树莓派开发板--Linux系统--ROS环境--实现一个简单的消息发布器和订阅器

简单的说就是,在ROS系统中利用话题实现两个子程序之间的信息传递。 举例子说,就好像某条电话线路中,A在一头说话,B在另一头接听,当B听到A发送的他可以理解的消息时,做出对应的行为。(例子可能不太恰...
  • u011957515
  • u011957515
  • 2016年12月12日 23:08
  • 571

Java实现的一个简单的下载器

代码还有很多不足之处,有什么想法希望大家可以交流一下。代码思路其实很简单,根据目标文件的网络位置(source)连接到网络并获取其InputStream然后写入到本地,我只实现了最基本的下载功能,至于...
  • u014401821
  • u014401821
  • 2014年04月08日 22:34
  • 378

ROS入门_1.14 编写及测试简单的消息发布器和订阅器 (C++)

1. 编写发布器节点 "节点(Node)" 是ROS中指代连接到ROS网络的可执行文件的术语。接下来,我们将会创建一个发布器节点("talker"),它将不断的在ROS网络中广播消息。 ...
  • ShuaiWangCSDN
  • ShuaiWangCSDN
  • 2017年01月15日 15:25
  • 784

编写测试简单的消息发布器和订阅器 (C++)(十)

本教程将介绍如何用 C++ 编写发布器节点和订阅器节点。内容编写发布器节点 源代码 代码说明 编写订阅器节点 源代码 代码说明 编译节点 测试 编写发布器节点『节点』(...
  • zx3517288
  • zx3517288
  • 2017年06月09日 23:16
  • 256

EF映射

EF映射的开始,开始的时候总能遇到各种错误,不过一一解决就是进步。
  • wgp15732622312
  • wgp15732622312
  • 2016年05月28日 21:42
  • 848

一个简单嗅探器的实现

这是最近的一段代码。 也没有什么好说的,简单的socket编程,设置网卡为混杂模式,接受所有封包,并打印所有封包的基本信息,及内容。 #include #include #include #...
  • yiyefangzhou24
  • yiyefangzhou24
  • 2011年09月25日 16:31
  • 4264

使用google analytics(分析)监测百度竞价关键词效果(网址构建工具)

面对着国内搜索市场压倒扭曲式成长,谷歌中国退出之后加之香港服务器的不稳定,其他的几个国内搜索引擎一时间也很难成气候的情况下,百度依然稳居着国内搜索市场上老大哥的位置。用户搜索份额也越来越一边倒的形势下...
  • adparking
  • adparking
  • 2012年10月17日 14:37
  • 1302

编写简单的消息发布器和订阅器

编写发布器节点 talker #include "ros/ros.h" //包含了ros的头文件#include "std_msgs/String.h" //引用了std_msgs/String...
  • weixin_40609307
  • weixin_40609307
  • 2017年11月03日 20:53
  • 75
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:一个简单的代码生成“器”
举报原因:
原因补充:

(最多只允许输入30个字)