درس برنامه سازی پیشرفته 2 visual basic.net

61
ه ت ف ر ش ی پ ی ه ساز م ا رن ب دزس2 Visual Basic.NET www.ali-arabi.com

Upload: baldwin-norman

Post on 03-Jan-2016

228 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: درس برنامه سازی پیشرفته 2 Visual Basic.NET

پیشرفته سازی برنامه درس

2

Visual Basic.NET

www.ali-arabi.com

Page 2: درس برنامه سازی پیشرفته 2 Visual Basic.NET

www.ali-arabi.com

غیبت ) 3 هر ازای به کالسی (0/5نمره کسر نمرهموقع ) ( 5 به تحویل کالسی تمرینات نمره

ترم 12 پایان عملی نمره

Page 3: درس برنامه سازی پیشرفته 2 Visual Basic.NET

www.ali-arabi.com

ها : سرفصل

تفاوتهای - از VB.Netو VBبرخی

های - برنامه VB.Netبه VB6تبدیل

توابع - با APIکار

ها، - آرایه For Eachو UBoundو LBoundآرایه و اولیه دهی مقدار و ) ازاشیاء ) ای آرایه و اجرا و طراحی زمان کنترلی آرایه و پویا های

به) اشاره (Typeofمختلف

، Arrayکالس - Array.Resize

اختیاری - متغییر (Optional)آرگومان طول با نامدار، ،(ParamArray) مقداری آرگومان، آخرین ارجاع (ByVal)بعنوان (ByRef)و

ها )’’’(XML documentation امکان- نوع و توابع (Structure)برای

Staticمتغییر -

Page 4: درس برنامه سازی پیشرفته 2 Visual Basic.NET

www.ali-arabi.com

-File System Object از استفاده Microsoft Scripting Runtime )COMباReference( وImports Scripting

-ImageList وMenus & Toolbars وListView وTreeView

-System.IO وFileIO.FileSystem وFileSystem

-Dialogs وRich TextBox وTab Control وToolTip

-Structure وEnum وCollection

-Files

و - مربوطه Printingگرافیک اشیاء و

,My )Computer.Screen, Application.Info, CommandLineArgsکالس -GetEnvironmentVariable, ..(, Math

-Err ).number, .Description( وDebug وExceptions

-Clipboard

داده : - کدنویسی – - Dataپایگاه و ویزارد مربوطه اشیاء ,SqlوAccess

-Reporting شامل مربوطه اشیاء Crystal Reportو

Page 5: درس برنامه سازی پیشرفته 2 Visual Basic.NET

www.ali-arabi.com

-Registry

و - ها Class Diagramکالس

امکان DLLتولید - <)ToolboxBitmap)imageFile>و

مثل - آماده فرمهای از برداری الگو و ,About Box, DialogاستفادهExplorer Form, Login Form, MDIParent Form, Splash Screen

-Web Application

-Mobile Programming

-Threading

-Setup سازی

Page 6: درس برنامه سازی پیشرفته 2 Visual Basic.NET

تفاوتهای از VB.Netو VBبرخی

نداریم- )1 فرض پیش (.Default Propertyخصوصیت

در مثال ، VBبعنوان Text1.text =“ali” ≡ Text1=“ali”

آنها- 2 نام از بعد باید ها، زیربرنامه و توابع فراخوانی هنگام)( . در .VBبگذاریم دارد وجود هم تناقضاتی مورد دراین

بر AndAlsoوجود- 3 بر OrElseو Andعالوه برای Orعالوه. شرط کامل بررسی یا لزوم اندازه به شرط بررسی

4: . همینطور- است اجباری متغییر تعریفDim x, y As Integer : Dim z As Integer = 10

از +=عملگر- 5 و ) Cکه است رسیده ارث *=و -=بهو . . . (:

S += n ≡ s = s + n

www.ali-arabi.com

Page 7: درس برنامه سازی پیشرفته 2 Visual Basic.NET

خالف VB.Netدر- 6 نحوه VBبر اگر سابروتین، و توابع درپارامترها کردن پاس یا فراخوانی

(Call By Reference & Call By Value )

فرض پیش بصورت نشود، گرفته ByValتعیین نظر درمیشود.

پایان- 7 با بلوک یک به میتواند متغییرها Loopیا Endتعریف)Nextیا شود محدود انواع(. ) Block level Scopeهم درون

و ... ( شروط و ها حلقه8 -End While بجایWend.

های- 9 . Optionalآرگومان دارند فرض پیش مقدار به نیاز ،(Optional Arguments Require a Default Value)

نمیتوانند- 10 ها، سابروتین و . Staticتوابع شوند تعریفبصورت آنها آنها Staticتعریف متغییرهای میشود باعث ،

Static. شوند www.ali-arabi.com

Page 8: درس برنامه سازی پیشرفته 2 Visual Basic.NET

دستور- 11 توسط توابع بازگشتی Returnمقدار . هم آنها نام توسط میتواند البته شود برگردانده

. شود برگرداندهمتد- 12 .Selectاز SetFocusبجای میشود استفاده13 . با- تعریف، امکان میشوند شروع صفر از آرایه ابعاد

. نیست پذیر امکان دیگری عدد از شروعAs Byte Dim a)3( As Byte ≡ Dim a)0 to 3(

14:) متفاوت- ) ابعاد با حتی ها آرایه انتساب امکانDim a)3( As Byte, b)3( As Byte

a)0( = 1 : a)3( = 6

b = a 'b = a.Clone)(

www.ali-arabi.com

Page 9: درس برنامه سازی پیشرفته 2 Visual Basic.NET

نوع Option Strict Onدستور- 15 تبدیل میشود، باعثپذیر امکان ، میشوند داده تخریب باعث که هایی داده

نوع. از متغییری انتساب مثال .Integerبه LongنباشدOption Strict On disallows implicit conversions from Long to

Integer.

. Objectمتغییرها- 16 کالس از کالس زیر .Objectهستند

Dim x As String

x = "Hello, World"

MsgBox)x.Length(

داده- 17 معادل Integerو Int16معادل ShortنوعInt32 و Long معادلInt64. است

www.ali-arabi.com

Page 10: درس برنامه سازی پیشرفته 2 Visual Basic.NET

به- 18 دیگر های داده :Stringتبدیل

Dim x As Integer = 1234567, y As String

y = x ‘ y = CStr)x( ‘ y = x.ToStringقبل . ) )Str)xالبته خالی فضای نیست فوق دستور سه معادل کامال

) مثبت عدد از

y = x.ToString)“0,0”( ‘ 1,234,567 ) سه رقم سه)رقم

