Search This Blog

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

Monday, September 28, 2015

Choose Visual Basic Example

VB.NET Functions > Choose

Returns a member of the list passed in Choice().
It is based on the value of Index. The first member of the list is selected when Index is 1.

Example

Dim choice = Choose(2, "val1", "val2", "val2") // "val2"





Monday, July 20, 2015

Day VB.Net Example

VB.NET > Functions > Day

Day returns the day of the month as an integer between 1 and 31.

Example


Dim today As Integer = Microsoft.VisualBasic.DateAndTime.Day(Now)






Friday, August 29, 2014

How to get if is Runtime or Design Mode VB.net

VB.NET > Control > DesignMode

DesignMode returns true is a control is being used in the context of a designer. 

Example:

If Me.DesignMode Then
      Return
End If





Friday, March 21, 2014

DateValue Visual Basic Example

VB.NET Functions > DateValue 

Returns a date value containing the date information represented by a string.

Example

Dim dt As Date
dt = DateValue("March 21, 2014")

MessageBox.Show(dt)






Wednesday, March 19, 2014

DateDiff Visual Basic Example

VB.NET Functions > DateDiff

Returns a value specifying the number of time intervals 
between two dates.

Example:


Dim dt1 As Date = #3/1/2014#
Dim dt2 As Date = #4/1/2014#

MessageBox.Show("Day diff: " & DateDiff(DateInterval.Day, dt1, dt2) & " Week diff: " & DateDiff(DateInterval.Weekday, dt1, dt2))






Monday, March 10, 2014

AppActivate Visual Basic

VB.NET Functions > AppActivate

Activates an application that is already running.

Example:
Start and activate notepad

AppActivate(Shell("C:\Windows\System32\notepad.exe", AppWinStyle.NormalFocus))




CurDir Visual Basic

VB.NET Functions > CurDir 

Returns the current path.

Example:


MessageBox.Show(CurDir())







Wednesday, March 5, 2014

Call Statement VB.NET Example

VB.NET > Statements > Call


Call transfers control to a function or dynamic-link library.
Call keyword is used when the called expression doesn't start with an identifier.


Example:


Class mClass
    Public Sub Method()
        MessageBox.Show("Call statement")
    End Sub
End Class

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Call New mClass().Method()
    End Sub

End Class





Tuesday, March 4, 2014

Rss Feed Reader Vb.net Example

VB.NET > XML > Rss Feed Reader






Example


 Dim url As String = "http://rss.cnn.com/rss/cnn_latest.rss"
        Dim rssFeed As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
        Dim response = rssFeed.GetResponse()
        Dim rssStream = response.GetResponseStream()

        Dim rssDoc As New XmlDocument()
        rssDoc.Load(rssStream)
        Dim rssItems As XmlNodeList = rssDoc.SelectNodes("rss/channel/item")
        Dim i As Integer = 0
        Dim dt As DataTable = New DataTable("table")
        dt.Columns.Add("title", Type.GetType("System.String"))
        dt.Columns.Add("link", Type.GetType("System.String"))

        While i < rssItems.Count
            Dim node As XmlNode = rssItems.Item(i).SelectSingleNode("title")
            Dim title As String
            Dim link As String
            If node IsNot Nothing Then
                title = node.InnerText
            Else
                title = ""
            End If

            node = rssItems.Item(i).SelectSingleNode("link")
            If node IsNot Nothing Then
                link = node.InnerText
            Else
                link = ""
            End If
            Dim dr As DataRow = dt.NewRow()
            dr("title") = title
            dr("link") = link

            dt.Rows.Add(dr)

            i += 1
        End While

        DataGridView1.DataSource = dt


#Region Directive Visual Basic Example

Visual Basic > Directives > #Region

Collapses and hides sections of code.

Example:

Public Class Form1
#Region "Person"
    Public Class Person

    End Class
#End Region
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

End Class








Thursday, January 30, 2014

LINQ DataTable compare date VB.NET

VB.NET > Data > DataTable > Filter with LINQ

LINQ DataTable compare date VB.NET example

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

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

        dr = dt.NewRow()
        dr("id") = 2
        dr("name") = "dan"
        dr("date_of_birth") = #9/11/1973#
        dt.Rows.Add(dr)

        Dim filteredTable As DataTable = (From n In dt.AsEnumerable()
                            Where n.Field(Of Date)("date_of_birth") = #9/11/1973#
                    Select n).CopyToDataTable()




Wednesday, January 29, 2014

DateAdd VB.NET Example

VB.NET > Functions > DateAdd

DateAdd: add or subtract a specified time interval from a date.

Example:

Label1.Text = Date.Now
Label2.Text = DateAdd(DateInterval.Day, 1, Date.Now)
Label3.Text = DateAdd(DateInterval.Month, 1, Date.Now)
Label4.Text = DateAdd(DateInterval.Year, 1, Date.Now)




Wednesday, December 11, 2013

Delete row from DataTable using LINQ VB.NET example

VB.NET > DataTable > Delete row with LINQ

Here is an example of deleting row from DataTable (dt) supposing you want to delete the row with id 1.


Dim row As DataRow = dt.AsEnumerable().SingleOrDefault(Function(r) r(0) = 1)


If Not row Is Nothing Then
    row.Delete()
End If
 
 





Add AutoIncrement Row Values to DataTable Having AutoIncrement and PrimaryKey DataColumn

VB.NET > DataGridView > Add AutoIncrement Row Values

Example how to add AutoIncrement and PrimaryKey DataColumn values to DataTable








VB.NET


  Dim dt As DataTable = New DataTable

  dt.Columns.Add("ID", GetType(Integer))
  dt.PrimaryKey = New DataColumn() {dt.Columns("ID")}
  dt.Columns("ID").AutoIncrement = True
  dt.Columns("ID").AutoIncrementSeed = 1
  dt.Columns("ID").ReadOnly = True

  dt.Columns.Add("Name", GetType(String))

  Dim dr = dt.NewRow()
  dr("Name") = "John"
  dt.Rows.Add(dr)

  dr = dt.NewRow()
  dr("Name") = "Dan"
   dt.Rows.Add(dr)

  DataGridView1.DataSource = dt

C#

DataTable dt = new DataTable();

dt.Columns.Add("ID", typeof(int));
dt.PrimaryKey = new DataColumn[] { dt.Columns["ID"] };
dt.Columns["ID"].AutoIncrement = true;
dt.Columns["ID"].AutoIncrementSeed = 1;
dt.Columns["ID"].ReadOnly = true;

dt.Columns.Add("Name", typeof(string));

dynamic dr = dt.NewRow();
dr("Name") = "John";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr("Name") = "Dan";
dt.Rows.Add(dr);

DataGridView1.DataSource = dt;