Volkan ÖZERK
  VB NOTLARI
 
VİSUAL BASİC ÇALIŞMA NOTLARIM


->Private Sub Command1_Click()
List1.Clear
End Sub
 
->Private Sub Command2_Click()
Unload Form1
End
End Sub
 
->Private Sub Data1_Reposition()
' Data1.Caption = Data1.Recordset("ProductName")
End Sub
 
->Private Sub MSFlexGrid1_Click()
List1.AddItem MSFlexGrid.Text
End Sub
 
->Private Sub Command1_Click()
Form2.Show
End Sub
  
Check Box:
 ->Private Sub Click1()
If Check1.Value = vbChecked Then
Text1.Font.Bold = True
Else
Text1.Font.Bold = False
End If
End Sub
 
->Private Sub Check2_Click(Index As Integer)
If Check2.Value = vbChecked Then
Text1.Font.Italic = True
Else
Text1.Font.Italic = False
End If
End Sub
 
->Private Sub Command1_Click()
Text2.Text = Text1.Text
End Sub
 
Option Button:
->Private Sub Command1_Click()
If Option1.Value = True Then
MsgBox ("Evet")
End If
If Option2.Value = True Then
MsgBox ("Hayır")
End If
End Sub
 
ListBox Ve ComboBox:
->Private Sub Command1_Click()
If List1.ListIndex = 0 Then
Text1.Text = List1.List(0)
Else
Text1.Text = "Herkez Bir Gün Fenerli Olacak"
End If
If Combo1.ListIndex = "0" Then
MsgBox ("fenerlisen doğru yoldasen")
Else
MsgBox ("Herkez Bir Gün Fenerli Olacak")
End If
End Sub
 
->Private Sub Form_Load()
List1.AddItem "FENERBAHÇE"
List1.AddItem "GALATASARAY"
List1.AddItem "BEŞİKTAŞ"
List1.AddItem "TRABZONSPOR"
List1.AddItem "ANKARAGÜCÜ"
List1.AddItem "GENÇLERBİRLİĞİ"
List1.AddItem "DENİZLİSPOR"
Combo1.AddItem "FENERBAHÇE"
Combo1.AddItem "GALATASARAY"
Combo1.AddItem "BEŞİKTAŞ"
Combo1.AddItem "TRABZONSPOR"
Combo1.AddItem "ANKARAGÜCÜ"
Combo1.AddItem "GENÇLERBİRLİĞİ"
Combo1.AddItem "DENİZLİSPOR"
End Sub
 
PİCTURE BOX:
->Private Sub Form_Load()
Picture1.Picture = LoadPicture("C:Documents and SettingsAdministratorDesktopnetsis.jpg")
End Sub
 
Picture box,image control,Shape Kontrol
->Private Sub Image1_Click()
Shape1.Left = Image2.Left
Picture1.Cls
Picture1.Print "Select: Club"
Shape1.Visible = True
End Sub
 
Private Sub Image2_Click()
Shape1.Visible = False
End Sub
 
 
*Çeşitli Değişkenleri Aynı İsimle Kullanma
Module 1’e yazılacak.
Public x As Integer
Sub Test()
x = 1
End Sub
Module 2’ye yazılacak.
Public x As Integer
Sub Test()
x = 2
End Sub
 
Private Sub Command1_Click()
Module1.Test
MsgBox Module1.x
End Sub
 
Private Sub Command2_Click()
Module2.Test
MsgBox Module2.x
End Sub
 
*Public Vs. Local Variables
->Public Temp As Integer
Sub Test()
Dim Temp As Integer
Temp = 2
MsgBox Form1.Temp
End Sub
 
->Private Sub Command1_Click()
Test
End Sub
 
->Private Sub Form_Load()
Temp = 1
End Sub
 
*Shadow Kontrol
->Private Sub Command2_Click()
Text1 = "Variable" 'Variable Shadows Control
Me.Text1 = "Control" 'Must qualify with Me to get control
End Sub
 
*Statik Değişken
 
->Function RunningTotal(num)
Static ApplesSold
ApplesSold= ApplesSold+num
RunningTotal=ApplesSold
End Function
*Bütün Değişkenleri Statik Tanımlamak
Formülün başına “Static” tanımlarsak içindeki tüm değişkenler statik olur,örneğin
Static Function RunningTotal(num)
*Sabit Tanımlamak
 [Public/Private] Const constantname[As type]=expression
Const   conPi=3,14159265358979
Public Const conMaxPlanets As Integer=9
Const conReleaseDate=#1/1/95#
Public Const conVersion=”07.10.A”
Const conCodeName= “Enigma”
Public Const conPi=3,14159265358979, conMaxPlanets=9,_
conWorldPop=6E+09
Const conPi2= conPi * 2
Kullanım örneği;
Static SolarSystem(1 to conMaxPlanets)
If numPeople > conWorldPop Then Exit Sub
 
 *Vb komutunda Alt Satıra Geçme:
