|
The
Form design, book_search.cs
Create Visual C# 2010 Forn - book_search.cs
Classes used in this Form: 1- Label,
Panel, ComboBox, Button, PictureBox, CheckBox,
TextBox, DataGrid, ImageList 2- Logical
Classe - DbConnection
Note: Logical Connection info
:
Data source =
C:\work_VCNet10\work_VCNet10\data\work_VCnet.mdb
Create the Form
book_search.cs
Add New Item -
book_search.cs Form to work_VCNet10
Project
- On the Projet
menu , click Add New Item ...,
Add New Item - work_VCNet10 dialog box appears, Select
Windows form Icon,
in the Name box type
book_search.cs and then click
Add.
|
 |
|
Note:
In the Windows Forms Designer appears the
design form created and in the Properties
Windows displays the corresponding properties
and
The design Form book_search.cs created, appears the empty window Form
design |
|
The codes file, created
... |
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
namespace
work_VCNet10
{
public
partial
class
book_search :
Form
{
public
book_search()
{
InitializeComponent();
}
private
void book_search_Load(object
sender,
EventArgs
e)
{
}
}
}
|
|
|
|
|
1. |
Picture files... |
|
- The folder
c:\work_VCNet10\work_VCNet10\pic stored the
bmp file search1.bmp,
v_line6.bmp, btn_cancel.bmp,
btn_ok.bmp
|
2. |
The
book_search.cs design |
|
 |
