您的位置:首页 > 其它

mygeneration中自己做的winform模板,用于eco的输入,access数据源

2009-01-06 18:22 549 查看
日志标题:mygeneration中自己做的模板,用于eco的输入
发表时间:2006-9-3 20:51:09

花了半天时间,做了一个delphi下eco的单表输入页面模板,下是备忘,表是基于access的,

其中主程序:

<%
Dim bFirst
Dim name
Dim pname
Dim objTable
Dim objColumn
Dim tableNames
Dim databaseName
Dim tableName
dim ecospace
dim jj
' Grab the namespace

namespace = input.Item("txtNamespace")
Set tableName = input.Item("lstTables")

MyMeta.Language = input.Item("cmbLanguages")
MyMeta.DbTarget = "OleDb"

' Grab the choices the user made in our UI Script (see Interface Code tab)
Set tableNames = input.Item("lstTables")
ecospace=input.item("txtecospace")

databaseName = input.Item("cmbDatabase")
Set database = MyMeta.Databases(0)

' Loop through the tables the user select and generate the stored procs and save them to disk
For intLp = 0 To tableNames.Count - 1

set objTable = database.Tables(tablenames.item(intLp))

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Now we have the table that we desire to build a Business Object From, let us begin.
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%>
unit fm<%=objtable%>;

interface

uses
System.Drawing, System.Collections, System.ComponentModel,
System.Windows.Forms, System.Data,
Borland.Eco.Handles, Borland.Eco.Windows.Forms, Borland.Eco.WinForm,
<%=ecospace%>EcoSpace, Package_1Unit,Borland.Eco.Services, System.Resources ;

type
form<%=objtable%> = class(System.Windows.Forms.Form)
{$REGION 'Designer Managed Code'}
strict private
/// <summary>
/// Required designer variable.
/// </summary>
Components: System.ComponentModel.Container;
RHRoot: Borland.Eco.Handles.ReferenceHandle;
eh<%=objtable%>: Borland.Eco.Handles.ExpressionHandle;
cm<%=objtable%>: Borland.Eco.Handles.CurrencyManagerHandle;
DataGrid1: System.Windows.Forms.DataGrid;
StatusBar1: System.Windows.Forms.StatusBar;
ToolBar1: System.Windows.Forms.ToolBar;
ToolBarButton1: System.Windows.Forms.ToolBarButton;
ToolBarButton2: System.Windows.Forms.ToolBarButton;
ToolBarButton3: System.Windows.Forms.ToolBarButton;
ToolBarButton4: System.Windows.Forms.ToolBarButton;
ToolBarButton5: System.Windows.Forms.ToolBarButton;
ToolBarButton6: System.Windows.Forms.ToolBarButton;
ToolBarButton7: System.Windows.Forms.ToolBarButton;
ToolBarButton8: System.Windows.Forms.ToolBarButton;

//自动增加的文本框与标签框*******
<% output.writeLn ""
for Each objColumn in objTable.Columns
name = GetAlias(objColumn)
if not mid(ucase(name),1,3)="ECO" then
output.write " Text" & name& ": System.Windows.Forms.TextBox;"& vbCrLf
output.write " lbl" & name& ": System.Windows.Forms.LABEL;"& vbCrLf
end if

Next %>

//*******************************

GroupBox1: System.Windows.Forms.GroupBox;
DataGridTableStyle1: System.Windows.Forms.DataGridTableStyle;
// DataGridTextBoxColumn1: System.Windows.Forms.DataGridTextBoxColumn;

<% output.writeLn ""
k=1
For Each objColumn in objTable.Columns
name = GetAlias(objColumn)
if not mid(ucase(name),1,3)="ECO" then
output.write "DataGridTextBoxColumn" &cstr(k) &": System.Windows.Forms.DataGridTextBoxColumn;" & vbCrLf
END IF
k=k+1
Next %>
Label2: System.Windows.Forms.Label;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
procedure InitializeComponent;

