Menu Horisontal

Senin, 21 Januari 2013

VB - Building Dynamic Forms at Runtime - Visual Basic.NET 2008

Sumber : http://visualbasic.w3computing.com/vb2008/5/vb-building-dynamic-forms-runtime.php   
 

Sometimes you won't know in advance how many instances of a given control might be required on a form. Let's say you're designing a form for displaying the names of all tables in a database. It's practically impossible to design a form that will accommodate every database users might throw at your application. Another typical example is a form for entering family related data, which includes the number of children in the family and their ages. As soon as the user enters (or changes) the number of children, you should display as many TextBox controls as there are children to collect their ages.
For these situations, it is possible to design dynamic forms, which are populated at runtime. The simplest approach is to create more controls than you'll ever need and set their Visible properties to False at design time. At runtime, you can display the controls by switching their Visible properties to True. As you know already, quick-and-dirty methods are not the most efficient ones. You must still rearrange the controls on the form to make it look nice at all times. The proper method to create dynamic forms at runtime is to add controls to and remove them from your form as needed, using the techniques discussed in this section.
Just as you can create new instances of forms, you can also create new instances of any control and place them on a form. The Form object exposes the Controls collection, which contains all the controls on the form. This collection is created automatically as you place controls on the form at design time, and you can access the members of this collection from within your code. It is also possible to add new members to the collection, or remove existing members, with the Add and Remove statements accordingly.

The Form's Controls Collection

All the controls on a form are stored in the Controls collection, which is a property of the Form object. The Controls collection exposes members for accessing and manipulating the controls at runtime, and they're the usual members of a collection:

Add method

The Add method adds a new element to the Controls collection. In effect, it adds a new control on the current form. The Add method accepts a reference to a control as an argument and adds it to the collection. Its syntax is the following, where controlObj is an instance of a control:
Controls.Add(controlObj)
To place a new Button control on the form, declare a variable of the Button type, set its properties, and then add it to the Controls collection:
Dim bttn As New System.WinForms.Button
bttn.Text = "New Button"
bttn.Left = 100
bttn.Top = 60
bttn.Width = 80
Me.Controls.Add(bttn)

Jumat, 11 Januari 2013

Daftar Fungsi-fungsi pada Visual Basic



Function Abs(Number)
    Member of VBA.Math
    Returns the absolute value of a number

Sub AppActivate(Title, [Wait])
    Member of VBA.Interaction
    Activates an application window

Function Asc(String As String) As Integer
    Member of VBA.Strings
    Returns the ANSI character code of the first character in a string

Function AscB(String As String) As Byte
    Member of VBA.Strings
    Returns the value of the first byte in a string

Function AscW(String As String) As Integer
    Member of VBA.Strings
    Returns the native character code of the first character in a string (Unicode or ANSI)

Function Atn(Number As Double) As Double
    Member of VBA.Math
    Returns the arctangent of a number

Sub Beep()
    Member of VBA.Interaction
    Sounds a tone through the computer's speaker

Sumber :


Function CallByName(Object As Object, ProcName As String, CallType As VbCallType, Args() As Variant)
    Member of VBA.Interaction
    Support IDispatch::Invoke

Function CBool(Expression) As Boolean
    Member of VBA.Conversion
    Converts an expression to a Boolean

Function CByte(Expression) As Byte
    Member of VBA.Conversion
    Converts an expression to a Byte

Function CCur(Expression) As Currency
    Member of VBA.Conversion
    Converts an expression to a Currency

Function CDate(Expression) As Date
    Member of VBA.Conversion
    Converts an expression to a Date

Senin, 07 Januari 2013

KOMUNIKASI PLC DENGAN VISUAL BASIC

Sumber : http://ayobelajarelektro.blogspot.com/2012/10/komunikasi-plc-dengan-visual-basic.html
 
