ตัวอย่างรหัสต้นฉบับอย่างสั้น (Short Source Code)
- สร้าง console application หรือเปลี่ยนใน Application type ก็ได้ แล้วสร้าง module ?
Module Module1
Public a As Integer = 5
Sub main()
Dim b As Integer
b = a + 1
Console.WriteLine(b)
a = Val(Console.ReadLine())
b = a + 5
For a = b To 10 : Console.WriteLine(a) : Next
If a = 1 Then Console.Write(5)
Console.ReadLine()
End Sub
End Module
- สร้าง Module ที่มี sub main จะแสดงชื่อ module ให้เลือกใน Startup Object
Module Module1
Sub main()
MsgBox(5)
End Sub
End Module
- ใน form1 สามารถสร้างและเรียกใช้ method ของตนได้
Public Class Form1
Sub m()
MsgBox(6)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Call m()
End Sub
End Class
- ถ้าสร้าง method ใน class แบบ shared จะเรียกใน form ได้ ถ้า imports
Public Class Class1
Shared Sub m()
MsgBox(6)
End Sub
End Class
Imports WindowsApplication1.Class1
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Call m()
End Sub
End Class
- ใช้แต่ method สร้าง form และ control ใน runtime
- All these Windows Controls are based on the Control class, the base class for all controls.
- Visual Basic allows us to work with controls in two ways: at design time and at runtime. ?
- The Control class is in the System.Windows.Forms namespace.
- เลือก enable application framework ใน WindowsApplication Properties แล้วเลือก Sub Main
Module Module1
Sub main()
Dim instance As New Form
Dim cm As New Button
cm.Text = "abc"
instance.Size = New Size(300, 200)
instance.Controls.Add(cm)
instance.ShowDialog()
End Sub
End Module
- ใช้แต่ method สร้าง form และ control และ event
Module Module1
Sub main()
Dim instance As New Form
Dim lb As New ListBox
Dim cm As New Button
AddHandler cm.Click, AddressOf cmEventHandler
cm.Text = "abc"
lb.Items.Add("ทดสอบ")
lb.Items.Add("ไทยออล")
lb.Left = 100 ' pixels
AddHandler lb.Click, AddressOf lbEventHandler
instance.Size = New Size(300, 200)
instance.Controls.Add(cm)
instance.Controls.Add(lb)
instance.ShowDialog()
End Sub
Public Sub cmEventHandler(ByVal sender As Object, ByVal e As System.EventArgs)
MsgBox(sender.text.ToString)
End Sub
Public Sub lbEventHandler(ByVal sender As Object, ByVal e As System.EventArgs)
Dim s As ListBox = sender
MsgBox(s.Items(s.SelectedIndex.ToString))
' MsgBox(sender.items(1)) ' ไทยออล
End Sub
End Module
- ฟังก์ชัน อาร์เรย์ และวันที่ (Random & Array & Date)
Dim v1 As Integer = CInt(Rnd() * 100)
Dim v2 As Double = Rnd()
Dim v3() As Integer = {5, 6}
MsgBox(v1 & "-" & v2 & "-" & v3(0) & "-" & v3.Length)
Dim v4 As Date
v4 = Now '11/13/2007 8:29:50 PM
v4 = #11/13/2007#
MsgBox(TimeOfDay & " " & v4) ' 8:29:50 PM 11/13/2007
Dim v5(2) As Integer
v5(0) = 7 : v5(1) = 8 : MsgBox(v5(0) + v5(1)) ' 15
Dim v6 As Integer
v6 = DateDiff(DateInterval.Day, #10/13/2006#, v4) ' 396
MsgBox(v6)
Dim v7 As String = "abcdef"
MsgBox(Mid(v7, 1, 2) & Microsoft.VisualBasic.Left(v7, 2))
Dim v8() As String
v8 = Split("a+b+c", "+")
MsgBox(v8(0) & chr(10) & v8(1))
' chr(65)=A & Asc("A")=65 & Format(Asc("A"), "000")=065
- การสุ่มแบบไม่ซ้ำ (Random & No duplicate)
Randomize()
Dim a(5) As Integer, i, j As Integer, s As String = "", found As Boolean
For i = 0 To 5
- found = False
- Dim t As Integer = CInt(Rnd() * 10)
- For j = 0 To i - 1
- If a(j) = t Then found = True
- Next
- If (found) Then i -= 1 Else a(j) = t : s &= t & Chr(10)
Next
MsgBox(s)
- consoleapplication + byval + byref + sub + function (stop with breakpoint + ต.ย.ข้อสอบ)
Module Module1
- Dim a As Integer
- Sub Main()
- Dim a As Integer = 5
- sub1(a, a)
- Console.WriteLine(func1(a, a)) ' 25
- End Sub
- Sub sub1(ByVal x As Integer, ByRef y As Integer)
- a += x : x += a : y += a
- End Sub
- Function func1(ByVal x As Integer, ByRef y As Integer) As Integer
- func1 = a + x + y
- End Function
End Module
- Timer + Nanosecond + Progressbar
Public Class Form1
' The value of this property represents the number of 100-nanosecond intervals
' that have elapsed since 12:00:00 midnight, January 1, 0001.
Dim start As Long
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
start = Now.Ticks
Timer1.Interval = 250 'milliseconds
Timer1.Start()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("Nano Second = " & (Now.Ticks - start))
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
If (ProgressBar1.Value = 100) Then ProgressBar1.Value = 0
ProgressBar1.Value += 1
Button1.Text = ProgressBar1.Value
End Sub
End Class
- โปรแกรมนี้ error เกี่ยวกับความเข้ากันได้ของคำสั่ง
- ทำไม error ว่า Keyword not supported: 'provider'. ตอนส่งค่าให้ ConnectionString
1. เพราะ provider=microsoft.jet.oledb.4.0 ใช้กับ SqlConnection ไม่ได้
2. ถ้าใช้ SqlConnection ต้องเืชื่อมต่อกับ SQLServer ที่้กำหนด server, user, password, db
Public Class Class1
Public Shared data As String = _
"provider=microsoft.jet.oledb.4.0;data source=c:\northwind.mdb"
End Class
Imports system.data.sqlclient
Public Class Form1
Dim conn As SqlConnection
Dim com As SqlCommand
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim strconn As String = Class1.data
conn = New SqlConnection()
With conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strconn ' Keyword not supported: 'provider'
.Open()
End With
End Sub
End Class
- จับเวลาการประมวลผล 10 ล้านรอบใน for
' my computer use about 62.5 Millisecond
Module Module1
Dim i, j, startclock, stopclock As Long
Sub Main()
display_time(startclock)
For i = 1 To 10000000 : j += i : Next
display_time(stopclock)
Console.WriteLine(stopclock - startclock)
Console.ReadLine()
End Sub
Sub display_time(ByRef x As Long)
x = Now.Ticks
Console.WriteLine(Now.Millisecond & " millisecond " & Now.Ticks & " ticks")
End Sub
End Module
- ASP Short Code
<body bgcolor=yellow>
<form id="fm" runat="server">
<asp:TextBox ID="TextBox1" runat="server" Text="0">
</form>
<% response.write(textbox1.text) %>
<% Session("x") = TextBox1.Text %>
</body>
' คำสั่งสำหรับปุ่มใน .vb เพื่อเปิดฟอร์มใหม่ Server.Transfer("default2.aspx", True)
' Session เป็นอีกวิธีหนึ่งที่ใช้ส่งค่าระหว่างฟอร์ม
' Response.Redirect("default.aspx?a=6") เป็นการส่งผ่าน request.querystring อีกวิธีหนึ่ง
- to call command in shell
Shell("notepad.exe", AppWinStyle.NormalFocus)
Shell("c:\x.bat", AppWinStyle.NormalFocus)
- to start & close process
Dim p As New Process()
Private Sub b1()
p = Process.Start("C:\Windows\system32\calc.exe")
End Sub
Private Sub b2()
p.CloseMainWindow()
p.Close()
End Sub
- SQL ที่น่ารู้สำหรับ Crystal Report มาก
1. select * from [order details]
2. select * from orders where orderdate = #11/16/1994#
3. select * from orders where customerid like "*A*"
4. select * from orders where employeeid > {?t1}
5. select * from orders where orderdate = {?t1} ' CDate(TextBox1.Text)
6. select orders.*,[order details].* from (
- [order details] inner join orders
- on [order details].orderid = orders.orderid
- ) where [order details].productid={?t1} and orders.employeeid = {?t2}
Imports Crystaldecisions.shared ?
- Dim rpt As String
- rpt = "C:\CrystalReport1.rpt"
- CrystalReportViewer1.ReportSource = rpt
- การ ping และ download แฟ้มจากอินเทอร์เน็ต
- If My.Computer.Network.Ping("www.google.com", 1000) Then
- MsgBox("Server pinged successfully.")
- Else
- MsgBox("Ping request timed out.")
- End If
- My.Computer.Network.DownloadFile("http://www.thaiall.com/ta1.gif", "C:\ta1.gif")
- My.Computer.Network.UploadFile( _
- "C:\ta1.gif", "http://www.yourhost.com/upload.aspx", "anonymous", "")
- ' Format(1, "000")=001
- If My.Computer.FileSystem.FileExists("c://x.txt") Then
- My.Computer.FileSystem.DeleteFile("c://x.txt")
- My.Computer.Network.DownloadFile("http://www.thaiall.com", "C:\x.txt")
- msgbox(My.Computer.FileSystem.ReadAllText("c://x.txt"))
- Else
- My.Computer.Network.DownloadFile("http://www.thaiall.com", "C:\x.txt")
- End If
|