Data1.RecordSource = _
“SELECT * FROM Titles, Publishers”   _                       ‘Buraya açıklama yzabiliriz.
& ”WHERE Publishers.PubId=Titles.PubID” _
& “AND Publishers.State= ‘CA’ “
 
Text1.Text= “Hello”    : Red=255   : Text1.Backcolor= _
Red
*Değişkene Veri Yükleme Ve Bulma:
->Private Sub Command1_Click()
X = 10
X = X + 1
Text1.Text = X
End Sub
 
*Fonksiyon örnek:
->Function SafeSqr(num)
Dim TempVal
            TempVal=Abs(num)
            SafeSqr   =Sqr(TemVal)
End Function
 
 *Prosedür Değişkenleri: (Dim OR Static)
Values in local variables declared with Static exist the entire time your application is running while variables declared with Dim exist only as long as the procedure is executing.
Dim    intTemp   As     Integer
Static    intPermanent      As     Integer
 
* Variables Değişkenleri: (Dim ,Private,Public)
->Private   intTemp   As     Integer
->Public   intTemp   As     Integer
 
->Private Sub Command1_Click()
Dim X, Y
X = "6"
Y = "7"
Print X & Y
Print X + Y
X = 6
Print X & Y
Print X + Y
End Sub
 
->Private Sub Command1_Click()
Dim rightnow ', daysleft, hoursleft, minutesleft
rightnow = Now
Print rightnow
End Sub
 
->Private Sub Command1_Click()
Dim rightnow, daysleft, hoursleft, minutesleft
rightnow = Now
daysleft = Int(DateSerial(Year(rightnow) + 1, 1, 1) - rightnow)
hoursleft = 24 - Hour(rightnow)
minutesleft = 60 - Minute(rightnow)
 
Print daysleft & "days left in the year"
Print hoursleft & "hoursleft in the day"
Print minutesleft & "minutes left in the hour"
 
End Sub
 
 
->Private Sub Command2_Click()
Dim Somedate, daysleft
If IsDate(Text1.Text) Then
Somedate = CDate(Text1.Text)
daysleft = DateSerial(Year(Somedate) + 1, 1, 1) - Somedate
Text2.Text = daysleft & "days left in the year."
Else
MsgBox Text1.Text & "is not a valid date."
End If
End Sub
 
Arrays:
Public veya Dim Counters(14) As Integer    ‘0 dan 14 ‘e kadar 15 element içerir.
Public veya Dim Sums(20) As Integer    ‘0 dan 20 ‘ye kadar 21 element içerir.
Dim Counters(1 to 15) As Integer
Dim Sums (100 to 120) As String
 
->Private Sub Command1_Click()
Dim intX As Integer
Dim countersA(5) As Integer
Dim countersB(4) As Integer
For intX = 0 To 4
countersB(intX) = "hello"
Next intX
Dim arrX(2) As Variant
arrX(1) = countersA()
arrX(2) = countersB()
MsgBox arrX(1)(2)
MsgBox arrX(2)(3)
End Sub
 
Multidimensional Arrays:
Static MatrixA(9,9) As Double
Static MatrixA(1 to 10,1 to 10) As Double
Dim MultiD(3,1 to 10,1 to 15)
 
Using Loops to Manipulate Arrays:
Dim I As Integer, J As Integer
Static MatrixA(1 To 10, 1 To 10) As Double
For I = 1 To 10
    For J = 1 To 10
    MatrixA(I, J) = I * 10 + J
    Next J
Next I
Dinamic Arrays:Büyük dizilerde hafızadan tasarruf için dinamik array kullanmak iyidir.
->Dim DynArray()
ReDim sadece procedürde kullanılır.
->Redim DynArray(X+1)
Redim sabit dizileride destekler.
Redim DynArray(4 to 12)
->Dim Matrix1 As Integer
Sub CalcValues Now()
.
.
.
ReDim Matrix1(19,29)    ‘veya ReDim Matrix1(X,Y)
 
*PROCEDURS:
They are several types of procedures used in visual basic:
*Sub procedurs don’t return a value
*Function return a value
*Property procedures can return and assign values,and set reference to objects
1)Sub Procedure:
[Private|Public] [Static] Sub procedurename (arguments)
Statements
End Sub
1.1)General Procedures:
1.2)Event Procedures:
You should use underscore(_) sign with the event procedure
1.2.1 Syntax for a control event
Private Sub controlname_eventname (arguments)
Statements
End Sub
1.2.2 Syntax for a from event
Private Sub Form_eventname(arguments)
Statements
End Sub
Or example you use MDI from ,the event procedure combines the word MDIForm_Load
 