procedure ToolBar1_ButtonClick(sender: System.object; e: System.Windows.Forms.ToolBarButtonClickEventArgs);
{$ENDREGION}
strict private
FEcoSpace: t<%=ecospace%>ecospace;
strict protected
/// <summary>
/// Clean up any resources being used.
/// </summary>
procedure Dispose(Disposing: Boolean);
procedure zeroform();
procedure setstate(mode:integer);
public
constructor Create();
// constructor Create(anEcoSpace: EcoSpace); overload;
function get_EcoSpace: Borland.Eco.Handles.EcoSpace;
property EcoSpace: Borland.Eco.Handles.EcoSpace read get_EcoSpace;
end;

[assembly: RuntimeRequired(TypeOf(form<%=objtable%>))]

implementation

uses
Borland.Eco.ObjectRepresentation;
{$AUTOBOX ON}

{$REGION 'Windows Form Designer generated code'}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
procedure form<%=objtable%> .InitializeComponent;
type
TArrayOfSystem_Windows_Forms_ToolBarButton = array of System.Windows.Forms.ToolBarButton;
TArrayOfSystem_Windows_Forms_DataGridTableStyle = array of System.Windows.Forms.DataGridTableStyle;
TArrayOfSystem_Windows_Forms_DataGridColumnStyle = array of System.Windows.Forms.DataGridColumnStyle;
begin
Self.RHRoot := Borland.Eco.Handles.ReferenceHandle.Create;

Self.eh<%=objtable%> := Borland.Eco.Handles.ExpressionHandle.Create;
Self.cm<%=objtable%> := Borland.Eco.Handles.CurrencyManagerHandle.Create;
Self.DataGrid1 := System.Windows.Forms.DataGrid.Create;

<% output.writeLn ""
k=1
For Each objColumn in objTable.Columns
name = GetAlias(objColumn)
if not mid(ucase(name),1,3)="ECO" then
output.write "Self.DataGridTextBoxColumn" &cstr(k) &":= System.Windows.Forms.DataGridTextBoxColumn.create;" & vbCrLf
END IF
k=k+1

Next %>

Self.DataGridTableStyle1:=System.Windows.Forms.DataGridTableStyle.Create;
Self.StatusBar1 := System.Windows.Forms.StatusBar.Create;
Self.ToolBar1 := System.Windows.Forms.ToolBar.Create;
Self.ToolBarButton1 := System.Windows.Forms.ToolBarButton.Create;
Self.ToolBarButton2 := System.Windows.Forms.ToolBarButton.Create;
Self.ToolBarButton3 := System.Windows.Forms.ToolBarButton.Create;
Self.ToolBarButton4 := System.Windows.Forms.ToolBarButton.Create;
Self.ToolBarButton8 := System.Windows.Forms.ToolBarButton.Create;
Self.ToolBarButton5 := System.Windows.Forms.ToolBarButton.Create;
Self.ToolBarButton6 := System.Windows.Forms.ToolBarButton.Create;
Self.ToolBarButton7 := System.Windows.Forms.ToolBarButton.Create;

Self.GroupBox1 := System.Windows.Forms.GroupBox.Create;
(System.ComponentModel.ISupportInitialize(Self.DataGrid1)).BeginInit;
Self.GroupBox1.SuspendLayout;
Self.SuspendLayout;
//
// RHRoot
//
Self.RHRoot.EcoSpaceType := TypeOf(<%=ECOSPACE%>EcoSpace.t<%=ECOSPACE%>EcoSpace);
//