Visual Basic adalah sebuah program yang bisa digunakan untuk mengakses data yang terdapat dalam memori sebuah PLC. Software Visual Basic yang digunakan adalan Visual Basic 6.0 dan PLC-nya dari keluarga Omron yaitu Omron CPM1A/CPM2A. Komunikasi PLC dengan Visual Basic sebagai implementasi konsep dasar pertukaran data yang dilakukan melalui port serial. Ada beberapa hal yang harus diperhatikan agar komunikasi PLC dengan VB dapat berjalan dengan baik, diantaranya adalah sebagai berikut :
1)    VB Enterprise Edition Controls
Ketika akan membuat sebuah program yang berhubungan langsung antara PLC dengan VB, maka pengguna program harus memilih VB Enterprise Edition Controls di new projek pada visual basic. Karena VB Enterprise Edition Controls ada property MSComm yang berfungsi untuk komunikasi antara PC dengan PLC (Progammable Logic Controller). MSComm juga berfungsi untuk menyamakan Port yang digunakan oleh PC maupun PLC (Progammable Logic Controller). Gambar berikut di bawah ini adalah gambar  VB Enterprise Edition Controls :
Gambar 12. VB Enterprise Edition Controls
VB Enterprise Edition Controls dipilih karena terdapat property MSComm. Dalam  property MSComm  terdapat beberapa  parameter yang harus diset persis antara PC dengan PLC.
2)    Pengaksesan Port Serial Pada Visual Basic
Dalam pengaksesan port serial pada Visual Basic akan dijelaskan beberapa hal, diantaranya adalah sebagai berikut :
a.      Properti MSComm
Untuk pengaksesan port serial kita dapat mengaksesnya secara langsung menggunakan kontrol MSComm yang telah disediakan Visual Basic. Kontrol MSComm menyediakan fisilitas komunikasi antara program aplikasi yang kita buat dengan port serial untuk mengirim atau menerima data melalui port serial. Setiap MSComm hanya menangani satu port serial sehingga jika kita ingin menggunakan lebih dari satu port serial harus digunakan MSComm lain. Properti yang sering digunakan adalah :
1)   CommPort
Digunakan untuk menentukan nomor port serial yang akan dipakai.
2)   Setting
Digunakan untuk menset nilai baud rate, pariti, jumlah bit data, dan jumlah bit stop.
3)   PortOpen
Digunakan untuk membuka ataupun menutup port serial yang dihubungkan dengan MSComm ini.
Berikut ini adalah contoh penggunaan properti MSComm :
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "9600, e, 7, 2"
MSComm1.PortOpen = True
End Sub
“MSComm1.CommPort” digunakan untuk mengatur port mana yang akan dipakai dan dalam kasus ini port yang dipakai adalah port 1.  MSComm1.Settings” digunakan untuk mengatur parameter komunikasi yang harus sama persis dengan yang ada pada PLC. Dalam hal ini PLC ter set standar yaitu baud rate 9600, dengan paritas even, panjang data berjumlah 7 bit dan stop bit berjumlah 2 bit (9600,e,7,2). Lalu kode  MSComm1.PortOpen”  digunakan untuk membuka dan menutup komunikasi. Berikut di bawah ini adalah gambar properti MSComm :
Gambar 13. properti MSComm
Jika antara PC dengan PLC tidak mampu berkomunikasi, maka ada beberapa hal yang masih harus dicek. Hal-hal yang perlu dicek adalah sebagai berikut :
·      Setting baud rate, biasanya setting PLC dan PC yang digunakan adalah (9600,e,7,2) 
·      Koneksi kabel RS 232, kabel harus terpasang dengan baik.
Berikut adalah gambar wiring diagram koneksi PC-PLC :
Gambar 14. Wiring Diagram Koneksi PC-PLC
b.      Komunikasi Serial RS-232
Komunikasi serial RS-232 termasuk salah satu bentuk konfigurasi pada komunikasi HLC (Host Link Communication) pada PLC terutama PLC Omron CPM1A/CPM2A.
Gambar 15. Komunikasi Serial RS-232
Dari gambar di atas, nampak bahwa PLC Omron CPM1A/CPM2A memerlukan modul komunikasi serial RS-232 untuk dapat berkomunikasi dengan serial device lainnya.  Berikut ini tampilan detailnya :
Gambar 16. Modul Komunikasi RS-232
Ada beberapa alasan penggunaan port serial, diantaranya adalah sebagai berikut :
·         Pada komunikasi dengan kabel yang panjang, masalah cable loss tidak akan menjadi masalah besar daripada menggunakan kabel parallel.
·         Dubutuhkan jumlah kabel yang sedikit, bisa hanya menggunakan 3 kabel yaitu saluran Transmit Data, saluran Receive Data, dan saluran Ground.
3)    Settingan Port Pada PC dan PLC
Agar terjadi komunikasi antara PC dengan PLC maka selain Property MSComm yang harus disetting, port pada PC maupun pada PLC harus juga disamakan. Berikut di bawah ini adalah settingan port pada PC maupun PLC :
Gambar 17. Communications Port (COM1) Properties Pada PC
Komunikasi serial pada PLC Omron CPM1A/CPM2A ini pada umumnya digunakan untuk komunikasi PLC dengan komputer. Protokol untuk komunikasi dengan PC disebut : Host Link communication (HLC). Pengesetan Host Link Communication atau Host Link Unit berfungsi untuk menjembatani PC dalam memonitor status pengoperasian dan lokasi data dari PLC. Berikut di bawah ini adalah settingan port pada PLC OMRON CPM1A/CPM2A :
Gambar 18. Serial Communications Settings Pada PLC
CONTOH PROGRAM PLC-VB
Kalau Mau Tahu Program Yang Lengkapnya cari aja di perpus teknik elektro unnes atau perpus pusat unnes judulnya (MONITORING DAN CONTROLLING PLC (PROGAMMABLE LOGIC CONTROLLER) MENGGUNAKAN PC
 (PERSONAL COMPUTER)
Nih tak kasih contoh sedikit tampilan programnya :
Program VB untuk merubah mode PLC
Berikut adalah salah satu contoh bentuk listing program dari tombol-tombol untuk merubah kondisi PLC :
·         Tombol Program
Private Sub Cmdprogram_Click()
data = "@00SC00" '(setting hostlink pada PLC 00)
kirim
End Sub
Terlihat pada listing program di atas untuk mengatur PLC pada mode program, data yang dikirim ke PLC melalui variable data adalah "@00SC00". Untuk mode monitor data yang dikirim ke PLC melalui variable data adalah "@00SC02". Sedangkan untuk mode run data yang dikirim ke PLC melalui variable data adalah "@00SC03". Ketika PLC pada posisi mode monitor, PLC masih melakukan editing pada program PLC dan mampu diperintah oleh PC. Sedangkan ketika mode run PLC tidak mampu lagi diperintah oleh PC.

Minggu, 06 Januari 2013

Penomoran Pada DatagridView VB.Net

Sumber : http://class-super.blogspot.com/search/label/VB.Net
Cara menambahkan nomor pada DatagridView :



Kode Program VB.Net :
Private Sub DataGridViewRowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs)
 Dim dg As DataGridView = DirectCast(sender, DataGridView) 
 Dim rowNumber As String = (e.RowIndex + 1).ToString() 
 While rowNumber.Length < dg.RowCount.ToString().Length
  rowNumber = "0" & rowNumber
 End While
 
 Dim size As SizeF = e.Graphics.MeasureString(rowNumber, Me.Font)
 If dg.RowHeadersWidth < CInt(size.Width + 20) Then
  dg.RowHeadersWidth = CInt(size.Width + 20)
 End If
 
 Dim b As Brush = SystemBrushes.ControlText
 e.Graphics.DrawString(rowNumber, dg.Font, b, e.RowBounds.Location.X + 15, _
 e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2))
