Sunday, January 17, 2010

Delegate In VB.Net:

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents radioADD As System.Windows.Forms.RadioButton
Friend WithEvents radioSub As System.Windows.Forms.RadioButton
Friend WithEvents radioDiv As System.Windows.Forms.RadioButton
Friend WithEvents radioMult As System.Windows.Forms.RadioButton
Friend WithEvents txtNum1 As System.Windows.Forms.TextBox
Friend WithEvents txtRes As System.Windows.Forms.TextBox
Friend WithEvents txtNum2 As System.Windows.Forms.TextBox
Friend WithEvents lblNum1 As System.Windows.Forms.Label
Friend WithEvents lblNum2 As System.Windows.Forms.Label
Friend WithEvents lblres As System.Windows.Forms.Label
Friend WithEvents btnOperation As System.Windows.Forms.Button
Private Sub InitializeComponent()
Me.radioADD = New System.Windows.Forms.RadioButton()
Me.radioSub = New System.Windows.Forms.RadioButton()
Me.radioDiv = New System.Windows.Forms.RadioButton()
Me.radioMult = New System.Windows.Forms.RadioButton()
Me.txtNum1 = New System.Windows.Forms.TextBox()
Me.txtRes = New System.Windows.Forms.TextBox()
Me.txtNum2 = New System.Windows.Forms.TextBox()
Me.btnOperation = New System.Windows.Forms.Button()
Me.lblNum1 = New System.Windows.Forms.Label()
Me.lblNum2 = New System.Windows.Forms.Label()
Me.lblres = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
'radioADD
'
Me.radioADD.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.radioADD.Location = New System.Drawing.Point(48, 40)
Me.radioADD.Name = "radioADD"
Me.radioADD.Size = New System.Drawing.Size(64, 24)
Me.radioADD.TabIndex = 0
Me.radioADD.Text = "Add"
'
'radioSub
'
Me.radioSub.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.radioSub.Location = New System.Drawing.Point(48, 72)
Me.radioSub.Name = "radioSub"
Me.radioSub.Size = New System.Drawing.Size(88, 24)
Me.radioSub.TabIndex = 1
Me.radioSub.Text = "substract"
'
'radioDiv
'
Me.radioDiv.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.radioDiv.Location = New System.Drawing.Point(48, 136)
Me.radioDiv.Name = "radioDiv"
Me.radioDiv.Size = New System.Drawing.Size(72, 24)
Me.radioDiv.TabIndex = 3
Me.radioDiv.Text = "Divide"
'
'radioMult
'
Me.radioMult.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.radioMult.Location = New System.Drawing.Point(48, 104)
Me.radioMult.Name = "radioMult"
Me.radioMult.Size = New System.Drawing.Size(88, 24)
Me.radioMult.TabIndex = 2
Me.radioMult.Text = "Multiply"
'
'txtNum1
'
Me.txtNum1.Location = New System.Drawing.Point(288, 56)
Me.txtNum1.Name = "txtNum1"
Me.txtNum1.Size = New System.Drawing.Size(112, 20)
Me.txtNum1.TabIndex = 4
Me.txtNum1.Text = ""
'
'txtRes
'
Me.txtRes.Location = New System.Drawing.Point(288, 120)
Me.txtRes.Name = "txtRes"
Me.txtRes.Size = New System.Drawing.Size(112, 20)
Me.txtRes.TabIndex = 6
Me.txtRes.Text = ""
'
'txtNum2
'
Me.txtNum2.Location = New System.Drawing.Point(288, 88)
Me.txtNum2.Name = "txtNum2"
Me.txtNum2.Size = New System.Drawing.Size(112, 20)
Me.txtNum2.TabIndex = 5
Me.txtNum2.Text = ""
'
'btnOperation
'
Me.btnOperation.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnOperation.Location = New System.Drawing.Point(280, 176)
Me.btnOperation.Name = "btnOperation"
Me.btnOperation.Size = New System.Drawing.Size(128, 40)
Me.btnOperation.TabIndex = 7
Me.btnOperation.Text = "DO OPERATION"
'
'lblNum1
'
Me.lblNum1.AutoSize = True
Me.lblNum1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblNum1.Location = New System.Drawing.Point(184, 56)
Me.lblNum1.Name = "lblNum1"
Me.lblNum1.Size = New System.Drawing.Size(76, 15)
Me.lblNum1.TabIndex = 8
Me.lblNum1.Text = "NUMBER 1"
'
'lblNum2
'
Me.lblNum2.AutoSize = True
Me.lblNum2.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblNum2.Location = New System.Drawing.Point(184, 88)
Me.lblNum2.Name = "lblNum2"
Me.lblNum2.Size = New System.Drawing.Size(76, 15)
Me.lblNum2.TabIndex = 9
Me.lblNum2.Text = "NUMBER 2"
'
'lblres
'
Me.lblres.AutoSize = True
Me.lblres.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblres.Location = New System.Drawing.Point(190, 120)
Me.lblres.Name = "lblres"
Me.lblres.Size = New System.Drawing.Size(59, 15)
Me.lblres.TabIndex = 10
Me.lblres.Text = "RESULT"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(536, 266)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblres, Me.lblNum2, Me.lblNum1, Me.btnOperation, Me.txtRes, Me.txtNum2, Me.txtNum1, Me.radioDiv, Me.radioMult, Me.radioSub, Me.radioADD})
Me.Name = "Form1"
Me.Text = "DELEGATE through VB.net::"
Me.ResumeLayout(False)

End Sub

#End Region

Delegate Function DelProcees(ByVal x As Double, ByVal y As Double) As Double
Public str As String

Private Sub btnOperation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOperation.Click
Dim x, y, z As Double
Dim proc As DelProcees
x = Val(txtNum1.Text)
y = Val(txtNum2.Text)
Select Case (str)
Case "add"
proc = New DelProcees(AddressOf Add)
Case "sub"
proc = New DelProcees(AddressOf Min)
Case "div"
proc = New DelProcees(AddressOf Div)
Case "mul"
proc = New DelProcees(AddressOf Mul)
End Select
z = proc(x, y)
txtRes.Text = z.ToString
End Sub

Private Function Add(ByVal x As Double, ByVal y As Double) As Double
Return (x + y)
End Function

Private Function Min(ByVal x As Double, ByVal y As Double) As Double
Return (x - y)
End Function

Private Function Mul(ByVal x As Double, ByVal y As Double) As Double
Return (x * y)
End Function

Private Function Div(ByVal x As Double, ByVal y As Double) As Double
Try
Return (x / y)
Catch exe As ArithmeticException
MessageBox.Show(exe.Message.ToString)
End Try
End Function

Private Sub radioADD_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radioADD.CheckedChanged
str = "add"
End Sub

Private Sub radioSub_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radioSub.CheckedChanged
str = "sub"
End Sub

Private Sub radioMult_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radioMult.CheckedChanged
str = "mul"
End Sub

Private Sub radioDiv_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radioDiv.CheckedChanged
str = "div"
End Sub

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

End Sub
End Class

No comments:

Post a Comment