//
//
// eh<%=objtable%>
//
Self.eh<%=objtable%>.Expression := '<%=objtable%>.allInstances';
Self.eh<%=objtable%>.RootHandle := Self.RHRoot;
//
//
// cm<%=objtable%>
//
Self.cm<%=objtable%>.BindingContext := Self.DataGrid1;
Self.cm<%=objtable%>.RootHandle := Self.eh<%=objtable%>;
//
// DataGrid1
//
Self.DataGrid1.DataMember := '';
Self.DataGrid1.DataSource := Self.eh<%=objtable%>;
Self.DataGrid1.HeaderForeColor := System.Drawing.SystemColors.ControlText;
Self.DataGrid1.Location := System.Drawing.Point.Create(8, 64);
Self.DataGrid1.Name := 'DataGrid1';
Self.DataGrid1.RightToLeft := System.Windows.Forms.RightToLeft.No;
Self.DataGrid1.RowHeaderWidth := 20;
Self.DataGrid1.Size := System.Drawing.Size.Create(320, 312);
Self.DataGrid1.TabIndex := 0;
Self.DataGrid1.TableStyles.AddRange(TArrayOfSystem_Windows_Forms_DataGridTableStyle.Create(Self.DataGridTableStyle1));
//
// DataGridTableStyle1
//
Self.DataGridTableStyle1.DataGrid := Self.DataGrid1;
<% output.writeLn ""

k=1
jj=1
output.write "Self.DataGridTableStyle1.GridColumnStyles.AddRange(TArrayOfSystem_Windows_Forms_DataGridColumnStyle.Create("
For Each objColumn in objTable.Columns
name = GetAlias(objColumn)
if not mid(ucase(name),1,3)="ECO" then
if (jj=1) then
output.write "Self.DataGridTextBoxColumn" & cstr(k)
jj=0
else
output.write ",Self.DataGridTextBoxColumn" & cstr(k)
end if
END IF
k=k+1

Next
output.write "));" & vbCrLf
%>

<% output.writeLn ""
k=1
For Each objColumn in objTable.Columns
name = GetAlias(objColumn)
if not mid(ucase(name),1,3)="ECO" then
output.write "Self.DataGridTextBoxColumn" &cstr(k) &".Format := '';" & vbCrLf
output.write "Self.DataGridTextBoxColumn" &cstr(k) &".FormatInfo := nil;" & vbCrLf
output.write "Self.DataGridTextBoxColumn" &cstr(k) &".HeaderText := '" & name & "';" & vbCrLf
output.write "Self.DataGridTextBoxColumn" &cstr(k) &".MappingName := '" & name & "';" & vbCrLf
output.write "Self.DataGridTextBoxColumn" &cstr(k) &".Width := 75;" & vbCrLf
END IF
k=k+1

Next %>
Self.DataGridTableStyle1.HeaderForeColor := System.Drawing.SystemColors.ControlText;
Self.DataGridTableStyle1.MappingName := '';
Self.DataGridTableStyle1.RowHeaderWidth := 20;
//
// DataGridTextBoxColumn1
//

//****************

//
// StatusBar1
//
Self.StatusBar1.Location := System.Drawing.Point.Create(0, 381);
Self.StatusBar1.Name := 'StatusBar1';
Self.StatusBar1.Size := System.Drawing.Size.Create(664, 24);
Self.StatusBar1.TabIndex := 1;
//
// ToolBar1
//
Self.ToolBar1.Appearance := System.Windows.Forms.ToolBarAppearance.Flat;
Self.ToolBar1.Buttons.AddRange(TArrayOfSystem_Windows_Forms_ToolBarButton.Create(Self.ToolBarButton1,
Self.ToolBarButton2, Self.ToolBarButton3, Self.ToolBarButton4, Self.ToolBarButton8,
Self.ToolBarButton5, Self.ToolBarButton6, Self.ToolBarButton7));
Self.ToolBar1.DropDownArrows := True;
Self.ToolBar1.Location := System.Drawing.Point.Create(0, 0);
Self.ToolBar1.Name := 'ToolBar1';
Self.ToolBar1.ShowToolTips := True;
Self.ToolBar1.Size := System.Drawing.Size.Create(664, 41);
Self.ToolBar1.TabIndex := 2;
Include(Self.ToolBar1.ButtonClick, Self.ToolBar1_ButtonClick);
//
// ToolBarButton1
//
Self.ToolBarButton1.Text := '新增';
//
// ToolBarButton2
//
Self.ToolBarButton2.Text := '修改';
//
// ToolBarButton3
//
Self.ToolBarButton3.Text := '保存';
//
// ToolBarButton4
//
Self.ToolBarButton4.Text := '取消';
//
// ToolBarButton8
//
Self.ToolBarButton8.Text := '删除';
//
// ToolBarButton5
//
Self.ToolBarButton5.Text := '上一条';
//
// ToolBarButton6
//
Self.ToolBarButton6.Text := '下一条';
//
// ToolBarButton7
//
Self.ToolBarButton7.Text := '退出';
//