The Properties of
the Form -
book_search.cs... |
Name: Icon: Maximize: Menu: Size: Start
Position: Text: Window
State: |
book_search Icon False (none) 368,332 CenterScreen book_search
... Normal | | | |
3. |
From the Toolbox/Windows Form add these
controls .... 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, Label
Properties
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,
Paneldetail
Properties
Name: Size: |
Panel11 656,
368 | |
- Panel112
Size:
152,
304
- Panel113
Size:
168,
24
- Panel114
Size:
656,
32
|
- Add
5 ComboBox
controls
- cmbname, ComboBox
Properties
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, Button
Properties
Name: Image: Size: Text: |
Btnsearch System.Drawing.Bitmap 24,
72 | |
The
folder
c:\work_VCNet10\work_VCNet10\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, PictureBox
Properties
Name: Image: Size
Mode: |
Pic_line011 System.Drawing.Bitmap Autosize | |
Bitmap
file added = C:\work_VCNet10\work_VCNet10\pic\v_line6.bmp
- Pic_line012 -
Bitmap
file added = C:\work_VCNet10\work_VCNet10\pic\v_line6.bmp
- Pic_line013 -
Bitmap
file added = C:\work_VCNet10\work_VCNet10\pic\v_line6.bmp
|
- Add
2 CheckBox
controls
- chk_allbook, CheckBox
Properties
Name: Size: |
chk_allbook 16,
16 | |
-
chk_condition
Size:
16,
16 |
- Add
2 TextBox
controls
- txtsearch1, TextBox
Properties
Name: Size: Text: |
txtsearch1 18,
114 SEARCH | |
- txtphone
Size: 132,
20
|
- Add
the DataGrid
control
- DataGrid1,
DataGrid Properties
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, DataGrid
Properties
Name: Size: TableStyles: |
DataGrid1 440,
328 (Collection) - of 5
columns
... | |
- Add
the ImageList
control
-
ImageList1,
ImageList
Properties
Name: Images: ImageSize: |
ImageList1 (Collection)
- of 2
Bitmap
image 56,
36 | |
The folder
c:\work_VCNet10\work_VCNet10\pic stored
the 2 bmp files btn_cancel.bmp,
btn_ok.bmp | | |
|
book_search.cs file, the codes after modification
...
|
|
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using System.Data;
using
System.Drawing;
using System.Linq;
using System.Text;
using
System.Windows.Forms;
namespace
work_VCnet10
{
public
partial
class
book_search
:
Form
{
//variable
//connection
System.Data.OleDb. OleDbConnection
DbConnection1 =
new
System.Data.OleDb.OleDbConnection();
//Select command
System.Data.OleDb. OleDbCommand
vselect;
//Dataadapter
System.Data.OleDb. OleDbDataAdapter
vda;
System.Data. DataSet
vdset;
string vitem_grid;
bool searchflg;
bool s_flag;
// public variables
public
class
book_search_cl
{
public
static
int
book_recno;
public
static
string findedit_Flag;
}
public book_search()
{
InitializeComponent();
}
private
void
book_search_Load(object
sender,
EventArgs
e)
{
int vleft;
int vtop;
book_note Fbook_note
=
new
book_note();
this.DbConnection1.ConnectionString
=
@"Provider=""Microsoft.Jet.OLEDB.4.0"";Data
Source=""C:\work_VCnet10\work_VCnet10\data\work_VCnet.mdb"";Persist
Security Info=False;User ID=Admin;Mode=Share Deny None;Jet
OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=1;Jet
OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk
Transactions=1;Jet OLEDB:Create System Database=False;Jet
OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale
on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet
OLEDB:SFP=False";
// Move
vleft = work_VCnet10. book_note.book_note_cl.booknote_left
+ work_VCnet10.book_note.book_note_cl.booknote_width
-
this.Width;
vtop = work_VCnet10. book_note.book_note_cl.booknote_top;
this.Location =
new
System.Drawing.Point(vleft,
vtop);
//resize
this.Btnsearch.Left
= (this.Panel113.Width
-
this.Btnsearch.Width)
/ 2;
this.Btnok.Top = (this.Panel114.Height
-
this.Btnok.Height) /
2;
this.Btncancel.Top =
this.Btnok.Top;
this.Pic_line011.Top
=
this.Btnok.Top + ((this.Btnok.Height
- (this.Pic_line011.Height
* 3)) / 4);
this.Pic_line012.Top
=
this.Pic_line011.Top
+
this.Pic_line011.Height
+ ((this.Btnok.Height
- (this.Pic_line011.Height
* 3)) / 4);
this.Pic_line013.Top
=
this.Pic_line012.Top
+
this.Pic_line012.Height
+ ((this.Btnok.Height
- (this.Pic_line011.Height
* 3)) / 4);
this.Pic_line011.Left
= (this.Btnok.Left
-
this.Pic_line011.Width)
/ 2;
this.Pic_line012.Left
=
this.Pic_line011.Left;
this.Pic_line013.Left
=
this.Pic_line011.Left;
s_flag =
false;
searchflg =
false;
sub_clearall();
switch(work_VCnet10.book_note.book_note_cl.booknote_type)
{
case
"find":
this.Text =
"search,
book note ...";
break;
case
"edit":
this.Text =
"edit, book
note ...";
break;
}
this.chk_allbook.Checked
=
true;
sub_load( "name");
sub_load( "first");
sub_load( "father");
sub_load( "city");
sub_search( "",
"",
"",
"",
"",
"");
}
//add data to comboboxes
private
void
sub_load(string
v_field)
{
int i;
switch(v_field)
{
case
"name":
vselect =
new
System.Data.OleDb.OleDbCommand("SELECT
DISTINCT Trim(name) AS vname FROM book WHERE (Trim(name)
<> '') ORDER BY Trim(name)");
break;
case
"first":
vselect =
new
System.Data.OleDb.OleDbCommand("SELECT
DISTINCT Trim(first_name) AS vfirst FROM book WHERE (Trim(first_name)
<>'')ORDER BY Trim(first_name)");
break;
case
"father":
vselect =
new
System.Data.OleDb.OleDbCommand("SELECT
DISTINCT Trim(father_name) AS vfather FROM book WHERE (Trim(father_name)
<>'')ORDER BY Trim(father_name)");
break;
case
"city":
vselect =
new
System.Data.OleDb.OleDbCommand("SELECT
DISTINCT Trim(city) AS vcity FROM book WHERE (Trim(city)<>'')ORDER
BY Trim(city)");
break;
}
vda =
new
System.Data.OleDb.OleDbDataAdapter(vselect);
vdset =
new
System.Data.DataSet();
vselect.Connection = DbConnection1;
vdset.EnforceConstraints =
false;
try
{
DbConnection1.Open();
vda.Fill(vdset,
"book");
}
catch (System.Exception
fillException)
{
System.Windows.Forms. MessageBox.Show(fillException.Message);
}
vdset.EnforceConstraints =
true;
DbConnection1.Close();
if (vdset.Tables["book"].Rows.Count
> 0)
{
for (i = 0 ; i <= (vdset.Tables["book"].Rows.Count
- 1); i++)
{
switch(v_field)
{
case
"name":
this.cmbname.Items.Add(vdset.Tables["book"].Rows[i]["vname"].ToString().Trim());
break;
case
"first":
this.cmbfirst.Items.Add(vdset.Tables["book"].Rows[i]["vfirst"].ToString().Trim());
break;
case
"father":
this.cmbfather.Items.Add(vdset.Tables["book"].Rows[i]["vfather"].ToString().Trim());
break;
case
"city":
this.cmbcity.Items.Add(vdset.Tables["book"].Rows[i]["vcity"].ToString().Trim());
break;
}
}
}
}
private
void
cmbname_SelectedIndexChanged(object
sender,
EventArgs
e)
{
if (s_flag ==
true)
sub_check();
}
private
void
sub_check()
{
if (this.chk_allbook.Checked
==
true)
this.chk_allbook.Checked
=
false;
if (this.Btnsearch.Enabled
==
false)
this.Btnsearch.Enabled
=
true;
}
private
void
cmbfirst_SelectedIndexChanged(object
sender,
EventArgs
e)
{
if (s_flag ==
true)
sub_check();
}
private
void
cmbfather_SelectedIndexChanged(object
sender,
EventArgs
e)
{
if (s_flag ==
true)
sub_check();
}
private
void
cmbcountry_SelectedIndexChanged(object
sender,
EventArgs
e)
{
if (s_flag ==
true)
sub_check();
}
private
void
cmbcity_SelectedIndexChanged(object
sender,
EventArgs
e)
{
if (s_flag ==
true)
sub_check();
}
private
void
txtphone_KeyPress(object
sender, System.Windows.Forms.KeyPressEventArgs
e)
{
if (s_flag ==
true)
sub_check();
}
private
void
chk_condition_CheckedChanged(object
sender,
EventArgs
e)
{
if (this.chk_condition.Checked
==
true)
this.lcondition.Text
=
"condition - Or";
if (this.chk_condition.Checked
==
false)
this.lcondition.Text
=
"condition - And";
}
private
void
chk_allbook_CheckedChanged(object
sender,
EventArgs
e)
{
if ((s_flag ==
true)
&& (this.chk_allbook.Checked
==
true))
{
if (this.Btnsearch.Enabled
==
false)
this.Btnsearch.Enabled
=
true;
}
}
private
void
btnclear_Click(object
sender,
EventArgs
e)
{
sub_clearall();
}
private
void
sub_clearall()
{
this.cmbname.SelectedIndex
= -1;
this.cmbfirst.SelectedIndex
= -1;
this.cmbfather.SelectedIndex
= -1;
this.cmbcountry.SelectedIndex
= -1;
this.cmbcity.SelectedIndex
= -1;
this.txtphone.Text =
"";
if (this.Btnsearch.Enabled
==
true)
this.Btnsearch.Enabled
=
false;
}
private
void
Btnsearch_Click(object
sender,
EventArgs
e)
{
searchflg =
true;
switch(this.chk_allbook.Checked)
{
case
true:
sub_search( "",
"",
"",
"",
"",
"");
break;
case
false:
sub_search( this.cmbname.Text.Trim(),
this.cmbfirst.Text.Trim(),
this.cmbfather.Text.Trim(),
this.cmbcountry.Text.Trim(),
this.cmbcity.Text.Trim(),
this.txtphone.Text.Trim());
break;
}
}
private
void
sub_search(string
v_name,
string
v_firstname,
string
v_fathername,
string
v_country,
string
v_city,
string
v_phone)
{
int vfound;
switch(this.chk_allbook.Checked)
{
case
true:
vselect =
new
System.Data.OleDb.OleDbCommand("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");
break;
case
false:
vselect =
new
System.Data.OleDb.OleDbCommand("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");
break;
}
vda =
new
System.Data.OleDb.OleDbDataAdapter(vselect);
vdset =
new
System.Data.DataSet();
vselect.Connection = DbConnection1;
vdset.EnforceConstraints =
false;
try
{
DbConnection1.Open();
vda.Fill(vdset,
"book");
}
catch (System.Exception
fillException)
{
System.Windows.Forms. MessageBox.Show(fillException.Message);
}
vdset.EnforceConstraints =
true;
DbConnection1.Close();
vfound = vdset.Tables[ "book"].Rows.Count;
this.DataGrid1.DataSource
= vdset;
this.DataGrid1.DataMember
=
"book";
if (searchflg ==
true)
{
this.Btnsearch.Enabled
=
true;
this.btnclear_Click(btnclear,
null);
searchflg =
false;
}
switch(this.chk_allbook.Checked)
{
case
true:
this.lbfound.Text =
vfound.ToString() +
" - item(s)
found, selected: all book(s)";
break;
case
false:
this.lbfound.Text =
vfound.ToString() +
" - item(s)
found";
break;
}
s_flag =
true;
this.lbfound.Focus();
}
private
void
sub_select()
{
int vvrec;
vselect =
new
System.Data.OleDb.OleDbCommand("SELECT
bookid, country, father_name, first_name, name, sex FROM
book WHERE (name <> '') ORDER BY bookid");
vda =
new
System.Data.OleDb.OleDbDataAdapter(vselect);
vdset =
new
System.Data.DataSet();
vselect.Connection = DbConnection1;
vdset.EnforceConstraints =
false;
try
{
DbConnection1.Open();
vda.Fill(vdset,
"book");
}
catch (System.Exception
fillException)
{
System.Windows.Forms. MessageBox.Show(fillException.Message);
}
vdset.EnforceConstraints =
true;
DbConnection1.Close();
if (vdset.Tables["book"].Rows.Count
> 0)
{
for (vvrec = 0 ;
vvrec <= (vdset.Tables["book"].Rows.Count
- 1); vvrec++)
{
if (vdset.Tables["book"].Rows[vvrec]["bookid"].ToString().Trim()
== vitem_grid.Trim())
{
book_search_cl.book_recno
= vvrec;
}
}
}
}
private
void
DataGrid1_Click(object
sender,
EventArgs
e)
{
vitem_grid =
this.DataGrid1[this.DataGrid1.CurrentRowIndex,
0].ToString();
this.Btnok.Enabled =
true;
}
private
void
DataGrid1_MouseUp(object
sender, System.Windows.Forms.MouseEventArgs
e)
{
vitem_grid =
this.DataGrid1[this.DataGrid1.CurrentRowIndex,
0].ToString();
this.Btnok.Enabled =
true;
}
private
void
Btnok_Click(object
sender,
EventArgs
e)
{
sub_select();
book_search_cl.findedit_Flag
=
"ok";
switch(work_VCnet10.book_note.book_note_cl.booknote_type)
{
case
"find":
this.Close();
work_VCnet10. book_note.ActiveForm.Refresh();
work_VCnet10. book_note.ActiveForm.Activate();
//fbooknote.sub_findok();
break;
case
"edit":
this.Close();
work_VCnet10. book_note.ActiveForm.Refresh();
work_VCnet10. book_note.ActiveForm.Activate();
//fbooknote.sub_modifyok();
break;
}
}
private
void
Btncancel_Click(object
sender,
EventArgs
e)
{
book_search_cl.findedit_Flag
=
"cancel";
this.Close();
work_VCnet10. book_note.ActiveForm.Refresh();
work_VCnet10. book_note.ActiveForm.Activate();
}
}
}
| | |
|
|