ثابت- ) 19 طول با پشتیبانی ( Fixed-Length Stringsرشته. شد. خواهد داده توضیح متعاقبا رکوردها، مبحث در البته نمیشود

داده- 20 داده Variantنوع نوع اندازه Objectبا جایگزین 4با بایتذخیره آن در نظر مورد داده که ای حافظه آدرس که است شده

. میدارد نگه را شد، خواهدDim x ≡ Dim x as Object

21 -Structure بجایUDT)User Data Type( گرفته قرار رکورد یااست.

www.ali-arabi.com

Page 11: درس برنامه سازی پیشرفته 2 Visual Basic.NET

های برنامه VB.Netبه VB6تبدیل

توسط شده ایجاد گزینه VBپروژه از استفاده با OpenراProject… منوی درخواستی، Fileاز مراحل گذراندن و

. کنید تبدیل

دستورات از بعضی گزینه VBمعادل از میتوانید را Upgrade Visual Basic 6 Code…

منوی .Toolsاز بخواهید نویسی، کد حالت در البته

www.ali-arabi.com

Page 12: درس برنامه سازی پیشرفته 2 Visual Basic.NET

توابع با APIکار

های کاستی توابع VB6اغلب به نیاز باعث ویندوز APIکهدر شد، با VB.netمی کار توابع مثل است شده افزوده

کالس ) وتوابع( Microsoft.Win32.Registryرجیستریکالس ) کاراکترها نوع و کوچکی و بزرگی و( ...Charتشخیص

توابع با کار نحوه دیگر، موارد با . VB6مانند APIدر استداده نوع تعاریف، در که تفاوت و Integerبه Longاین

Integer بهShort . بجای که است بدیهی گردد Typeتبدیلاز باید .Structureهم، شود استفاده

www.ali-arabi.com

Page 13: درس برنامه سازی پیشرفته 2 Visual Basic.NET

ها آرایه

Dim n)10( As Integer 'Dim n)0 To 10( As IntegerDim m)1 To 10( As Integer ‘Error:Array lower bounds can be only '0‘MsgBox)LBound)n(( '0MsgBox)UBound)n(( '10MsgBox)n.Length( '11For i As Integer = n.GetLowerBound)0( To n.GetUpperBound)0( n)i( = INextFor Each x As Integer In n MsgBox)x(NextDim p)2, 3( As ByteDim x)( As Integer = {1, 2, 3, 4}Dim y),( As Byte = {{1, 2, 3}, {5, 6, 7}} 'm)0 to 1, 0 to 2(ReDim Preserve x)2( '{1, 2, 3}

www.ali-arabi.com

Page 14: درس برنامه سازی پیشرفته 2 Visual Basic.NET

طراحی زمان کنترلی آرایه

با کار .VB6شیوه است متفاوتفرم، روی نظر مورد کنترلهای یا اشیاء گذاشتن از پس

. کنیم( می انتخاب را آنها الفپنجره ( ابزار نوار از .Events،گزینه Propertiesب کنیم می انتخاب را

تایپ ( را مناسبی نام یا کنیم می کلیک دابل نظر مورد رویداد روی ج. کنیم می کلیک دابل آنگاه و کردهکنیم ( . ) می وارد را مربوطه کد (senderد است آرایه در نظر مورد عضو

رویداد همان افزودن با را کنترلها از یکی به مربوط رویداد میتوان البتهبسط روال آن عنوان انتهای در آن نوع هم رویداد هر حتی یا دیگر اشیاء

داد.Private Sub Button1_Click)ByVal sender As System.Object, ByVal e As

System.EventArgs( Handles Button1.Click, TextBox1.MouseHover MsgBox)sender.text(End Sub

www.ali-arabi.com

Page 15: درس برنامه سازی پیشرفته 2 Visual Basic.NET

اجرا زمان کنترلی آرایه

Private Sub Form1_Load)ByVal sender As Object, ByVal e As ... Dim btn)3( As Button For i As Byte = 0 To 3 btn)i( = New Button With btn)i( .Parent = Me .Top = i * .Height + 10 .Text = i AddHandler .Click, AddressOf btn_click End With Next End Sub Private Sub btn_click)ByVal s As Object, ByVal e As EventArgs( MsgBox)s.text( End Sub

www.ali-arabi.com

Page 16: درس برنامه سازی پیشرفته 2 Visual Basic.NET

مختلف اشیاء از ای آرایه

Dim b1 As New Button With {.Text = "b1", .Top = 10}Dim b2 As New Button With {.Text = "b2", .Top = 40}Dim t As New TextBox With {.Top = 70, .Text=“Test”}Dim a)2( As Objecta.SetValue)b1, 0(a.SetValue)b2, 1(a.SetValue)t, 2(For Each obj As Object In a

obj.Parent = Me If TypeOf obj Is Button Then MsgBox)obj.text( End IfNext

www.ali-arabi.com

Page 17: درس برنامه سازی پیشرفته 2 Visual Basic.NET

)... ( ، یک دکمه فشردن ، باشیم داشته فرم روی و متن کادر دکمه، شیئ تعدادی اینکه فرض با. نماید می آشکار را ها آن سه، دکمه و نهان را متن کادرهای ، دو دکمه و دهد می قرار آرایه در را آنها Dim obj)( As Object Private Sub Button1_Click)ByVal sender As System.Object, ByVal e As . . . ReDim obj)Me.Controls.Count( For index As Integer = 0 To Me.Controls.Count - 1 obj)index( = Me.Controls)index( Next End Sub Private Sub Button2_Click)ByVal sender As System.Object, ByVal e As . . . For index As Integer = 0 To Me.Controls.Count - 1 'If Not )obj)index( Is Button3( Then ‘ سه دکمه از غیر اشیاء همه If TypeOf obj)index( Is TextBox Then obj)index(.visible = False End If Next End Sub Private Sub Button3_Click)ByVal sender As System.Object, ByVal e As . . . For index As Integer = 0 To Me.Controls.Count - 1 obj)index(.visible = True Next End Sub

www.ali-arabi.com

Page 18: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Arrayکالس

اینجا . Arrayدستور VB6در در است آرایه اولیه دهی مقدار برای. : Arrayکالس آن امکانات از برخی است ها آرایه همه پایه کالس

Dim n)( As Integer = {10, 2, 3} Array.Resize)n, 5( ' {10, 2, 3, 0, 0} Array.Resize)n, 2( ' {10, 2} Array.Sort)n( '{2, 10} Array.Reverse)n( ' {10, 2} MsgBox)Array.IndexOf)n, 2(( ' 1 Dim m)( As Integer = {1, 3, 5, 7} Array.Copy)n, m, 1( ' m:{10, 3, 5, 7} فضای وجود صورت در

کافی

Dim a)( As Byte = {1, 2, 3, 4, 5} Dim b)( As Byte = {10, 20, 30, 40} Array.Copy)a, 1, b, 2, 2( ' b:{10, 20, 2, 3}

Dim c)6( As Byte a.CopyTo)c, 1( ' c:{0, 1, 2, 3, 4, 5, 0}www.ali-arabi.com

Page 19: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Function MyFunc)ByVal x As Byte, Optional ByVal y As Byte = 20, Optional ByVal z As Byte = 30( As Integer

Return x + y + z

End Function

Private Sub Button1_Click)ByVal sender As System.Object, ByVal e As System.EventArgs( Handles . . . MsgBox)MyFunc)1(( ‘51

MsgBox)MyFunc)1, 2(( ‘33

MsgBox)MyFunc)1, 2, 3(( ‘6

End Subدهی مقدار

اجباری

بعد های آرگومانآرگومان یک ازباید ، اختیاری

باشند اختیاری

: ها برنامه زیر و توابع های آرگومان

www.ali-arabi.com

اختیاری ( آرگومان الف(Optional):

Page 20: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Function MyFunc)ByVal x As Byte, ByVal y As Byte( As Single

Return x / y

End Function

Private Sub Button1_Click)ByVal sender As System.Object, . . .

MsgBox)MyFunc)20, 10(( ‘2

MsgBox)MyFunc)y:=10, x:=20(( ‘2

End Sub

www.ali-arabi.com

آرگومان ( بنامدار :

Page 21: درس برنامه سازی پیشرفته 2 Visual Basic.NET

www.ali-arabi.com

طول ( با آرگومان جمتغییر :

Function MyFunc)ByVal ParamArray n)( As Byte( As Integer

Dim s As Integer = 0

For index As Integer = 0 To n.Length - 1

s += n)index(

Next

Return s

End Function

Private Sub Button1_Click)ByVal sender As . . .

MsgBox)MyFunc)20, 10(( ‘30

MsgBox)MyFunc)1, 2, 3, 4(( ‘10

End Sub

Function MyFunc)ByVal ParamArray n)( As Byte( As Integer

Dim s As Integer = 0

For index As Integer = 0 To n.Length - 1

s += n)index(

Next

Return s

End Function

Private Sub Button1_Click)ByVal sender As . . .

MsgBox)MyFunc)20, 10(( ‘30

MsgBox)MyFunc)1, 2, 3, 4(( ‘10

End Sub

آخرین فقطآرگومان

Page 22: درس برنامه سازی پیشرفته 2 Visual Basic.NET

www.ali-arabi.com

ارجاعی ( و مقداری های آرگومان Call By Value, Call By)دReference):

Sub MySub)ByVal x As Byte, ByRef y As Byte(

x += 1

y += 1

MsgBox)x + y( ‘ 22

End Sub

Private Sub Button1_Click)ByVal sender As . . .

Dim a As Byte = 10, b As Byte = 10

MySub)a, b(

MsgBox)a( ‘ 10

MsgBox)b( ‘ 11

End Sub

Page 23: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Public Class Form1

''' <summary>

''' This Function Give 2 Number and Return Divide of them

''' </summary>

''' <param name="x"> مقسوم </param>

''' <param name="y"> علیه <param/>مقسوم

''' <returns> اعشاری عدد قالب در تقسیم <returns/> حاصل

''' <remarks> تقسیم محاسبه <remarks/> تابع

Function MyFunc)ByVal x As Integer, ByVal y As Integer( As Single

Dim s As Single = x / y

Return s

End Function

Private Sub Button1_Click)ByVal sender As System.Object, ByVal e . . .

Dim x As Single = MyFunc)10, 20(

End Sub

End Class

XML Documentation:

www.ali-arabi.com

Page 24: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Function updateSales)ByVal thisSale As Decimal( As Decimal

Static totalSales As Decimal = 0

totalSales += thisSale

Return totalSales

End Function

Private Sub Button1_Click)ByVal sender As . . .

MsgBox)updateSales)1000(( '1000

MsgBox)updateSales)2500(( '3500

End Sub

ایستا :(static)متغییر

www.ali-arabi.com

Page 25: درس برنامه سازی پیشرفته 2 Visual Basic.NET

TextBox1.Font.Size = 20 ) ( خواندنی فقط خطاTextBox1.Font = New Font)TextBox1.Font.Name, 20, TextBox1.Font.Style(

لسیت به سیستم روی شده نصب فونتهای تمام افزودنFor Each f As FontFamily In FontFamily.Families

ListBox1.Items.Add)f.Name(

Next

یا Dim fnt As New System.Drawing.Text.InstalledFontCollection

For Each f As FontFamily In fnt.Families

ListBox2.Items.Add)f.Name(

Next

: ها فونت با کار

www.ali-arabi.com

Page 26: درس برنامه سازی پیشرفته 2 Visual Basic.NET

File System Object: Imports ScriptingPublic Class Form1 Dim fso As New FileSystemObject Dim t As TextStream Private Sub Button1_Click)ByVal sender As System.Object, . . . Dim p As String = fso.BuildPath)"c:\windows", "a.txt"( ‘c:\windows\a.txt t = fso.CreateTextFile)"c:\a\f.txt"( t.Write)RichTextBox1.Text( t.Close)( For Each d As Drive In fso.Drives RichTextBox1.Text += d.DriveLetter & " " & d.DriveType.ToString)( & vbNewLine Next p = fso.GetAbsolutePathName)"c:\\windows/a.txt"( 'c:\windows\a.txt p = fso.GetBaseName)"c:\a\abc.txt"( 'abc Dim drv As Drive = fso.GetDrive)"c:\"( Dim f As File = fso.GetFile)"c:\a\f.txt"( MsgBox)f.Size & vbNewLine & f.Type(

Dim fldr As Folder = fso.GetFolder)"c:\"( For Each subfldr As Folder In fldr.SubFolders RichTextBox1.Text += subfldr.Name & vbNewLine Next MsgBox)fso.GetSpecialFolder)SpecialFolderConst.WindowsFolder(.Path( End Sub Private Sub Button2_Click)ByVal sender As System.Object, . . . t = fso.OpenTextFile)"c:\a\f.txt"( RichTextBox1.Text = t.ReadAll)( t.Close)( End SubEnd Class

ابتداProject Add Reference

<COM Tab>Microsoft Scripting Runtime

Page 27: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Private Sub Form1_Load)ByVal sender As System.Object, ByVal e As System.EventArgs( Handles MyBase.Load

ListView1.Columns.Add)"Name"(

ListView1.Columns.Add)"Ext"(

Dim lvi As ListViewItem = ListView1.Items.Add)"F3", 2(

lvi.SubItems.Add)"10"(

For v As View = View.LargeIcon To View.Tile

ComboBox1.Items.Add)v(

Next

Dim tn As TreeNode = TreeView1.Nodes.Add)"t1", "t1"(

Dim tn11 As TreeNode = tn.Nodes.Add)"t11"(

tn11.Nodes.Add)"t111"(

tn11.Nodes.Add)"t112"(

tn.Nodes.Add)"t12"(

TreeView1.Nodes.Add)"t2", "t2"(

TreeView1.Nodes.Add)"t3", "t3"(

TreeView1.ExpandAll)(

TreeView1.CheckBoxes = True

End Sub

Private Sub ListView1_Click)ByVal sender As Object, ByVal e As System.EventArgs( Handles ListView1.Click

If ListView1.SelectedItems.Count > 0 Then

MsgBox)ListView1.SelectedItems)0(.Text(

End If

End Sub

Private Sub TreeView1_AfterSelect)ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs( Handles TreeView1.AfterSelect

ListView1.Clear)(

For Each tn As TreeNode In TreeView1.SelectedNode.Nodes

ListView1.Items.Add)tn.Text(

Next

End Sub

ListView, TreeView :

www.ali-arabi.com

Page 28: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Private Sub Button1_Click)ByVal sender As System.Object, ByVal e As System.EventArgs( Handles Button1.Click

IO.Directory.CreateDirectory)"c:\b\c"( ‘ نباشد نظر مورد فولدر اگر حتی Dim d As System.IO.Directory

d.CreateDirectory)"c:\c"(

Dim d As New IO.DirectoryInfo)"c:\windows"(

MsgBox)d.CreationTime(

Dim x As Integer

For Each di As IO.DirectoryInfo In d.GetDirectories)(

x = di.Attributes And IO.FileAttributes.Hidden

If x = IO.FileAttributes.Hidden Then

ListBox1.Items.Add)di.FullName(

End If

Next

For Each f As IO.FileInfo In d.GetFiles)(

ListBox1.Items.Add)f.FullName(

Next

IO.File.Create)"c:\a\abc.txt"(

Dim file As New IO.FileStream)"c:\a\abc.txt", IO.FileMode.OpenOrCreate, IO.FileAccess.Write(

For i As Byte = 1 To 10

file.WriteByte)i(

Next

file.Close)(

End Sub

Private Sub Button2_Click)ByVal sender As System.Object, ByVal e As System.EventArgs( Handles Button2.Click

Dim file As New IO.FileStream)"c:\a\abc.txt", IO.FileMode.OpenOrCreate, IO.FileAccess.Read(

Dim i As Byte

While file.Position < file.Length

i = file.ReadByte)(

ListBox1.Items.Add)i(

End While

file.Close)(

End Sub

System.IO :

www.ali-arabi.com

Page 29: درس برنامه سازی پیشرفته 2 Visual Basic.NET

For Each fn As String In FileIO.FileSystem.FindInFiles)"c:\windows", "Class", True, FileIO.SearchOption.SearchTopLevelOnly, "*.txt", "*.log"(

ListBox1.Items.Add)fn(

Next

FileSystem.ChDir)"c:\windows\system32"(

MsgBox)FileSystem.CurDir)"c:"((

Dim fn As String = FileSystem.Dir)"c:\*.*", FileAttribute.Archive + FileAttribute.Hidden(

While fn <> ""

ListBox1.Items.Add)fn(

fn = FileSystem.Dir)(

End While

FileIO.FileSystem وFileSystem:

www.ali-arabi.com

Page 30: درس برنامه سازی پیشرفته 2 Visual Basic.NET

If ColorDialog1.ShowDialog)( = Windows.Forms.DialogResult.OK Then

TextBox1.BackColor = ColorDialog1.Color

End If

FolderBrowserDialog1.ShowNewFolderButton = False

FolderBrowserDialog1.Description = "Select A Folder ... "

If FolderBrowserDialog1.ShowDialog)( = Windows.Forms.DialogResult.OK Then

TextBox1.Text = FolderBrowserDialog1.SelectedPath)(

End If

Dim fntdlg As New FontDialog With {.ShowColor = True, .Font = RichTextBox1.SelectionFont}

fntdlg.Color = RichTextBox1.SelectionColor

If fntdlg.ShowDialog)( = Windows.Forms.DialogResult.OK Then

RichTextBox1.SelectionFont = fntdlg.Font

RichTextBox1.SelectionColor = fntdlg.Color

End If

OpenFileDialog1.Filter = "Text Files|*.txt|All Files|*.*"

OpenFileDialog1.Multiselect = True

If OpenFileDialog1.ShowDialog)( = Windows.Forms.DialogResult.OK Then

For Each fn As String In OpenFileDialog1.FileNames

RichTextBox1.Text += fn & vbNewLine

Next

End If

RichTextBox1.SaveFile)"c:\a\b.dat", RichTextBoxStreamType.RichText(

RichTextBox1.HideSelection = False

RichTextBox1.LoadFile)"c:\a\b.dat", RichTextBoxStreamType.RichText(

Dim tp As New TabPage)"Test"(

tp.BackColor = Color.Beige

Dim txt As New TextBox With {.Text = "Ok", .Parent = tp}

TabControl1.TabPages.Add)tp(

Dim tt As New ToolTip With {.BackColor = Color.Blue, .IsBalloon = True, .ShowAlways = True}

tt.SetToolTip)Button2, "Ok"(

tt.SetToolTip)TextBox1, "رمز"(

Dialogs وRichTextBox وTab Control وToolTip:

www.ali-arabi.com

Page 31: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Public Class Form1

#Region "Declaration"

Enum Weekday

sat '= 2

sun

mon

tue

wed

thu

fri

End Enum

Structure My_Date

Dim y As Int16

Dim m, d As Byte

End Structure

Structure Student

Dim name As String

Dim bd As My_Date

Dim salary As Single

Dim holiday As Weekday

End Structure

#End Region

Private Sub Button1_Click)ByVal sender As System.Object, ByVal e As System.EventArgs( Handles Button1.Click

Dim st As Student

st.name = "ali"

st.bd.y = 1369

st.salary = 3000000

st.holiday = Weekday.fri

For w As Weekday = Weekday.sat To Weekday.thu

st.salary += 100000

Next

MsgBox)st.salary(

End Sub

End Class

Structures, Enum:

www.ali-arabi.com

Page 32: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Structure Person

Dim Name As String

Dim Address As String

End Structure

Dim PhoneBook As New Collection

Private Sub Button1_Click)ByVal sender As System.Object, ByVal e As System.EventArgs( Handles Button1.Click

Dim p As Person

p.Name = "Ali"

p.Address = "Tehran"

PhoneBook.Add)p, "021 7689 9798"(

p.Name = "Reza"

p.Address = "Mashhad"

PhoneBook.Add)p, "0511 789 9798"(

For Each p In PhoneBook

ListBox1.Items.Add)p.Name & "-" & p.Address(

Next

End Sub

Private Sub Button2_Click)ByVal sender As System.Object, ByVal e As System.EventArgs( Handles Button2.Click

If PhoneBook.Contains)TextBox1.Text( Then

Dim p As Person = PhoneBook)TextBox1.Text(

MsgBox)p.Name & "-" & p.Address(

End If

End Sub

Collection:

www.ali-arabi.com

Page 33: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Private Sub Button1_Click)ByVal sender As System.Object, ByVal e . . .

Dim f As Integer = FreeFile)(

FileOpen)f, "C:\a\test.txt", OpenMode.Output, OpenAccess.Write, OpenShare.LockRead(

For i As Integer = 1 To 10

'Print)f, i(

Write)f, "i=", i(

Next

FileClose)f(

End Sub

Private Sub Button2_Click)ByVal sender As System.Object, ByVal e . . .

Dim f As Integer = FreeFile)(

Dim n As Integer, s As String = ""

FileOpen)f, "C:\a\test.txt", OpenMode.Input(

While Not EOF)f(

Input)f, s(

Input)f, n(

ListBox1.Items.Add)s & n(

End While

FileClose)f(

End Sub

ترتیبی : دسترسی با متنی های فایل

www.ali-arabi.com

Page 34: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Structure Person

<VBFixedString)15(> Dim Name As String

<VBFixedString)11(> Dim Tel As String

End Structure

Dim p As Person

Private Sub Button1_Click)ByVal sender As System.Object, ByVal e . . .

Dim f As Integer = FreeFile)(

p.Name = TextBox1.Text

p.Tel = TextBox2.Text

Dim lr As Integer = Len)p(

FileOpen)f, "C:\a\test.dat", OpenMode.Random, OpenAccess.Write, OpenShare.LockWrite, Len)p((

Dim l As Long = LOF)f( / Len)p( 'Dim l As Long = FileLen)"C:\a\test.txt"( / Len)p(

FilePut)f, p, l + 1(

FileClose)f(

End Sub

Private Sub Button2_Click)ByVal sender As System.Object, ByVal e . . .

Dim f As Integer = FreeFile)(

FileOpen)f, "C:\a\test.dat", OpenMode.Random, OpenAccess.Read, , Len)p((

FileGet)f, p, CLng)TextBox3.Text((

MsgBox)p.Name & vbNewLine & p.Tel(

FileClose)f(

End Sub

: دسترسیمستقیم با تصادفی های فایل

www.ali-arabi.com

Page 35: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Dim g As Graphics

Private Sub Button1_Click)ByVal sender As System.Object, ByVal e As System.EventArgs( Handles Button1.Click

g = Me.CreateGraphics)(

g.DrawRectangle)Pens.Coral, 10, 10, Me.Width - 30, Me.Height - 50(

'g.DrawArc)Pens.Black, 100, 100, 50, 100, 0, -90(

'g.DrawBezier)Pens.PaleGreen, 10, 10, 200, 10, 300, 200, 10, 10(

'g.DrawEllipse)Pens.Azure, 10, 10, Me.Width - 30, Me.Height - 50(

'Dim i As New Icon)"c:\a\msn.ico"(

'g.DrawIcon)i, 100, 100(

'g.DrawIcon)i, New Rectangle)10, 10, Me.Width - 30, Me.Height - 50((

'g.DrawIconUnstretched)i, New Rectangle)10, 10, Me.Width - 30, Me.Height - 50((

'g.DrawImage)Image.FromFile)"c:\a\msn.ico"(, New Point)10, 10((

Dim points)( As Point = {New Point)10, 10(, New Point)500, 10(, New Point)250, 250(, New Point)10, 10(}

'Dim pointstype)( As Byte = {PathPointType.Line, PathPointType.Line, PathPointType.Line, PathPointType.Line}

'Dim p As New GraphicsPath)points, pointstype(

'g.DrawPath)Pens.Blue, p(

'g.DrawPie)Pens.Red, New Rectangle)10, 10, Me.Width - 30, Me.Height - 50(, 0, -90(

'g.DrawPolygon)Pens.Blue, points(

'Dim b As New SolidBrush)Color.Aqua(

'Dim b As New LinearGradientBrush)New Point)10, 10(, New Point)50, 50(, Color.Blue, Color.Red(

'Dim b As New PathGradientBrush)points, WrapMode.Tile(

'g.FillRectangle)b, New Rectangle)10, 10, Me.Width - 30, Me.Height - 50((

'Dim b As New HatchBrush)HatchStyle.Percent20, Color.Blue, Color.Red(

'Dim b As New TextureBrush)Image.FromFile)"c:\a\watermark.bmp"(, WrapMode.TileFlipXY(

Dim b As SolidBrush = SystemBrushes.Highlight

g.FillPolygon)b, points(

End Sub

گرافیک :

www.ali-arabi.com

Page 36: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Dim f As Integer

Private Sub Button1_Click)ByVal sender As System.Object, ByVal e . . .

PrintDialog1.Document = PrintDocument1

If PrintDialog1.ShowDialog)( = Windows.Forms.DialogResult.OK Then

PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings

f = FreeFile)(

FileOpen)f, "c:\a\b.log", OpenMode.Input(

PrintDocument1.Print)(

MsgBox)"Printing Finished."(

FileClose)f(

End If

End Sub

Private Sub PrintDocument1_PrintPage)ByVal sender As System.Object, ByVal e . . .

Dim fnt As New Font)"Arial", 14, FontStyle.Bold Or FontStyle.Italic(

e.Graphics.DrawRectangle)Pens.Chocolate, e.MarginBounds(

Dim x As Integer = e.MarginBounds.Left, y As Integer = e.MarginBounds.Top

While Not EOF)f(

Dim linestr As String = LineInput)f(

e.Graphics.DrawString)linestr, fnt, Brushes.AliceBlue, x, y(

y += fnt.GetHeight)(

If )y > e.MarginBounds.Bottom( Then Exit While

End While

If )y > e.MarginBounds.Bottom( Then

e.HasMorePages = True

Else

e.HasMorePages = False

End If

End Sub

Printing:

www.ali-arabi.com

Page 37: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Dim s As Double = 0

For Each arg As String In My.Application.CommandLineArgs

s += CDbl)arg(

Next

MsgBox)s(

MsgBox)My.Application.Info.Description(

MsgBox)My.Application.GetEnvironmentVariable)"OS"((

MsgBox)My.Computer.Screen.PrimaryScreen.WorkingArea.Height(

MsgBox)My.Computer.Screen.Bounds.Height(

MsgBox)My.Computer.Keyboard.CapsLock(

My.Computer.Keyboard.SendKeys)"{CAPSLOCK}"(

MsgBox)My.Computer.Info.OSFullName(

MsgBox)My.Computer.Clock.LocalTime(

TextBox2.Text = My.Computer.Clipboard.GetText

PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

PictureBox1.Image = My.Computer.Clipboard.GetImage)(

نام :Myفضای

www.ali-arabi.com

Page 38: درس برنامه سازی پیشرفته 2 Visual Basic.NET

MsgBox)Math.Round)12.685, 2, MidpointRounding.AwayFromZero((

MsgBox)Math.Round)12.685, 2, MidpointRounding.ToEven((

MsgBox)Math.Round)12.675, 2, MidpointRounding.AwayFromZero((

MsgBox)Math.Round)12.675, 2, MidpointRounding.ToEven((

Dim A_D, A_R As Double

A_D = 30

A_R = A_D * Math.PI / 180

MsgBox)Math.Sin)A_R((

Dim d, r As Integer

d = Math.DivRem)100, 30, r(

MsgBox)d & vbNewLine & r(

:Mathکالس

www.ali-arabi.com

Page 39: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Dim x, y As Integer

'On Error Resume Next

On Error GoTo l1

x = CInt)TextBox1.Text(

y = CInt)TextBox2.Text(

MsgBox)x \ y(

MsgBox)x(

Return

l1:

MsgBox)Err.Number & ": " & Err.Description(

Resume Next 'Run MsgBox)x(

‘Resume 'Run MsgBox)x \ y(

خطا ) ( :On Error , Errمدیریت

www.ali-arabi.com

Page 40: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Dim x, y As Integer

Try

x = CInt)TextBox1.Text(

y = CInt)TextBox2.Text(

MsgBox)x \ y(

Catch ex As DivideByZeroException

MsgBox)" صفر بر )"تقسیم

'MsgBox)Err.Number & ": " & Err.Description(

Return

Catch ex As InvalidCastException

MsgBox)" کنید وارد عددی )"مقادیر

Catch ex As Exception

MsgBox)ex.Message(

Finally

MsgBox)"Final"(

End Try

خطا ) …Exception, Try…CatchمدیریتFinally: )

www.ali-arabi.com

Page 41: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Imports System.Data.SqlClient

Public Class Form1

Dim con As SqlConnection, da As SqlDataAdapter, dgv As DataGridView, ds As DataSet

Private Sub Form1_Load)ByVal sender As System.Object, ByVal e . . .

dgv = New DataGridView)( With {.Parent = Me, .Dock = DockStyle.Fill}

AddHandler dgv.SelectionChanged, AddressOf dgv_SelectionChanged

con = New SqlConnection)"server=site9-ostad; database=test;integrated security=true"(

'Dim con As New SqlConnection)"server=site9-ostad; database=test;user id=ali; password=123"(

قبال در SqlConاگر نوع Project-<Windows Application Properties -< Settingsرا ایم Connection stringاز کرده تعریف 'Dim con As New SqlConnection)My.Settings)"SqlCon"((

Try

con.Open)(

ds = New DataSet)(

da = New SqlDataAdapter)"Select * From St_Info", con(

da.Fill)ds, "St_Info"(

dgv.DataSource = ds

dgv.DataMember = "St_Info"

Catch ex As Exception

MsgBox)"Connection Not Open"(

Finally

con.Close)(

End Try

End Sub

داده : کدنویسی – Dataپایگاه و ویزارد مربوطه اشیاء :و

www.ali-arabi.com

Page 42: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Private Sub Insert_Click)ByVal sender As System.Object, ByVal e . . .

con.Open)(

Dim sqlcmdins As SqlCommand

'sqlcmdins = New SqlCommand)"Insert into st_info values)" & CLng)TextBox1.Text( & ",'" & TextBox2.Text & "','" & TextBox3.Text & "'(", con(

'sqlcmdins = New SqlCommand)"Insert Into st_info )St_ID,St_Name,St_Avg( values )" & CLng)TextBox1.Text( & ",'" & TextBox2.Text & "','" & TextBox3.Text & "'(", con(

sqlcmdins = New SqlCommand)"Insert into st_info values)@id,@nam,@avg(", con(

sqlcmdins.Parameters.AddWithValue)"@id", TextBox1.Text(

sqlcmdins.Parameters.AddWithValue)"@nam", TextBox2.Text(

sqlcmdins.Parameters.AddWithValue)"@avg", TextBox3.Text(

sqlcmdins.ExecuteNonQuery)(

con.Close)(

End Sub

Private Sub dgv_SelectionChanged)ByVal sender As System.Object, ByVal e As System.EventArgs(

If dgv.RowCount < 1 Then

TextBox1.Text = dgv.CurrentRow.Cells)0(.Value : TextBox2.Text = dgv.CurrentRow.Cells)1(.Value

TextBox3.Text = dgv.CurrentRow.Cells)2(.Value

End If

End Sub

Private Sub Del_Click)ByVal sender As System.Object, ByVal e . . .

da = New SqlDataAdapter)(

da.DeleteCommand = New SqlCommand)"Delete St_Info Where St_ID=" & CLng)TextBox1.Text(, con(

da.DeleteCommand.ExecuteNonQuery)(

con.Close)(

End Sub

قبلی ... اسالید ادامه

www.ali-arabi.com

Page 43: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Private Sub cmdRefresh_Click)ByVal sender As System.Object, ByVal e As . . .

con.Open)(

ds.Tables)"St_Info"(.Clear)(

da = New SqlDataAdapter)"Select * From St_Info", con(

da.Fill)ds, "St_info"(

con.Close)(

End Sub

Private Sub cmdUpdate_Click)ByVal sender As System.Object, ByVal e As . . .

con.Open)(

Dim sqlcmdupdate As SqlCommand

sqlcmdupdate = New SqlCommand)"Update st_info Set St_Name=@nam, St_Avg=@avg Where St_ID=@id", con(

sqlcmdupdate.Parameters.AddWithValue)"@id", TextBox1.Text(

sqlcmdupdate.Parameters.AddWithValue)"@nam", TextBox2.Text(

sqlcmdupdate.Parameters.AddWithValue)"@avg", TextBox3.Text(

sqlcmdupdate.ExecuteNonQuery)(

con.Close)(

End Sub

End Class

قبلی ... اسالید ادامه

www.ali-arabi.com

Page 44: درس برنامه سازی پیشرفته 2 Visual Basic.NET

On Error Resume Next

con.ConnectionString = "Server=. ;Database=Master; Integrated Security=True"

con.Open)(

strSQL = "EXECUTE sp_attach_db @dbname = 'Students', @filename1 = 'C:\Students_Data.MDF', @filename2 = 'C:\Students_Log.LDF'"

Comm.CommandText = strSQL

Comm.ExecuteNonQuery)(

con.Close)(

con.ConnectionString = "Server=.; Database=Students; Integrated Security=True"

con.Open)(

یک Attachنحوه :Data Baseکردن

www.ali-arabi.com

Page 45: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim n, f As String

n = GetSetting("Info", "User1", "Name", "~!")

f = GetSetting("Info", "User1", "Famil")

'Dim U As String(,) = GetAllSettings("Info", "User1")

'n = U(0, 1) : f = U(1, 1)

If n = "~!" Then

n = InputBox("Enter Name:") : f = InputBox("Enter Famil")

SaveSetting("Info", "User1", "Name", n)

SaveSetting("Info", "User1", "Famil", f)

Else

MsgBox("Hello " & n & " " & f)

End If

'DeleteSetting("Info") 'DeleteSetting("Info", "User1") 'DeleteSetting("Info", "User1","Name")

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

My.Computer.Registry.CurrentUser.OpenSubKey("Console", True).SetValue("Test", 123)

'My.Computer.Registry.LocalMachine.SetValue("Test", 123, Microsoft.Win32.RegistryValueKind.DWord)

MsgBox(My.Computer.Registry.CurrentUser.OpenSubKey("Console", True).GetValue("Test"))

'MsgBox(Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Console", True).GetValue("Test"))

Dim reg As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser

reg.OpenSubKey("AppEvents\EventLabels", True).SetValue("Test", 45)

Dim p As Process() = Process.GetProcessesByName("Explorer")

p(0).Kill()

End Sub()

:Registry

www.ali-arabi.com

Page 46: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e . . .

Try

'Dim c As New My_Circle(5)

'MsgBox(c.Radius)

'MsgBox(c.Pyramid())

'MsgBox(c.Area())

Dim O As New My_Cylinder(10, 4)

MsgBox(O.Area())

Catch ex As Exception

MsgBox(ex.Message)

End Try

End Sub

End Class

Public Class My_Circle

Private RadiusValue As Single

'Public Property Radius() As Single

Public ReadOnly Property Radius() As Single

Get

Return RadiusValue

End Get

'Set(ByVal value As Single)

' If value < 0 Then Throw New ApplicationException("Radius <0 !!!!!")

' R = value

'End Set

End Property

ها : کالس

www.ali-arabi.com

Public Function Pyramid() As Single

Return 2 * Math.PI * Radius

End Function

Public Function Area() As Single

Return Math.PI * Radius ^ 2

End Function

Protected Overrides Sub Finalize()

MsgBox(“ Destructor ")

End Sub

Sub New() ‘Constructor

RadiusValue = 0

End Sub

Sub New(ByVal R As Single) ‘Constructor

RadiusValue = R

End Sub

End Class

Page 47: درس برنامه سازی پیشرفته 2 Visual Basic.NET

ها : کالس ادامه

www.ali-arabi.com

Public Class My_Cylinder

Inherits My_Circle

Private HeightValue As Single

Public Property Height() As Single

Get

Return HeightValue

End Get

Set(ByVal value As Single)

HeightValue = value

End Set

End Property

Sub New()

MyBase.New(0)

Height = 0

End Sub

Sub New(ByVal Radius As Single, ByVal Height As Single)

MyBase.New(Radius)

MyClass.Height = Height

End Sub

Public Overloads Function Area()

Return Height * Pyramid() + 2 * MyBase.Area

End Function

End Class

Page 48: درس برنامه سازی پیشرفته 2 Visual Basic.NET

پشته ) ( :Stackکالس

www.ali-arabi.com

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, . . .

Dim s As New My_Stack

For index As Integer = 1 To 10

s.Push(index)

Next

While Not s.Empty()

ListBox1.Items.Add(s.Pop())

End While

End Sub

End Class

Public Class Node

Public avg As Single

Public prev As Node

End Class

Public Class My_Stack

Private top As Node

Public Sub Push(ByVal a As Single)

Dim newtop As New Node

newtop.avg = a

newtop.prev = top

top = newtop

End Sub

Public Function Pop()

Dim a As Single = top.avg

top = top.prev

Return a

End Function

Public Function Empty()

If top Is Nothing Then

Return True

Else

Return False

End If

End Function

End Class

Page 49: درس برنامه سازی پیشرفته 2 Visual Basic.NET

:Class Library )DLL(

www.ali-arabi.com

Public Class My_Math

Public Function Sin_D(ByVal d As Single) As Double

Dim r As Single = d * Math.PI / 180

Return Math.Sin(r)

End Function

Public Function fact(ByVal x As Byte) As Double

If x <= 1 Then Return 1 Else Return x * fact(x - 1)

End Function

End Class

Page 50: درس برنامه سازی پیشرفته 2 Visual Basic.NET

:User Control Library )DLL(

www.ali-arabi.com

Imports System.Drawing

<ToolboxBitmap("C:\a\a.bmp")> _

Public Class My_Clock

Public Event Time_M_Change As EventHandler

Public Property Lable_B_C() As System.Drawing.Color

Get

Return Label1.BackColor

End Get

Set(ByVal value As System.Drawing.Color)

Label1.BackColor = value

End Set

End Property

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e . . .

Label1.Text = Now.Hour & ":" & Now.Minute & ":" & Now.Second

If Now.Second = 59 Then RaiseEvent Time_M_Change(sender, e)

End Sub

End Class

Page 51: درس برنامه سازی پیشرفته 2 Visual Basic.NET

www.ali-arabi.com

بیشتر بدانیم

Page 52: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Private Sub LinkLabel1_LinkClicked)…(

System.Diagnostics.Process.Start)"http://www.yahoo.com"(

End Sub

Dim g As Graphics, b As Bitmap, first As Boolean = True

Private Sub Button1_Click)ByVal sender As System.Object, ByVal e As g.DrawRectangle)Pens.Blue, 10, 10, 100, 200(

g.DrawString)"AutoReDraw", New Font)"Arial", 20(, Brushes.Black, 10, 10(

Form1_Paint)Nothing, Nothing(

End Sub

Private Sub Form1_Activated)ByVal sender As System.Object, ByVal e As

If first Then

b = New Bitmap)Width, Height, Me.CreateGraphics)((

g = Graphics.FromImage)b(

'g.Clear)Me.BackColor(

first = False

End If

End Sub

Private Sub Form1_Paint)ByVal sender As System.Object, ByVal e As

Me.CreateGraphics)(.DrawImage)b, 0, 0(

End Sub

www.ali-arabi.com

Page 53: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Private Sub Form1_Activated …

AxAgent1.Characters.Load)"merlin"(

AxAgent1.Characters)"merlin"(.Show)(

For Each s As String In AxAgent1.Characters)"merlin"(.AnimationNames

ListBox1.Items.Add)s(

Next

End Sub

Private Sub ListBox1_SelectedIndexChanged …

AxAgent1.Characters)"merlin"(.Stop)(

AxAgent1.Characters)"merlin"(.Play)ListBox1.Text(

End Sub

www.ali-arabi.com

Page 54: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Dim g As Graphics = Me.CreateGraphics

g.DrawEllipse)Pens.Aqua, New Rectangle)10, 10, 300, 200((

'Me.CreateGraphics.DrawEllipse)Pens.Aqua, New Rectangle)10, 10, 300, 200((

g.FillEllipse)Brushes.Green, New Rectangle)10, 10, 300, 200((

'g.FillEllipse)New SolidBrush)Color.Green(, New Rectangle)10, 10, 300, 200((

Dim b As New Drawing2D.LinearGradientBrush)New Point)0, 0(, New Point)100, 100(, Color.Aqua, Color.Black(

g.FillEllipse)b, New Rectangle)10, 10, 300, 200((

Dim b As New Drawing2D.HatchBrush)Drawing2D.HatchStyle.Shingle, Color.Blue, Color.Red(

g.FillEllipse)b, New Rectangle)10, 10, 300, 200((

Dim points)( As Point = {New Point)10, 10(, New Point)20, 30(, New Point)0, 30(}

Dim b As New Drawing2D.PathGradientBrush)points, Drawing2D.WrapMode.Tile(

g.FillEllipse)b, New Rectangle)10, 10, 300, 200((

Dim b As New TextureBrush)Image.FromFile)"c:\WINDOWS\Blue Lace 16.bmp"((

g.FillEllipse)b, New Rectangle)10, 10, 300, 200((

www.ali-arabi.com

Page 55: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Private Sub Form1_Load …

HScrollBar1.Maximum = PictureBox1.Image.Width - PictureBox1.Width

VScrollBar1.Maximum = PictureBox1.Image.Height - PictureBox1.Height

End Sub

Private Sub HScrollBar1_Scroll …

PictureBox1.CreateGraphics.DrawImage(PictureBox1.Image, -HScrollBar1.Value, -VScrollBar1.Value)

End Sub

Private Sub VScrollBar1_Scroll …

PictureBox1.CreateGraphics.DrawImage(PictureBox1.Image, -HScrollBar1.Value, -VScrollBar1.Value)

End Sub

www.ali-arabi.com

Page 56: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Me.TransparencyKey = Me.BackColor

Me.Opacity = 0.5 '0 to 1

Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None

Dim ProcID As Integer

ProcID = Shell)"CALC.EXE", AppWinStyle.NormalFocus(

AppActivate)ProcID(

My.Computer.Keyboard.SendKeys)"22", True(

www.ali-arabi.com

Page 57: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Try Catch Finally

Registry ( savesetting , getsetting, getallsetting, deleting )

my.computer.registry

imports Microsoft.win32

dim r as registrykey

r=registry.---

Process.GetProcessesByName("Explorer")(0).Kill()

Process.Start("notepad")

Microsoft Speech Object Library

Dim sp As New SpeechLib.SpVoice

sp.Speak(“Hello“)

SerialPort1.PortName = "COM3"

SerialPort1.Open()

SerialPort1.Write("atdt 09151234321" + vbCrLf)

www.ali-arabi.com

Page 58: درس برنامه سازی پیشرفته 2 Visual Basic.NET

Dim i As Integer = 1 Private Sub PrintDocument1_PrintPage … Dim x As Single = e.MarginBounds.Left Dim y As Single = e.MarginBounds.Top Dim fnt As New Font(Me.Font.FontFamily, 20, FontStyle.Bold Or FontStyle.Italic) e.Graphics.DrawRectangle(Pens.Red, e.MarginBounds) While i < 200 e.Graphics.DrawString(i, fnt, Brushes.Blue, x, y) i += 1 y += fnt.GetHeight() 'TextRenderer.MeasureText)i, fnt(.Height If y + fnt.GetHeight() > e.MarginBounds.Height + e.MarginBounds.Top Then Exit While End While If (i < 200) Then e.HasMorePages = True Else e.HasMorePages = False End Sub

Private Sub Button1_Click … PrintDialog1.Document = PrintDocument1 If PrintDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings PrintDocument1.Print() End If End Sub

www.ali-arabi.com

Page 59: درس برنامه سازی پیشرفته 2 Visual Basic.NET

روی - کلیک دابل و آنها انتخاب و فرم روی اشیاء گذاشتن ، طراحی نظر Eventهنگام موردکد : Propertiesازپنجره نوشتن Private Sub Btn_Click)ByVal sender Asویا

System.Object, ByVal e As System.EventArgs( Handles Button2.Click, Button1.Click ,…

اجرا - هنگام

Dim btn(5) As Button

Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs)

Dim b As Button = sender

MsgBox(b.Text)

End Sub

Private Sub Form1_Load ...

For i As Integer = 0 To 5

btn(i) = New Button With {.Text = i, .Parent = Me}

btn(i).Location = New Point(10, i * (btn(i).Height + 10))

AddHandler btn(i).Click, AddressOf btn_Click

Next

End Sub

www.ali-arabi.com

Page 60: درس برنامه سازی پیشرفته 2 Visual Basic.NET

) رکوردی ) یا تصادفی و متنی ها فایلFreefile, fileopen, write, writeline, fileclose,

print, input, line input, eof, fileput, filelen=lof, len)structure var.(, fileget, seek=loc

*****len*****A Programmers Introduction to Visual Basic.NET.pdfاز 77صفحه

www.ali-arabi.com

Page 61: درس برنامه سازی پیشرفته 2 Visual Basic.NET

کالسی تمرینات

آن 1( مقدار و شود اضافه عضوی آرایه انتهای به اول دکمه روی کلیک با( . گردد .Redimدریافت گردد( اعالم اعضا مجموع دوم دکمه روی کلیک وبا

)2 ) آن ) تلفظ و بلند صحیح عدد یک حرفی انگلیسی و فارسی معادل

هیچ ) 3( طراحی زمان پویا کامال ویندوز، حساب ماشین حد در حساب ماشین) نباشد فرم روی شیئی

)4Explorer ) هنگام ) متنی فایل چاپ و انتقال و حذف و کپی امکانات باتوسط . ) کلیک کامل Searchبهمراه( List Viewو Tree Viewراست

)5Wordpad با Printکامل

)6Paint امکان و خروج هنگام فرم اندازه و مکانی وضعیت حفظ Printبافرم

دیگر Stackکالس 7( پویای ساختار هر یا

با 8( تلفن کدنویسی ... ( Accessیا Sql )DataBaseو Fileدفترچه با یا

حداقل 9( که ای دهد 10برنامه انجام رجیستری روی کار

www.ali-arabi.com