// GroupBox1
//
Self.GroupBox1.Location := System.Drawing.Point.Create(368, 64);
Self.GroupBox1.Name := 'GroupBox1';
Self.GroupBox1.RightToLeft := System.Windows.Forms.RightToLeft.No;
Self.GroupBox1.Size := System.Drawing.Size.Create(264, 312);
Self.GroupBox1.TabIndex := 20;
Self.GroupBox1.TabStop := False;
Self.GroupBox1.Text := '输入';
//
// Label1
//
<% output.writeLn ""
k=1
For Each objColumn in objTable.Columns
name = GetAlias(objColumn)
if not mid(ucase(name),1,3)="ECO" then

output.write "Self.lbl" &name & ":= System.Windows.Forms.Label.Create;" & vbCrLf
output.write "Self.Text" &name & ":= System.Windows.Forms.TextBox.Create;" & vbCrLf

output.write "Self.Text" &name & ".DataBindings.Add(System.Windows.Forms.Binding.Create('Text',Self.eh" & objtable & ",'" & name & "'));" & vbCrLf
output.write "Self.Text" &name & ".Location := System.Drawing.Point.Create(60," & cstr(5+k*25) &");" & vbCrLf
output.write "Self.Text" &name &".Name := 'TExt" & name &"';" & vbCrLf
output.write "Self.Text" &name &".Size := System.Drawing.Size.Create(100, 20);" & vbCrLf
output.write "Self.Text" &name &".TabIndex := 20;" & vbCrLf
output.write "Self.Text" &name &".Text := 'TExt" & name & "';"& vbCrLf & vbCrLf

output.write "Self.lbl" &name & ".Location := System.Drawing.Point.Create(10," & cstr(5+k*25) &");" & vbCrLf
output.write "Self.lbl" &name &".Name := 'lbl" & name & "';" & vbCrLf
output.write "Self.lbl" &name &".Size := System.Drawing.Size.Create(40, 20);" & vbCrLf
output.write "Self.lbl" &name &".Text :='" & name & "';"& vbCrLf & vbCrLf
output.write "Self.GroupBox1.Controls.Add(Self.lbl" &name & ");" & vbCrLf
output.write "Self.GroupBox1.Controls.Add(Self.Text" &name & ");" & vbCrLf
END IF
k=k+1

Next %>
//

//

//
// form<%=objtable%>
//
Self.AutoScaleBaseSize := System.Drawing.Size.Create(6, 14);
Self.ClientSize := System.Drawing.Size.Create(664, 405);

Self.Controls.Add(Self.GroupBox1);
Self.Controls.Add(Self.ToolBar1);
Self.Controls.Add(Self.StatusBar1);
Self.Controls.Add(Self.DataGrid1);
Self.Name := 'form<%=objtable%>' ;
Self.RightToLeft := System.Windows.Forms.RightToLeft.No;
Self.StartPosition := System.Windows.Forms.FormStartPosition.CenterScreen;
Self.Text := '<%=objtable%>输入';
(System.ComponentModel.ISupportInitialize(Self.DataGrid1)).EndInit;
Self.GroupBox1.ResumeLayout(False);
Self.ResumeLayout(False);
end;

