Search This Blog

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

Monday, June 3, 2013

Arrow keys not trigger VB NET

VB.NET > Form > Methods > ProcessCmdKey

ProcessCmdKey method processes a command key.

Example:
Use ProcessCmdKey to catch arrow keys (arrow keys not trigger VB NET in form events)

Solution:
   
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keydata As Keys) As Boolean
        If keydata = Keys.Right Or keydata = Keys.Left Or keydata = Keys.Up Or keydata = Keys.Down Then
            OnKeyDown(New KeyEventArgs(keydata))
            ProcessCmdKey = True
        Else
            ProcessCmdKey = MyBase.ProcessCmdKey(msg, keydata)
        End If
    End Function

Private Sub RunForm_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Right Then
        End If
End Sub

 






Tuesday, January 29, 2013

Filter DataTable with LINQ (Visual Basic)


VB.NET > Data > DataTable > Filter with LINQ

Use AsEnumerable method to return the input type DataTable as IEnumerable.
The CopyToDataTable method takes the results of a query and copies the data into a DataTable.






Example:

Public Class Form1

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

    Dim dt As DataTable = New DataTable("table")
    dt.Columns.Add("id", Type.GetType("System.Int32"))
    dt.Columns.Add("name", Type.GetType("System.String"))

    Dim dr As DataRow = dt.NewRow()
    dr("id") = 1
    dr("name") = "john"
    dt.Rows.Add(dr)

    dr = dt.NewRow()
    dr("id") = 2
    dr("name") = "dan"
    dt.Rows.Add(dr)

    Dim filteredTable As DataTable = (From n In dt.AsEnumerable()
        Where n.Field(Of Int32)("id") = 1
                    Select n).CopyToDataTable()

    ComboBox1.DataSource = filteredTable
    ComboBox1.DisplayMember = "name"
    ComboBox1.ValueMember = "id"
  End Sub

End Class






Tuesday, January 22, 2013

Stop Statement (VB.NET)

VB.Net > StatementsStop

Stop is a programmatic alternative to setting a breakpoint and suspends the execution. When the debugger finds Stop statement, it breaks execution of the program.

Note: Unlike End, it does not close any files or clear any variables.

Example:

Dim i As Integer
Dim j As Integer
For i = 1 To 100
 j = i + 1
 Stop
Next i







With...End With Statement (VB.NET)

VB.NET > Statements > With...End With

Executes a series of statements that repeatedly refers to a single object or structure.

By using With...End With, you can perform a series of statements on a specified object without specifying the name of the object multiple times.

Example:


Public Class Form1
  Private Class Customer
    Public Property Id As String
    Public Property Name As String
  End Class
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    With New Customer
      .Id = 1
      .Name = "Elsoft"
      MessageBox.Show(.Name)
    End With
  End Sub




Monday, January 21, 2013

Terminate application (VB.Net)

VB.NET > Statements > End

End  force the application to stop running.
  • closes files opened Open statement
  • clears all the application variables

Example:

End application on button click

Public Class Form1

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

    End Sub

    Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
        If MsgBox("Do you want to exit?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            End
        End If
    End Sub

End Class




Monday, January 14, 2013

Concatenate string VB.NET

VB.NET > String > Concatenate

You can use +, & operators and Concat method.
Concat concatenates one or more instances of String.

The + and & operators are not identical in VB.NET.
  • & is only used for string concatenation
  • + is overloaded to do both string concatenation and arithmetic addition.

Example

1. Using & operator with strings

Dim str1 As String = "Visual "
Dim str2 As String = "Basic"
' Concatenate
Dim str3 = str1 & str2
MessageBox.Show(str3)


 
2. Using & operator with string and number

Dim str1 As String = "Visual Basic "
Dim str2 As Integer = 6

' Concatenate
Dim str3 = str1 & str2
MessageBox.Show(str3)


3. Concat Method

Dim str1 As String = "Visual"
Dim str2 As Integer = 6
' Concatenate
Dim str3 = String.Concat(str1, " ", str2)
MessageBox.Show(str3)





Thursday, January 10, 2013

First and Last day of a Month VB .NET

VB.NET > Functions > > DateSerial

Returns a Date value representing a specified year, month, and day, with the time information set to midnight (00:00:00).

Examples


First and last day of a month

VB.net
' first day of month
FirstDay = DateSerial(Today.Year, Today.Month, 1)
' month's last day
LastDay = DateSerial(Today.Year, Today.Month , 0)

C#

DateTime firstDayOfTheMonth = new DateTime(DateTime.Today.Year,DateTime.Today.Month, 1);
DateTime lastDayOfTheMonth = firstDayOfTheMonth.AddMonths(1).AddDays(-1);






Friday, December 21, 2012

Class with events VB NET

VB.NET > Class > WithEvents

WithEvents specifies that one or more declared member variables refer to an instance of a class that can raise events.

Example: class with events

 Public Class Export
        Public Event TimesUp()
        Public Sub New()
            Dim worker As New BackgroundWorker()
            AddHandler worker.DoWork, AddressOf DoWork
            AddHandler worker.RunWorkerCompleted, AddressOf bg_RunWorkerCompleted
            worker.RunWorkerAsync()
        End Sub
        Private Sub DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
            ' export stuff
            Thread.Sleep(5000)
        End Sub
        Private Sub bg_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
            MsgBox("Background worker completed", vbInformation, "Background worker")
            RaiseEvent TimesUp() 'Triggers an event declared at module level within a class, form, or document.
        End Sub
    End Class

    Dim WithEvents _Export As New Export
    Private Sub handleTimesUp() Handles _Export.TimesUp
        MsgBox("TimesUp Event Raised and Handled")
    End Sub

 





BackgroundWorker VB.NET

VB.NET > BackgroundWorker

Executes an operation on a separate thread.

DoWork event occurs when RunWorkerAsync is called.
RunWorkerCompleted event occurs when the background operation has completed, has been canceled, or has raised an exception.

Example:

Imports System.ComponentModel
Imports System.Threading
Module Module1

  Public Sub backWork()
    Dim worker As New BackgroundWorker()
    AddHandler worker.DoWork, AddressOf DoWork
    AddHandler worker.RunWorkerCompleted, AddressOf bg_RunWorkerCompleted
    worker.RunWorkerAsync()
  End Sub

  Private Sub DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
    ' some work
    Thread.Sleep(5000)
  End Sub

  Private Sub bg_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
    MsgBox("Background worker completed", vbInformation, "Background worker")
  End Sub

End Module





Wednesday, November 28, 2012

Handle events for dynamic run-time controls - VB.NET

VB.NET > Statements > AddHandler > Add event to dynamic control

Use AddHandler and AddressOf to add event to dynamic control

Example

Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Text
Imports System.Windows.Forms
Imports System.Drawing.Drawing2D
Imports System

Public Class Form1

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  Dim btn = New Button() //add dynamic control to form
  btn.Text = "Click here"
  btn.Size = New System.Drawing.Size(100, 100)
  btn.Location = New System.Drawing.Point(50, 50)
  AddHandler btn.Click, AddressOf Button1_Click  //add event to control
  Me.Controls.Add(btn)
End Sub

Protected Sub Button1_Click(sender As System.Object, e As System.EventArgs)
  Dim btn As Button = sender
  MsgBox(btn.Text)
End Sub

End Class