End Sub

Printing Directly to the Printer in VB.NET

Ini diambil dari sumber dibawah ini, dan belum pernah coba. Cuma mau sharing dan siapa tahu ada yang cari
Sumber : http://www.c-sharpcorner.com/uploadfile/jodonnell/printing-directly-to-the-printer-in-vb-net/
Using this code enables you to print directly to the printer using WIN32 API calls and therefore should enable you to print at maximum speed rather than relying in the Windows Printing subsystems. Additionally I have added code to show how to send PCL codes to the printer.

The code has been compiled using the Everett beta of Visual Studio.NET however if you have not got this just coy and paste the code into a console project and recompile.

' PrintDirect.vb
' Shows how to write data directly to the printer using Win32 API's
'Adapted from Microsoft Support article Q298141
'This code assumes you have a printer at share file://192.168.1.101/hpl
'This code sends Hewlett Packard PCL5 codes to the printer to print
' out a rectangle in the middle of the page. 
Imports System
Imports System.Text
Imports System.Runtime.InteropServices
_
Public Structure DOCINFO 
Public pDocName As String 
Public pOutputFile As String 
Public pDataType As String
End
 Structure 'DOCINFO
Public Class PrintDirect
 

False, CallingConvention := CallingConvention.StdCall)> _
Public Shared Function OpenPrinter(pPrinterName As String, ByRef phPrinter As IntPtr, pDefault AsInteger) As Long
 False, CallingConvention := CallingConvention.StdCall)> _