procedure form<%=objtable%>.setstate(mode:integer);
begin
datagrid1.ReadOnly:=true;
case mode of
1: //新增状态
begin
self.ToolBarButton2.Enabled:=false;
self.ToolBarButton7.Enabled:=false;
self.ToolBarButton1.Enabled:=false;
self.ToolBarButton5.Enabled:=false;
self.ToolBarButton6.Enabled:=false;
self.ToolBarButton3.Enabled:=true;
self.ToolBarButton4.Enabled:=true;
datagrid1.Enabled:=false;
groupbox1.Enabled:=true;
//textbox1.Focus;
end;
2: //修改状态
begin
self.ToolBarButton2.Enabled:=false;
self.ToolBarButton7.Enabled:=false;
self.ToolBarButton1.Enabled:=false;
self.ToolBarButton5.Enabled:=false;
self.ToolBarButton6.Enabled:=false;
self.ToolBarButton3.Enabled:=true;
self.ToolBarButton4.Enabled:=true;
datagrid1.Enabled:=false;
groupbox1.Enabled:=true;
//textbox1.Focus;
end;
3:
begin
self.ToolBarButton2.Enabled:=true;
self.ToolBarButton7.Enabled:=true;
self.ToolBarButton1.Enabled:=true;
self.ToolBarButton5.Enabled:=true;
self.ToolBarButton6.Enabled:=true;
self.ToolBarButton3.Enabled:=false;
self.ToolBarButton4.Enabled:=false;
datagrid1.Enabled:=true;
groupbox1.Enabled:=false;
end;
end;

end;

procedure form<%=objtable%>.zeroform;
begin
<% output.writeLn ""
for Each objColumn in objTable.Columns
name = GetAlias(objColumn)
if not mid(ucase(name),1,3)="ECO" then
output.write " Text" & name& ".clear;"& vbCrLf
end if

Next %>
end;

{$ENDREGION}

procedure form<%=objtable%>.Dispose(Disposing: Boolean);
begin
if Disposing then
begin
if Components <> nil then
Components.Dispose();
end;
inherited Dispose(Disposing);
end;

constructor form<%=objtable%>.Create();
begin
inherited Create;
//
// Required for Windows Form Designer support
//
InitializeComponent;
//
// TODO: Add any constructor code after InitializeComponent call
//
FEcoSpace := t<%=ecospace%>ecospace.Create;
// Note that you need to set the EcoSpace property for each ReferenceHandle you add to the form.

RHRoot.EcoSpace := FEcoSpace;
if (not fecospace.Active) then
fecospace.Active:=true;

zeroform;
setstate(3);
end;

function form<%=objtable%>.get_EcoSpace: Borland.Eco.Handles.EcoSpace;
begin
Result := FEcoSpace;
end;

procedure form<%=objtable%>.ToolBar1_ButtonClick(sender: System.object; e: System.Windows.Forms.ToolBarButtonClickEventArgs);
var a<%=objtable%>: <%=objtable%>;
begin
case toolbar1.Buttons.IndexOf(e.Button) of
0: //新增
begin
fEcoSpace.UndoService.StartUndoBlock('abc');
a<%=objtable%> := <%=objtable%>.create(fecospace);
datagrid1.DataBindings;
eh<%=objtable%>.EnsureBindingList;
cm<%=objtable%>.set_Position(eh<%=objtable%>.Element.GetAsCollection.Count - 1);
setstate(1);
end;
1: //修改
begin
fEcoSpace.UndoService.startUndoBlock('abc');
setstate(2);
end;
2: //保存
begin
fecospace.UndoService.ClearAllUndoBlocks;
setstate(3);
fecospace.UpdateDatabase;
end;
3: //取消
begin
fEcoSpace.UndoService.UndoBlock('abc');
fecospace.UndoService.ClearAllUndoBlocks;
setstate(3);
end;
4: // 删除
begin
if (MessageBox.Show('你真的要删除吗,按确定后记录将被删除并不能恢复?', '删除数据',
MessageBoxButtons.YesNo, MessageBoxIcon.Question) = System.Windows.Forms.DialogResult.Yes) then
begin
try
<%=objtable%>(cm<%=objtable%>.Element.AsObject).AsIObject.Delete;
except
messagebox.show('该名称已经使用,不能删除!');
exit;
end;
fecospace.UpdateDatabase;
end;
end;
5: // 上一条
begin

end;
6: // 下一条
begin

