|
The Form design,
book_search
Create
Delphi 8 Windows Forn - book_search
Classes used in this Form: System.Drawing,
System.Collections, System.ComponentModel, System.Windows.Forms,
System.Data, System.Data.OleDb, System.Resources
Type of Controls
used in this Form: Label,
Panel, ComboBox, Button, PictureBox, CheckBox, TextBox,
DataGrid, OleDbConnection, ImageList
Create
the Form book_search
- On the File menu ,
click New. and then click Windows Form
- New File
name WinForm.pas appears. (Form name - TWinForm)
- On the View menu ,
click Object Inspector. In Object Inspector
modify the following ...
|
|
1. |
WinForm.pas - the File name created
|
|
|
Old |
New |
File
Name: |
WinForm.pas |
book_search.,pas |
|
|
|
|
2. |
TWinForm- Form name created
|
|
|
Old |
New |
Caption:
Name:
Icon:
Menu:
Maximizebox:
StartPosition:
Size:
Text: |
WinForm
TWinForm
(Icon)
(none)
True
WindowsDefaultLocation
300,300
WinForm |
book_search
Tbook_search
(Icon)
(none)
False
Manuel
670, 456
book search ... |
|
|
|
|
3. |
Save this
file ... On the
File menu , click Save |
|
|
|
1. |
Picture files... |
|
- The folder
c:\work_Delphinet\pic stored the bmp file search1.bmp,
v_line6.bmp, btn_cancel.bmp, btn_ok.bmp
|
2. |
The book_search design |
|
|
3. |
From the Tool palette add these
controls ...
(From the View menu, click Tool
palette)
. 10 Label controls,
5 Panel controls, 5 ComboBox
controls, 4 Button controls, 3 PictureBox
controls, 2 CheckBox controls, 2 TextBox
controls, 1 DataGrid controls, 1 OleDbConnection
controls, 1 ImageList
controls |
|
- Add 10 Label
control
- lcondition, Object inspector
Name: Font: Size: Text: TextAlign: |
lcondition Microsoft Sans
Serif, 8.25pt 104,
16 condition TopLeft | |
- lsearch1
- lbook
- lcity1
- lphone1
- lcountry1
- lfirst1
- lfather1
- lname1
- lbfound
|
- Add 5 Panel
controls
- Panel11, Object inspector
Name: Size: |
Panel11 656,
368 | |
- Panel112
Size:
152, 304
- Panel113
Size:
168,
24
- Panel114
Size:
656, 32
|
- Add 5 ComboBox
controls
- cmbname, Object
inspector
Name: Size: |
cmbname 136,
21 | |
- cmbfather
Size: 136,
21
- cmbfirst
Size:
136,
21
- cmbcountry
Size: 136,
21
- cmbcity
Size: 136,
21
|
- Add 4 Button
controls
- Btnsearch, Object
inspector
Name: Image: Size: Text: |
Btnsearch System.Drawing.Bitmap 24,
72 | |
The
folder c:\work_Delphinet\pic
stored the bmp files
search1.bmp
- btnclear
Text: clear
- Btnok
ImageList1
ImageIndex:
0
- Btncancel
mageList1
ImageIndex:
1
|
- Add 3 PictureBox
control
s
- Pic_line011, Object inspector
Name: Image: Size
Mode: |
Pic_line011 System.Drawing.Bitmap Autosize | |
Bitmap file added = C:\work_Delphinet\pic\v_line6.bmp
- Pic_line012 -
Bitmap file added = C:\work_Delphinet\pic\v_line6.bmp
- Pic_line013 -
Bitmap file added = C:\work_Delphinet\pic\v_line6.bmp
|
- Add 2 CheckBox
controls
- chk_allbook, Object
inspector
Name: Size: |
chk_allbook 16,
16 | |
-
chk_condition
Size: 16,
16 |
- Add 2 TextBox
controls
- txtsearch1, Object
inspector
Name: Size: Text: |
txtsearch1 18,
114 SEARCH | |
- txtphone
Size: 132,
20
|
- Add the DataGrid
control
- DataGrid1,
Object inspector
Use the DataGrid
control to display the fields of a data source as
columns in a table. Each row in the DataGrid control
represents a record in the data source. The DataGrid
control supports selection, editing, deleting, paging,
and sorting. Different column types determine the
behavior of the columns in the
control.
DataGrid1, Object inspector
Name: Size: TableStyles: |
DataGrid1 440,
328 (Collection) - of 5 columns
... | |
- Add the OleDbConnection
control
-
OleDbConnection1, Object
inspector
Represents an
open connection to a data source The
OleDbConnection object of the .NET Framework
Data Provider for OLE DB provides connectivity to data
sources exposed using OLE DB The .NET Framework
Data Provider for OLE DB automatically pools
connections using OLE DB session pooling. Connection
string arguments can be used to enable or disable OLE
DB services including pooling. It is recommended that you always
close the Connection when you are finished
using it
The OleDbConnection1
Properties:
Name: ConnectionString: |
OleDbConnection1 Jet
OLEDB:Global Partial
.... | |
Note: Connection info
:
Provider =
Microsoft.Jet.OLEDB.4.0
Data source =
C:\work_Delphinet\data\work_Delphinet.mdb
- Add the ImageList
control
-
ImageList1,
Object inspector
Name: Images: ImageSize: |
ImageList1 (Collection)
- of 2 Bitmap
image 56,
36 | |
The folder c:\work_Delphinet\pic stored
the 2 bmp files btn_cancel.bmp,
btn_ok.bmp | | |
|
book_search file, the codes after modification
...
The
text
Code is
red
color, the codes added
manuel |
|
unit
book_search;
interface
uses
System.Drawing, System.Collections,
System.ComponentModel,
System.Windows.Forms, System.Data, System.Data.OleDb,
System.Resources;
type
Tbook_search = class(System.Windows.Forms.Form)
strict protected
/// <summary>
/// Clean up any resources being used.
/// </summary>
procedure Dispose(Disposing: Boolean); override;
private
{ Private Declarations }
procedure sub_load(v_field:
string);
procedure sub_check;
procedure sub_clearall;
procedure sub_search(v_name: string; v_firstname:
string; v_fathername: string; v_country: string; v_city:
string; v_phone: string);
procedure sub_select;
public
constructor Create;
class function Showbooksearch:
System.Windows.Forms.DialogResult;
class function book_v_recno: Integer;
class function book_findedit_Flag: string;
end;
[assembly: RuntimeRequiredAttribute(TypeOf(Tbook_search))]
implementation
uses
book_note;
var
vselect: System.Data.OleDb.OleDbCommand;
vda: System.Data.OleDb.OleDbDataAdapter;
vdset: System.Data.DataSet;
vitem_grid: string;
findedit_Flag: string;
s_flag: Boolean;
searchflg: Boolean;
v_recno: Integer;
Windows Form Designer
generated code |
procedure
Tbook_search.Dispose(Disposing: Boolean);
begin
if Disposing then
begin
if Components <> nil then
Components.Dispose();
end;
inherited Dispose(Disposing);
end;
constructor Tbook_search.Create;
var
vleft: Integer;
vtop: Integer;
begin
inherited Create;
//
// Required for Windows Form Designer support
//
InitializeComponent;
//
// TODO: Add any constructor code after
InitializeComponent call
//
// Move
vleft := book_note.Tbook_note.booknote_left +
book_note.Tbook_note.booknote_width - Self.Width;
vtop := book_note.Tbook_note.booknote_top;
Self.Location := System.Drawing.Point.Create(vleft, vtop);
//resize
Self.Btnok.Top := (self.Panel5.Height -
Self.Btnok.Height) div 2;
self.Btncancel.Top := self.Btnok.Top;
self.Btnsearch.Left := (self.Panel3.Width -
self.Btnsearch.Width) div 2;
self.Pic_line011.Top := self.Btnok.Top + ((self.Btnok.Height
- (self.Pic_line011.Height * 3)) div 4);
self.Pic_line012.Top := self.Pic_line011.Top +
self.Pic_line011.Height + ((self.Btnok.Height -
(self.Pic_line011.Height * 3)) div 4);
self.Pic_line013.Top := self.Pic_line012.Top +
self.Pic_line012.Height + ((self.Btnok.Height -
(self.Pic_line011.Height * 3)) div 4);
self.Pic_line011.Left := (self.Btnok.Left -
self.Pic_line011.Width) div 2;
self.Pic_line012.Left := self.Pic_line011.Left;
self.Pic_line013.Left := self.Pic_line011.Left;
end;
class function
Tbook_search.Showbooksearch:
System.Windows.Forms.DialogResult;
begin
with Tbook_search.Create do
Result := ShowDialog;
end;
procedure Tbook_search.Tbook_search_Load(sender:
System.Object; e: System.EventArgs);
begin
s_flag := False;
searchflg := False;
sub_clearall;
self.lbfound.Text := '';
//booknote_type := 'find';
if (book_note.Tbook_note.booknote_type = 'find') then
self.Text := 'search, book note ...';
if (book_note.Tbook_note.booknote_type = 'edit') then
self.Text := 'edit, book note ...';
self.chk_allbook.Checked := True;
sub_load('name');
sub_load('first');
sub_load('father');
sub_load('city');
sub_search('', '', '', '', '', '');
end;
//add data to comboboxes
procedure Tbook_search.sub_load(v_field: string);
var
i: Integer;
begin
vselect := System.Data.OleDb.OleDbCommand.Create;
vda := System.Data.OleDb.OleDbDataAdapter.Create;
vdset := System.Data.DataSet.Create;
if (v_field = 'name') then
vselect.CommandText := 'SELECT DISTINCT Trim(name) AS
vname FROM book WHERE (Trim(name) <> "") ORDER BY
Trim(name)';
if (v_field = 'first') then
vselect.CommandText := 'SELECT DISTINCT Trim(first_name)
AS vfirst FROM book WHERE (Trim(first_name) <>"")ORDER
BY Trim(first_name)';
if (v_field = 'father') then
vselect.CommandText := 'SELECT DISTINCT Trim(father_name)
AS vfather FROM book WHERE (Trim(father_name) <>"")ORDER
BY Trim(father_name)';
if (v_field = 'city') then
vselect.CommandText := 'SELECT DISTINCT Trim(city) AS
vcity FROM book WHERE (Trim(city)<>"")ORDER BY Trim(city)';
vselect.Connection := Self.OleDbConnection1;
vda.SelectCommand := vselect;
vdset.EnforceConstraints := False;
Try
//Open the connection.
Self.OleDbConnection1.Open();
vda.Fill(vdset, 'book');
finally
//Turn constraint checking back on.
vdset.EnforceConstraints := True;
//Close the connection
Self.OleDbConnection1.Close();
end;
if (vdset.Tables['book'].Rows.Count > 0) then
begin
for i := 0 to (vdset.Tables['book'].Rows.Count - 1) do
begin
if (v_field = 'name') then
Self.cmbname.Items.Add(vdset.Tables['book'].Rows[i].Item['vname'].ToString().Trim());
if (v_field = 'first') then
Self.cmbfirst.Items.Add(vdset.Tables['book'].Rows[i].Item['vfirst'].ToString().Trim());
if (v_field = 'father') then
Self.cmbfather.Items.Add(vdset.Tables['book'].Rows[i].Item['vfather'].ToString().Trim());
if (v_field = 'city') then
Self.cmbcity.Items.Add(vdset.Tables['book'].Rows[i].Item['vcity'].ToString().Trim());
end;
end;
end;
procedure Tbook_search.Chkcond_CheckedChanged(sender:
System.Object; e: System.EventArgs);
begin
end;
procedure
Tbook_search.cmbname_SelectedIndexChanged(sender:
System.Object; e: System.EventArgs);
begin
if (s_flag = True) then sub_check;
end;
procedure Tbook_search.sub_check;
begin
if (self.chk_allbook.Checked = True) then
self.chk_allbook.Checked := False;
if (self.Btnsearch.Enabled = False) then
self.Btnsearch.Enabled := True;
end;
procedure
Tbook_search.cmbfirst_SelectedIndexChanged(sender:
System.Object; e: System.EventArgs);
begin
if (s_flag = True) then sub_check;
end;
procedure
Tbook_search.cmbfather_SelectedIndexChanged(sender:
System.Object;
e: System.EventArgs);
begin
if (s_flag = True) then sub_check;
end;
procedure
Tbook_search.cmbcountry_SelectedIndexChanged(sender:
System.Object;
e: System.EventArgs);
begin
if (s_flag = True) then sub_check;
end;
procedure
Tbook_search.cmbcity_SelectedIndexChanged(sender:
System.Object; e: System.EventArgs);
begin
if (s_flag = True) then sub_check;
end;
procedure Tbook_search.txtphone_KeyPress(sender:
System.Object; e: System.Windows.Forms.KeyPressEventArgs);
begin
if (s_flag = True) then sub_check;
end;
procedure Tbook_search.chk_allbook_CheckedChanged(sender:
System.Object; e: System.EventArgs);
begin
if (s_flag = True and self.chk_allbook.Checked = True)
then
begin
if (self.Btnsearch.Enabled = False ) then
self.Btnsearch.Enabled := True;
end;
end;
procedure Tbook_search.Btnclear_Click(sender:
System.Object; e: System.EventArgs);
begin
sub_clearall;
end;
procedure Tbook_search.sub_clearall;
begin
self.cmbname.set_SelectedIndex(-1);
self.cmbfirst.set_SelectedIndex(-1);
self.cmbfather.set_SelectedIndex(-1);
self.cmbcountry.set_SelectedIndex(-1);
self.cmbcity.set_SelectedIndex(-1);
self.txtphone.Text := '';
if (self.Btnsearch.Enabled = True) then
self.Btnsearch.Enabled := False;
end;
procedure Tbook_search.Btnsearch_Click(sender:
System.Object; e: System.EventArgs);
begin
searchflg := True;
if (Self.chk_allbook.Checked = True) then
sub_search('', '', '', '', '', '');
if (Self.chk_allbook.Checked = False) then
sub_search(Self.cmbname.Text.Trim(),
Self.cmbfirst.Text.Trim(), Self.cmbfather.Text.Trim(),
Self.cmbcountry.Text.Trim(), Self.cmbcity.Text.Trim(),
Self.txtphone.Text.Trim());
end;
procedure Tbook_search.sub_search(v_name: string;
v_firstname: string; v_fathername: string; v_country:
string; v_city: string; v_phone: string);
var
vfound : Integer;
begin
vselect := System.Data.OleDb.OleDbCommand.Create;
vda := System.Data.OleDb.OleDbDataAdapter.Create;
vdset := System.Data.DataSet.Create;
if (self.chk_allbook.Checked = True) then
vselect.CommandText := 'SELECT bookid,alternativePhone,
city, country, father_name, fax, first_name, homephone,
mobilephone, name, sex, workphone FROM book WHERE (name
<> "") ORDER BY name, first_name, father_name';
if (self.chk_allbook.Checked = False) then
vselect.CommandText := 'SELECT bookid,alternativePhone,
city, country, father_name, fax, first_name, homephone,
mobilephone, name, sex, workphone FROM book WHERE ((name
<> "") And (Trim(name) = "' + v_name + '") Or (Trim(first_name)
= "' + v_firstname + '") Or (Trim(father_name) = "' +
v_fathername + '") Or (Trim(country) = "' + v_country +
'") Or (Trim(city) = "' + v_city + '") Or ((Trim(homephone)
= "' + v_phone + '") Or (Trim(workphone) = "' + v_phone
+ '") Or (Trim(mobilephone) = "' + v_phone + '") Or (Trim(alternativePhone)
= "' + v_phone + '") Or (Trim(fax) = "' + v_phone +
'"))) ORDER BY name, first_name, father_name';
vselect.Connection := Self.OleDbConnection1;
vda.SelectCommand := vselect;
vdset.EnforceConstraints := False;
Try
//Open the connection.
Self.OleDbConnection1.Open();
vda.Fill(vdset, 'book');
finally
//Turn constraint checking back on.
vdset.EnforceConstraints := True;
//Close the connection
Self.OleDbConnection1.Close();
end;
vfound := vdset.Tables['book'].Rows.Count;
self.DataGrid1.DataSource := vdset;
self.DataGrid1.DataMember := 'book';
s_flag := True;
if (searchflg = true) then
begin
Self.Btnsearch.Enabled := True;
Self.btnclear_Click(btnclear, nil);
searchflg := False;
end;
if (self.chk_allbook.Checked = True) then
self.lbfound.Text := vfound.ToString + ' - item(s)
found';
if (self.chk_allbook.Checked = False) then
self.lbfound.Text := vfound.ToString + ' - item(s)
found';
self.lbfound.Focus();
end;
procedure Tbook_search.sub_select;
var
vvrec: Integer;
begin
vselect := System.Data.OleDb.OleDbCommand.Create;
vda := System.Data.OleDb.OleDbDataAdapter.Create;
vdset := System.Data.DataSet.Create;
vselect.CommandText := 'SELECT bookid, country,
father_name, first_name, name, sex FROM book WHERE (name
<> "") ORDER BY bookid';
vselect.Connection := Self.OleDbConnection1;
vda.SelectCommand := vselect;
vdset.EnforceConstraints := False;
Try
//Open the connection.
Self.OleDbConnection1.Open();
vda.Fill(vdset, 'book');
finally
//Turn constraint checking back on.
vdset.EnforceConstraints := True;
//Close the connection
Self.OleDbConnection1.Close();
end;
if (vdset.Tables['book'].Rows.Count > 0) then
begin
for vvrec := 0 to (vdset.Tables['book'].Rows.Count - 1)
do
begin
if (vdset.Tables['book'].Rows[vvrec].Item['bookid'].ToString
= vitem_grid) then
begin
v_recno := vvrec;
exit;
end;
end;
end;
end;
procedure Tbook_search.DataGrid1_Click(sender:
System.Object; e: System.EventArgs);
begin
vitem_grid :=
self.DataGrid1.Item[self.DataGrid1.CurrentRowIndex,0].ToString;
self.Btnok.Enabled := True;
end;
procedure Tbook_search.DataGrid1_MouseUp(sender:
System.Object; e: System.Windows.Forms.MouseEventArgs);
begin
vitem_grid :=
self.DataGrid1.Item[self.DataGrid1.CurrentRowIndex,0].ToString;
self.Btnok.Enabled := True;
end;
class function Tbook_search.book_v_recno: Integer;
begin
result := v_recno;
end;
class function Tbook_search.book_findedit_Flag: string;
begin
result := findedit_Flag;
end;
procedure Tbook_search.Btnok_Click(sender: System.Object;
e: System.EventArgs);
begin
sub_select;
findedit_Flag := 'ok';
if (book_note.Tbook_note.booknote_type = 'find') then
begin
close;
book_note.Tbook_note.ActiveForm.Refresh;
book_note.Tbook_note.ActiveForm.Activate;
end;
if (book_note.Tbook_note.booknote_type = 'edit') then
begin
close;
book_note.Tbook_note.ActiveForm.Refresh;
book_note.Tbook_note.ActiveForm.Activate;
end;
end;
procedure Tbook_search.Btncancel_Click(sender:
System.Object; e: System.EventArgs);
begin
findedit_Flag := 'cancel';
close;
book_note.Tbook_note.ActiveForm.Refresh;
book_note.Tbook_note.ActiveForm.Activate;
end;
end. | | |
|
|