Public Shared Function StartDocPrinter(hPrinter As IntPtr, Level As Integer, ByRef pDocInfo AsDOCINFO) As Long
 True, CallingConvention := CallingConvention.StdCall)> _
Public Shared Function StartPagePrinter(hPrinter As IntPtr) As Long
 True, CallingConvention := CallingConvention.StdCall)> _
Public Shared Function WritePrinter(hPrinter As IntPtr, data As String, buf As Integer, ByRefpcWritten As Integer) As Long
 True, CallingConvention := CallingConvention.StdCall)> _
Public Shared Function EndPagePrinter(hPrinter As IntPtr) As Long
 True, CallingConvention := CallingConvention.StdCall)> _
Public Shared Function EndDocPrinter(hPrinter As IntPtr) As Long
 True, CallingConvention := CallingConvention.StdCall)> _
Public Shared Function ClosePrinter(hPrinter As IntPtr) As Long
End
 Class 'PrintDirect
Public Class App
Public Shared Sub Main()
Dim lhPrinter As New System.IntPtr()
Dim di As New DOCINFO()
Dim pcWritten As Integer = 0
Dim st1 As String
' text to print with a form feed character 
st1 = "This is an example of printing directly to a printer" + ControlChars.FormFeed
di.pDocName = "my test document"
di.pDataType = "RAW"
' the \x1b means an ascii escape character
st1 = ChrW(27) + "*c600a6b0P" + ControlChars.FormFeed
'lhPrinter contains the handle for the printer opened
'If lhPrinter is 0 then an error has occured
PrintDirect.OpenPrinter("\\192.168.1.101\hpl", lhPrinter, 0)
PrintDirect.StartDocPrinter(lhPrinter, 1, di)
PrintDirect.StartPagePrinter(lhPrinter)
Try
' Moves the cursor 900 dots (3 inches at 300 dpi) in from the left margin, and
' 600 dots (2 inches at 300 dpi) down from the top margin.
st1 = ChrW(27) + "*p900x600Y"
PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, pcWritten)
' Using the print model commands for rectangle dimensions, "600a" specifies a rectangle
' with a horizontal size or width of 600 dots, and "6b" specifies a vertical
' size or height of 6 dots. The 0P selects the solid black rectangular area fill.
st1 = ChrW(27) + "*c600a6b0P"
PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, pcWritten)
' Specifies a rectangle with width of 6 dots, height of 600 dots, and a
' fill pattern of solid black.
st1 = ChrW(27) + "*c6a600b0P"
PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, pcWritten)
' Moves the current cursor position to 900 dots, from the left margin and
' 1200 dots down from the top margin.
st1 = ChrW(27) + "*p900x1200Y"
PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, pcWritten)
' Specifies a rectangle with a width of 606 dots, a height of 6 dots and a // fill pattern of solid black.
st1 = ChrW(27) + "*c606a6b0P"
PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, pcWritten)
' Moves the current cursor position to 1500 dots from the left margin and
' 600 dots down from the top margin.
st1 = ChrW(27) + "*p1500x600Y"
PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, pcWritten)
' Specifies a rectangle with a width of 6 dots, a height of 600 dots and a
' fill pattern of solid black.
st1 = ChrW(27) + "*c6a600b0P"
PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, pcWritten) 
' Send a form feed character to the printer
st1 = ControlChars.FormFeed
PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, pcWritten)
Catch e As Exception
Console.WriteLine(e.Message)
End Try
PrintDirect.EndPagePrinter(lhPrinter)
PrintDirect.EndDocPrinter(lhPrinter)
PrintDirect.ClosePrinter(lhPrinter)
End Sub 'Main
End Class 'App