Codes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

LIST BOX

Adding from txtbox to listbox


If txtname.text <> string.empty then

Listbox1.items.Add(txtname.text)

End if

Deleting selections from listbox


Listbox1.items.removeAt(listbox1.selectedindex)

Listbox1.items.remove ("))‫)"االختيار نفسه(( القيمة‬

‫حذف بالرتبة وليست القيمة مثل‬

Remove

Deleting All from listbox


Listbox1.items.clear()

Counting All items in listbox


Msgbox("items Count: "& listbox1.items.count)

Sorting Alpha. All in listbox


Listbox1.sorted=true
Combo BOX
SELECTED INDEX CHANGED

Adding items to combobox


1-Cbx1.items.add("the name required")

2-Cbx1.items.addrange()

Counting All items in combobox


Msgbox("items Count: "& cbx1.items.count)

Deleting selections from combobox


1-cbx1.items.removeAt(0)

2-cbx1.items.removeAt(cbx1.selectedindex)

3-cbx1.items.remove ("))‫)"االختيار نفسه(( القيمة‬

‫حذف بالرتبة وليست القيمة مثل‬

Remove

Deleting All from combobox


cbx1.items.clear()

SELECTED INDEX CHANGED

Dim index As integer=cbx1.selectedindex

If index=0 then

ACTION 1 >>>> example……> Lstv1.view=view.large icons

Else if index=1 then

ACTION 2 >>>> example……> Lstv1.view=view.small icons

………………………..>
ListView
Form_Load

Setting view of listview


Lstv1.view=view.details

Lstv1.Gridlines=True

Lstv1.checkboxes=True

Adding columns to listview


Lstv1.columns.Add("id",100) ……….> width=100

Lstv1.columns.Add("name",250)

Lstv1.columns.Add("job",400)

………………………..>

Adding items(Lines) to listview


DIM lv1 AS new ListViewItem("0001",0) ……….> pic=0 index

Lv1.subitems.Add("ahmed")

Lv1.subitems.Add("chemist")

DIM lv2 AS new ListViewItem("0002")

Lv1.subitems.Add("ali")

Lv1.subitems.Add("che2t")

………………………..>

1 - Lstv1.items.Add(lv1)

Lstv1.items.Add(lv2)

FOR TOO MUCH ITEMS …….

2 - Lstv1.items.AddRange(New ListViewItem(){lv1,lv2,lv3,……})

3- Lstv1.items.Add("0004",1pic)
Lstv1.items(3).subitems.Add("magdy")

Lstv1.items(3).subitems.Add("engineer")

FOR CHECKEDBOXES
For Each item AS ListViewItem In Lstv1.checkeditems

Msgbox(Item.subitems(0))

Msgbox(String.format("ID is: (0), Name is: (1),……….>",item.subitems(0).text,


item.subitems(1).text……..>)

Next

Deleting selections from combobox


For Each item AS ListViewItem In Lstv1.checkeditems

OR

For Each item AS ListViewItem In Lstv1.selecteditems

Lstv1.items.removeAt(item.index)

Next

GROUPING by button click


Lstv1.view=view.details

Lstv1.Gridlines=True

Lstv1.groups.Add("Group1","Customers Gr")

Lstv1.groups.Add("Group2","Supp Gr")

………….>

Lstv1.items(0).group=lstv1.groups(0)

Lstv1.items(1).group=lstv1.groups(1)

Lstv1.items(2).group=lstv1.groups(0)

………….>
Reading Data from SQL tables in listview
After setting connection

Dim qry as string="SELECT * FROM tablename"

Dim cmd as new sqlcommand(qry,conn)

Dim reader as sqldatareader=cmd.executereader

Adding columns to listview

Lstv1.columns.Add("id",100) ……….> width=100

Lstv1.columns.Add("name",250)

Lstv1.columns.Add("job",400)

………………………..>

Lstv1.columns(0).width = 100

Lstv1.columns(1).width = 300

………………………..>

Adding items(Lines) to listview

WHILE reader.read

Dim item as new listviewitem(reader("ID").tostring())

Item.subitems.Add(reader("name"))

Item.subitems.Add(reader("job"))

Item.subitems.Add(reader("notes"))

Lstv1.items.Add(item)

END WHILE

Reader.close()
ADDING (INSERTING) Data to SQL tables from input form
After connection

TRY

Dim insert as string="INSERT INTO tblname (id,name,job,……………)" _

& "VALUES("&txt1.text&",'"& txt2.text &"',………..)"

FOR Date….> '"& txt4.value.tostring("yyyy-MM-dd") &"'

Dim cmd as new sqlcommand(insert,conn)

Conn.open()

Cmd.executenonquery()

Msgbox("SUCCESS",msgboxstyle.information,"Adding Data")

CATCH ex AS EXCEPTION

Msgbox(ex.message)

FINALLY

Conn.close()

END TRY

DELETING Data from SQL tables by form


TRY

Dim qry as string="SELECT * FROM tblname WHERE ID=" & txt1,text

Dim cmd as new sqlcommand(qry,conn)

Conn.open()

Dim dr AS new sqldatareader= Cmd.executereader()

dr.read()

if dr.HasRows THEN

txt2.text=dr(1)

txt3.text=dr(2)

FOR Date….> txt4.value=dr(4)

………………………..>
IF Msgbox("Are You Sure DELETE??? ",msgboxstyle.YesNo,"deleting")=dialogResult.Yes THEN

Dim delete as string="DELETE FROM tblname WHERE ID=" & txt1,text

Dr.close()

Cmd=new sqlcommand(delete,conn)

Cmd.executenonquery

Msgbox("DELETE DONE ",msgboxstyle.information,"deleting")

ELSE

Msgbox("CANCELED ",msgboxstyle.information,"CANCEL")

ELSE

Msgbox("No Result ",msgboxstyle.Exclamation,"deleting")

CATCH ex AS EXCEPTION

Msgbox(ex.message)

FINALLY

Conn.close()

END TRY

EDITING Data in SQL tables by form


On id cell event ………………. VALIDATED

TRY

Dim qry as string="SELECT * FROM tblname WHERE ID=" & txt1,text

Dim cmd as new sqlcommand(qry,conn)

Conn.open()

Dim dr AS new sqldatareader= Cmd.executereader

dr.read()

IF dr.HasRows THEN

txt2.text=dr(1)

txt3.text=dr(2)
FOR Date….> txt4.value=dr(4)

………………………..>

Btnadd.enabled=false

ELSE

Btnadd.enabled=True

All txt134.clear()

forDATE..>txt4.resettext()

END IF

On id button event ………………. EDIT_CLICK

IF Msgbox("Are You Sure EDIT??? ",msgboxstyle.YesNo,"deleting")=dialogResult.Yes THEN

Dim EDIT as string="UPDATE tblname SET name='" & txt2.text &"',job='"& txt2.text&"',………,
'"& txt4.value.tostring("yyyy-MM-dd") &"' WHERE ID=" & txt1,text "

conn.open()

Cmd=new sqlcommand(EDIT,conn)

Cmd.executenonquery

Msgbox("EDIT DONE ",msgboxstyle.information,"EDITING")

ELSE

Msgbox("CANCELED ",msgboxstyle.information,"CANCEL")

END IF

CATCH ex AS EXCEPTION

Msgbox(ex.message)

FINALLY

Conn.close()

END TRY
DATA GRID View
‫الوضع المنفصل عبارة عن‬
‫داتا ادابتر‬ ‫جلب بيانات بواسطة‬ -1
‫تخزينها في الذاكرة في صورة‬ -2

‫عبارة عن جدول واحد فقط‬... ‫تحتوي علي مجموعة جداول === او داتا تيبل‬... ‫داتا سيت‬

‫داتا جريد فيو او ليست فيو‬ ‫ثم عرضها في‬ -3

SEARCHING Data from SQL tables into DataGridView


IN CLASS

After connection

Dim adapter as new sqldatadapter

Dim ds as new dataset

FORM_LOAD

1 - Dim QRY as string="SELECT * FROM tblname"

2 - Dim QRY as string="SELECT ID as ‫ المعرف‬, Name as ‫ االسم‬, …………….> FROM tblname"

‫ ' ' >>>> لو االسم العربي من كلمتين يوضع بين‬OR { }

Adapter=new sqldataadapter(qry,conn)

Adapter.fill(ds,"fake tblname")

1 - DGV1.Datasource=ds.tables("fake tblname ")

2 - DGV1.Datasource=ds.tables(0) ‫لوجود جدول واحد فقط نستخدم الرتبة بدل االسم اللي هي صفر‬

‫داتا تيبل‬ ‫تخزينها في الذاكرة في صورة‬

Dim dt as new datatable

Adapter.fill(dt)

DGV1.Datasource=dt

‫نظرا الستخدام جدول واحد فقط فالتعديل يعطي نفس النتيجة‬


ADDING Data to SQL tables and DataGridView
IN CLASS

After connection

Dim adapter as new sqldatadapter

Dim dt as new datatable

Dim cmdb as new sqlcommandBuilder

BtnAdd_CLICK

Dim row as datarow = dt.newrow‫ثم اعطاء تفاصيله باالسفل‬ .... ‫انشاء سطر فارغ‬............

Row(0) = txt1.text

Row(1) = txt2.text

……………….>>

Dt.rows.Add(row)

Cmdb = new sqlcommandBuilder(adapter)

Adapter.update(dt)

Msgbox("Added Successfully", msgboxstyle.information, "ADDING")

DELETING Data from SQL tables and DataGridView


IN CLASS

After connection

Dim adapter as new sqldatadapter

Dim dt as new datatable

FORM_LOAD

1 - Dim QRY as string="SELECT * FROM tblname"

2 - Dim QRY as string="SELECT ID as ‫ المعرف‬, Name as ‫ االسم‬, …………….> FROM tblname"

Adapter=new sqldataadapter(qry,conn)

Adapter.fill(dt)

DGV1.Datasource=dt
‫لتعريف الحقل الرئيسي البرايمري كي‬

Dt.constraints.Add("Primary",dt.columns(0), true) OR

Dt.constraints.Add("Primary",dt.columns("‫)"المعرف‬, true)

btnDELETE_CLICK

Dim row as datarow = dt.rows.find(txtid.text)

IF row IS NOTHING THEN

Msgbox("Not FOUND", msgboxstyle.exclamation, "DELETING")

ELSE

Row.delete()

Cmdb= new sqlcommqndbuilder(adapter)

Adapter.update(dt)

Msgbox("DELETED", msgboxstyle.exclamation, "DELETING")

END IF

UPDATING Data in SQL tables and DataGridView

Dim OLDID as integer

On id cell event ………………. VALIDATED

Dim row as datarow = dt.rows.find(txtid.text)

IF row IS NOT NOTHING THEN

OLDID = row(0)

Txt2.text = row(1)

Txt3.text = row(2)

………………………>>

Msgbox("Not FOUND", msgboxstyle.exclamation, "DELETING")

END IF

btnUPDATE_CLICK
Dim row as datarow = dt.rows.find(OLDID)

Row(0) = txt1.text

Row(1) = txt2.text

………………………>>

Cmdb= new sqlcommqndbuilder(adapter)

Adapter.update(dt)

Msgbox("Updated", msgboxstyle.information, "UpdatING")

SEARCHING / Filtering / Sorting Data in SQL tables and DataGridView

USING DATAVIEW tool for searching in DataTable


Adding textbox for searching

Btn_search_CLICK

Dim dv as dataview = dt.defaultview

Sorting
Dv.sort = " ID desc"

Dv.sort = " ID asc"

Filtering
Dv.rowfilter = " ID > 2"

Dv.rowfilter = " ID =" &txtsearch.text

Dv.rowfilter = " name like '%" & txtsearch.text & "%'"

Dv.rowfilter = " name like '%" & txtsearch.text & "%' OR job like '%" & txtsearch.text & "%'"

Dv.rowfilter = " ID+name+job……….>> like '%" & txtsearch.text & "%' "

DGV1.datasource = dv
Combo BOX-again
SELECTED INDEX CHANGED

Adding items to combobox


1-Cbx1.items.add("the name required")

2-Cbx1.items.addrange()

Counting All items in combobox


Msgbox("items Count: "& cbx1.items.count)

Deleting selections from combobox


1-cbx1.items.removeAt(0)

2-cbx1.items.removeAt(cbx1.selectedindex)

3-cbx1.items.remove ("))‫)"االختيار نفسه(( القيمة‬

‫حذف بالرتبة وليست القيمة مثل‬

Remove

Deleting All from combobox


cbx1.items.clear()

SELECTED INDEX CHANGED

Dim index As integer=cbx1.selectedindex

If index=0 then

ACTION 1 >>>> example……> Lstv1.view=view.large icons

Else if index=1 then

ACTION 2 >>>> example……> Lstv1.view=view.small icons

………………………..>
MAIN CLASS

After connection

Dim adapter as sqldataadapter

Dim dt as new datatable

FORM_LOAD

Adapter=new sqldataadapter("SELECT * FROM tblname",conn)

Adapter.fill(dt)

Cbx1.datasource = dt

Cbx1.displaymember = "name"

Cbx1.valuemember = "ID"

SELECTED INDEX CHANGED

TRY

‫ تم عمل نسخة اخري من الداتا تيبل‬.. ‫لحدوث تعارض بين البيانات في الكومبو وااللليبل‬

Dim dt2 as datatable = dt.copy

DIM dv as dataview = dt2.defaultview

dv.rowfilter = "ID=" & cbx1.selectedvalue

‫اظهار البيانات الخاصة بالعنصر اللي تم اختياره من القائمة‬

lblname.text = dv.item(0)(1)

lbljob.text = dv.item(0)(2)

…………………….>> OR

Dim row() as datarow = dt.select("ID=" & cbx1.selectedvalue)

lblname.text = row(0)(1)

lbljob.text = row(0)(2)

…………………….>>

CATCH ex AS EXCEPTION

EXIT sub

END TRY
Adding items to combobox according to ANOTHER COMBOBOX

CBX1 , CBX2 , DGV1


Private Sub FrmAddUser_KeyDown(sender As Object, e As KeyEventArgs) Handles
Me.KeyDown

If e.KeyCode = Keys.Enter Then

Me.SelectNextControl(Me.ActiveControl, True, True, False, True)

End If

End Sub

Form property must be:

Key preview=true
VR_ItemsMovement
SELECT dbo.buy_d.item_detail_code, dbo.buy.buy_date AS DDatee, dbo.buy_d.qty AS
QQty, dbo.supplier.supplier_name AS PersonName, 1 AS A_TypeID, '‫ 'مشتريات‬AS
A_TypeName

FROM dbo.buy INNER JOIN

dbo.buy_d ON dbo.buy.buy_code = dbo.buy_d.buy_code INNER JOIN

dbo.supplier ON dbo.buy.supplier_code = dbo.supplier.supplier_code

UNION ALL

SELECT dbo.sales_return_d.item_detail_code, dbo.sales_return.sales_return_date,


dbo.sales_return_d.qty, dbo.customer.customer_name, 2 AS Expr1, '‫ 'مرتجع مبيعات‬AS Expr2

FROM dbo.sales_return INNER JOIN

dbo.sales_return_d ON dbo.sales_return.sales_return_code =
dbo.sales_return_d.sales_return_code INNER JOIN

dbo.customer ON dbo.sales_return.customer_code =
dbo.customer.customer_code

UNION ALL

SELECT dbo.invoice_d.item_detail_code, dbo.invoice.inv_date, dbo.invoice_d.qty,


dbo.customer.customer_name, 3 AS Expr1, '‫ 'مبيعات‬AS Expr2

FROM dbo.invoice INNER JOIN

dbo.invoice_d ON dbo.invoice.invoice_code = dbo.invoice_d.invoice_code


INNER JOIN

dbo.customer ON dbo.invoice.customer_code = dbo.customer.customer_code

UNION ALL

SELECT dbo.buy_return_d.item_detail_code, dbo.buy_return.buy_date,


dbo.buy_return_d.qty, dbo.supplier.supplier_name, 4 AS Expr1, '‫ 'مرتجع للمورد‬AS Expr2

FROM dbo.buy_return INNER JOIN

dbo.buy_return_d ON dbo.buy_return.buy_return_code =
dbo.buy_return_d.buy_return_code INNER JOIN

dbo.supplier ON dbo.buy_return.supplier_code = dbo.supplier.supplier_code

UNION ALL
SELECT dbo.mortal_d.item_detail_code, dbo.mortal.mortal_date, dbo.mortal_d.qty,
dbo.mortal.mortal_name, 5 AS Expr1, '‫ 'هالك‬AS Expr2

FROM dbo.mortal INNER JOIN

dbo.mortal_d ON dbo.mortal.mortal_id = dbo.mortal_d.mortal_id

UNION ALL

SELECT dbo.hagz_d.item_detail_code, dbo.Sarf.sarf_date, dbo.Sarf.qty,


dbo.customer.customer_name, 6 AS A_TypeID, '‫ 'صرف للعميل‬AS A_TypeName

FROM dbo.Sarf INNER JOIN

dbo.hagz_d ON dbo.Sarf.hagz_d_code = dbo.hagz_d.hagz_d_code INNER JOIN

dbo.hagz_m ON dbo.hagz_d.hagz_code = dbo.hagz_m.hagz_code INNER JOIN

dbo.customer ON dbo.hagz_m.customer_code = dbo.customer.customer_code

You might also like