When a procedure name does not match a control name,it becomes a general procedure.
2.Function Procedures:
[Private|Public][Static] Function procedurename (arguments) [As Type]
Statements
End Function
Difference Between Sub And Function Procedures
*Generally,you call a function by including the function procedure name and arguments on teh right side of a larger statement or expression (returnvalue=function()).
*Function procedures have data types,just as a variables do.This determines the type of the return value.
*You return a value by asigning it to the procedurename itself. When the Function procedure returns a value, this value can then become part of a larger expression.
 
For Example :
Function Hypotenuse (A As Integer,B As Integer)_
As String
Hypotenuse=Sqr(A^2+B^2)
End Function
You call a Function procedure the same way you call any of the built-in function in Visual BasiC:
Label1.Caption =Hypotenuse(CInt(Text1.Text),_
CInt(Text2.Text))
Or
strX=Hypotenuse(Width,Heighth)
To View A Procedure İn Another Module:
1.From the view Menu ,Choose Object Browser
2.Select the Project from the Project library box.
3.Select the module from the Classes List ,and the procedure from the members of list.
4.Choose View Definition
Calling Sub Procedures: There are two ways caling sub procedures:
1)Call MyProc (FirstArgument,SecondArgument)
2)MyProc FirstArgument,SecondArgument
Calling Function Procedures: 
1)Call Year(Now)
2)Year Now
Calling Procedures İn Other modules:
->Procedures in Forms:
İf a procedure named SomeSub Then
Call Form1.Somesub(arguments)
->Procedures in class Module:
For example ,DemoClass is an instance of the class. For example,DemoClass is an instance of a classnamed Class1:
Dim DemoClass As New Class1
DemoClass.SomeSub
->Procedures in standart Modules: 
Module2.CommonName(arguments)
Passing Arguments to Procedures:
Argument Data Types:
->Function WhatsForLunch(Weekday As String,Hour_
As Integer) As String
If Weekday= “Friday” Then
WhatsForLunch=”Fish”
Else
WhatsForLunch=”Chicken”
Endi f
İf Hour >4 Then WhatsForLunch=”Too Late”
End Function
 
Passing Argument By Value:
Only a copy of avariable is passed when an argument is passed by value. If the procedure changes the value,the change affects only the copy and not the variable itself. Use the “ByVal” keyword to indicate an argument passed by value.
For Example:
Sub PostAccounts(ByVal intAcctNum as Integer)
.
.
.
End Sub
 
 
Passing Argument By Reference:
Sub CallingProcedure ()
Dim intX As Integer
intX=12*3
Foo(intX)
End Sub
Sub Foo(Bar As String)
MsgBox Bar
End Sub
 
Using Optional Arguments:
If you specify an optimal argument,all sub sequent arguments in the argument list must also be optional and declared with the optional keyword.
Example(This code provides all optional arguments)
Dim strName As String
Dim strAddress As String
Sub ListText(Optional x As String, Optional y_
As String)
List1.AddItem x
List2.AddItem y
End Sub
 
Private Sub Command1_Click()
strName=”volkan”
strAdress=”volkan1985.tr.gg”
Call ListText(strName,strAdress)
End Sub
 
This code,however,doesn’t provide all optional arguments:
Dim strName As String
Dim varAdress As Variant
Sub ListText(x As String,Optional y As Variant)
List1.AddItem x
If Not IsMissing(y) Then
List1.AddItem y
Endi f
End Sub
 
Private Sub Command1_Click()
strName=”volkan”
Call ListText(strName)
End Sub
 
In the case where an optional argument is not provided ,the argument is actually assigned as a variant with the value of Empty.The example above show how to test for missing optional arguments using the Ismissing function.
 
 
Providing a Default for an Optional Arguments:
->Sub ListText(x As String,Optional y As_
Integer=12345)
List1.AddItem   x
List2.AddItem y
End Sub
Private Sub Command1_Click()
strName=”yourname”
Call ListText(strName)
End Sub
 
Using an Indefinite Number of Arguments:
Using the “ParamArray” keyword allows you to specify that a procedure will accept an arbitrary number of arguments.
 
Dim x As Integer
Dim y As Integer
Dim intSum As Integer
Sub Sum(ParamArray intNums())
For Each x In intNums
y=y+x
Next x
intSum=y
End Sub
Private Sub Command1_Click()
Sum 1,3,5,7,8
List1.AddItem intSum
End Sub
 
 
 
 
 
 
 
 
  Sayfamı 13347 ziyaretçi (22414 klik) kişi ziyaret etti  
 
Bu web sitesi ücretsiz olarak Bedava-Sitem.com ile oluşturulmuştur. Siz de kendi web sitenizi kurmak ister misiniz?
Ücretsiz kaydol