Search This Blog

Showing posts with label VB.NET. Show all posts
Showing posts with label VB.NET. Show all posts

Thursday, December 5, 2013

AddHandler Visual Basic Example

VB.NET > Statements > AddHandler

AddHandler link an event with an event handler at run time.

Example:

1. Add event to an object

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim Obj As New Class1
        ' Associate an event handler with an event
        AddHandler Obj.myEvent, AddressOf EvHandler
        Obj.Class1Event()
    End Sub

    Sub EvHandler()
        ' Handle the event
        MsgBox("EvHandler")
    End Sub

    Public Class Class1
        Public Event myEvent()
        Public Sub Class1Event()
            RaiseEvent myEvent()
        End Sub
    End Class

End Class

2. Handle events for dynamic runtime control





Wednesday, November 27, 2013

Form VB.NET

VB.NET > Form

Form

Is a representation of any window displayed in an application.

Type
  • standard
  • tool
  • borderless
  • floating
Example

Create dialog box in runtime

Dim form1 As New Form()

Dim btnYes As New Button()
Dim btnNo As New Button()
btnYes.Text = "Yes"
btnYes.Location = New Point(5, 5)
btnNo.Text = "No"
btnNo.Location = New Point(btnYes.Left, btnYes.Top + 30)

form1.Controls.Add(btnYes)
form1.Controls.Add(btnNo)

form1.Text = "Confirm Dialog Box"
form1.FormBorderStyle = FormBorderStyle.FixedDialog
form1.MaximizeBox = False
form1.MinimizeBox = False
form1.AcceptButton = btnYes
form1.CancelButton = btnNo
form1.StartPosition = FormStartPosition.CenterScreen

form1.ShowDialog()





Object-Oriented Programming VB.NET

VB.NET > Object-Oriented Programming VB.NET

Visual Basic .NET has been entirely rewritten to be fully object-oriented and everything in Visual Basic .NET can be treated as an object.

OOP language:
  • Abstraction
  • Encapsulation
  • Polymorphism
  • Inheritance
Examplea




System Namespace VB.NET

VB.NET > System namespace

System namespace in VB.NET contains fundamental classes and base classes.

String






Saturday, September 21, 2013

Read Connection String From Web.Config VB.Net

VB.NET > Data > SqlConnection > ConnectionString
 
Dim con As SqlConnection
Dim conString As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ToString
con = New SqlConnection(conString)
con.Open()




Thursday, July 18, 2013

Asc Method Visual Basic

VB.NET > Functions > ASC

Asc Method returns an Integer value of character code corresponding to a character.

Example:

Dim IntCode As Integer
IntCode = Asc("A") '65
IntCode = Asc("a") '97
IntCode = Asc("@") '64
IntCode = Asc("?") '93





CType Function Visual Basic NET

VB.NET > Functions > CType

CType function returns the result of explicitly converting an expression to a specified data type.

Example:

Public Class Form1
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim mStr As String = "100"
    Dim mInt As Integer = CType(mStr, Integer) 'convert "100" string to mInt = 100 OK

    mStr = "aaa"
    mInt = CType(mStr, Integer) ' Error: Conversion from string "aaa" to type 'Integer' is not valid. NOK
  End Sub
End Class






Monday, July 15, 2013

Abstract classes, MustInherit, MustOverride , Overrides VB NET

VB.NET > OOP > Abstract Class

Abstract classes specify members that must be implemented in the inheriting classes. Any class can inherit only from one abstract class. You we cannot create objects of an abstract class.

MustInherit
Specifies that a class can be used only as a base class and that you cannot create an object directly from it.

MustOverride  
Specifies that a property or procedure is not implemented in this class and must be overridden in a derived class before it can be used.

Overrides
Specifies that a property or procedure overrides an identically named property or procedure inherited from a base class.




Example:

Public Class Form1
  Public MustInherit Class Employee

    Private mFirstName As String
    Private mLastName As String

    Public Sub New(ByVal FirstName, ByVal LastName)
      mFirstName = FirstName
      mLastName = LastName
    End Sub

    Public Property FirstName() As String
      Get
        Return mFirstName
      End Get
      Set(ByVal value As String)
        mFirstName = value
      End Set
    End Property

    Public Property LastName() As String
      Get
        Return mLastName
      End Get
      Set(ByVal value As String)
        mLastName = value
      End Set
    End Property

    Public MustOverride Function Income() As Double
  End Class

  Public Class Teacher
    Inherits Employee

    Private mSalary As Double

    Public Sub New(ByVal FirstName, ByVal Lastname, ByVal salary)
      MyBase.New(FirstName, Lastname)
      mSalary = salary
    End Sub

    Public Property Salary() As String
      Get
        Return mSalary
      End Get
      Set(ByVal value As String)
        mSalary = value
      End Set
    End Property

    Public Overrides Function Income() As Double
      Return Salary() * 1.2
    End Function
  End Class

  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim mTeacher As New Teacher("John", "Smith", 100)
    Dim sal = mTeacher.Income ' 120
  End Sub

End Class