end;
7: //退出
begin
close;
// fecospace.UpdateDatabase;
end;
end;

end;
end.

<%
' Save the output file for this Table
Dim filename
filename = input.item("txtPath")

Dim length
Dim pos
lenth = Len(filename)
pos = InStrRev(filename, "/")

If Not pos = lenth Then
filename = filename & "/"
End If

filename = filename & "fm" & objTable.Alias & ".pas"

output.save filename, false
buffer = buffer & output.text
output.clear
Next ' tableName

output.write buffer
%>

<%
'===========================================================================
' These are support routines called by the above scirpt
'===========================================================================
Function GetAlias(objColumn)
Dim name
name = TrimSpaces(objColumn.Alias)
GetAlias = UCase(Left(name, 1)) & Right(name, Len(name) -1)
End Function

Function GetName(objColumn)
Dim name
name = objColumn.Name
GetName = UCase(Left(name, 1)) & Right(name, Len(name) -1)
End Function

Function TrimSpaces(str)

Dim tname
Dim name
Dim char
Dim l

name = ""
tname = str
l = Len(tname)

For j = 1 To l
char = Mid(tname, j, 1)
If Not char = " " Then
name = name & char
End If
Next

TrimSpaces = name
End Function
%>

另外ui界面:

<%

Dim bFirst
Dim name
Dim pname
Dim objTable
Dim objColumn
Dim tableNames
Dim databaseName
Dim tableName

' Grab the namespace
namespace = input.Item("txtNamespace")

Set tableName = input.Item("lstTables")

MyMeta.Language = input.Item("cmbLanguages")
MyMeta.DbTarget = "OleDb"

' Grab the choices the user made in our UI Script (see Interface Code tab)
Set tableNames = input.Item("lstTables")

databaseName = input.Item("cmbDatabase")
Set database = MyMeta.Databases(0)

' Loop through the tables the user select and generate the stored procs and save them to disk
For intLp = 0 To tableNames.Count - 1

Set objTable = database.Tables(tablenames.item(intLp))

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Now we have the table that we desire to build a Business Object From, let us begin.
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%>
'===============================================================================

unit fm<%=objtable%>;

interface

uses
System.Drawing, System.Collections, System.ComponentModel,
System.Windows.Forms, System.Data,
Borland.Eco.Handles, Borland.Eco.Windows.Forms, Borland.Eco.WinForm,
jxcEcoSpace, Package_1Unit,Borland.Eco.Services, System.Resources ;

type
form<%=objtable%> = class(System.Windows.Forms.Form)
{$REGION 'Designer Managed Code'}
strict private
/// <summary>
/// Required designer variable.
/// </summary>
Components: System.ComponentModel.Container;
RHRoot: Borland.Eco.Handles.ReferenceHandle;
eh<%=objtable%>: Borland.Eco.Handles.ExpressionHandle;

'===============================================================================
<%
' Save the output file for this Table
Dim filename
filename = input.item("txtPath")

Dim length
Dim pos
lenth = Len(filename)
pos = InStrRev(filename, "/")

If Not pos = lenth Then
filename = filename & "/"
End If

filename = filename & objTable.Alias & ".vb"

output.save filename, false
buffer = buffer & output.text
output.clear
Next ' tableName

output.write buffer
%>

<%
'===========================================================================
' These are support routines called by the above scirpt
'===========================================================================
Function GetAlias(objColumn)
Dim name
name = TrimSpaces(objColumn.Alias)
GetAlias = UCase(Left(name, 1)) & Right(name, Len(name) -1)
End Function

Function GetName(objColumn)
Dim name
name = objColumn.Name
GetName = UCase(Left(name, 1)) & Right(name, Len(name) -1)
End Function

Function TrimSpaces(str)

Dim tname
Dim name
Dim char
Dim l

name = ""
tname = str
l = Len(tname)

For j = 1 To l
char = Mid(tname, j, 1)
If Not char = " " Then
name = name & char
End If
Next

TrimSpaces = name
End Function
%>

改进中,做作备忘.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: