Imports System.Linq Imports System.Data.SqlClient Imports System.Data.OleDb Imports Microsoft.Office.Core Imports Excel = Microsoft.Office.Interop.Excel Imports ExcelAutoFormat = Microsoft.Office.Interop.Excel.XlRangeAutoFormat Imports Microsoft.Office.Interop Imports System.IO Imports System.Xml.XPath Imports System.Data Imports System.Xml Public Class Form1 Private ListItemType As Object Public Property DataGrid1 As Object Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.CenterToScreen() End Sub Private Sub TextBoxNumberOfRows_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBoxNumberOfRows.KeyPress If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack Or e.KeyChar = "+") Then MessageBox.Show("Invalid Input! Numbers Only.", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error) e.Handled = True End If End Sub Private Sub ButtonDataCreate_Click(sender As Object, e As EventArgs) Handles ButtonDataCreate.Click If TextBoxNumberOfRows.Text = "" Then MessageBox.Show("Number of Row cannot be empty !!!", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error) Return End If ButtonCreateData.Text = "Please Wait..." ButtonCreateData.Enabled = False For i As Integer = 1 To TextBoxNumberOfRows.Text With DataGridView1.Rows .Add(i, "10000" & i, "OPTIFAST SHAKE", "061-" & i) End With Next ButtonCreateData.Text = "Make Data" ButtonCreateData.Enabled = True End Sub Private Sub ButtonExporttoExcel_Click(sender As Object, e As EventArgs) Handles ExporttoExcel.Click Try ExporttoExcel.Text = "Please Wait..." ExporttoExcel.Enabled = False SaveFileDialog1.Filter = "Excel Document (*.xlsx)|*.xlsx" If SaveFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then Dim xlApp As Microsoft.Office.Interop.Excel.Application Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet Dim misValue As Object = System.Reflection.Missing.Value Dim i As Integer Dim j As Integer xlApp = New Microsoft.Office.Interop.Excel.Application xlWorkBook = xlApp.Workbooks.Add(misValue) xlWorkSheet = xlWorkBook.Sheets("sheet1") For i = 0 To DataGridView1.RowCount - 2 For j = 0 To DataGridView1.ColumnCount - 1 For k As Integer = 1 To DataGridView1.Columns.Count xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString() Next Next Next xlWorkSheet.SaveAs(SaveFileDialog1.FileName) xlWorkBook.Close() xlApp.Quit() releaseObject(xlApp) releaseObject(xlWorkBook) releaseObject(xlWorkSheet) MsgBox("Successfully saved" & vbCrLf & "File are saved at : " & SaveFileDialog1.FileName, MsgBoxStyle.Information, "Information") ExporttoExcel.Text = "Export To MS Excel" ExporttoExcel.Enabled = True End If Catch ex As Exception MessageBox.Show("Failed to save !!!", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error) Return End Try End Sub Private Sub releaseObject(obj As Object) Try System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) obj = Nothing Catch ex As Exception obj = Nothing Finally GC.Collect() End Try End Sub Private Sub ButtonClearDVG_Click(sender As Object, e As EventArgs) Handles ButtonClearDVG.Click DataGridView1.Columns.Clear() If DataGridView1.Columns.Count = 0 Then With DataGridView1 .Columns.Clear() .Columns.Add("No", "No") .Columns.Add("PLU", "PLU") .Columns.Add("DESCRIPTION", "DESCRIPTION") .Columns.Add("QTY", "QTY") End With End If DataGridView1.DataSource = Nothing End Sub Private Sub ButtonImportFromExcel_Click(sender As Object, e As EventArgs) Handles ButtonImportFromExcel.Click Dim conn As OleDbConnection Dim dtr As OleDbDataReader Dim dta As OleDbDataAdapter Dim cmd As OleDbCommand Dim dts As DataSet Dim excel As String Dim OpenFileDialog As New OpenFileDialog OpenFileDialog1.FileName = "" OpenFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop OpenFileDialog1.Filter = "All Files (*.*)|*.*|Excel files (*.xlsx)|*.xlsx|CSV Files (*.csv)|*.csv|XLS Files (*.xls)|*xls" If (OpenFileDialog1.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then DataGridView1.Columns.Clear() Dim fi As New FileInfo(OpenFileDialog1.FileName) Dim FileName As String = OpenFileDialog1.FileName excel = fi.FullName conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excel + ";Extended Properties=Excel 12.0;") dta = New OleDbDataAdapter("Select * From [Sheet1$]", conn) dts = New DataSet dta.Fill(dts, "[Sheet1$]") DataGridView1.DataSource = dts DataGridView1.DataMember = "[Sheet1$]" conn.Close() End If End Sub Private Class ButtonCreateData Public Shared Property Text As String Public Shared Property Enabled As Boolean End Class End Class Class DataGridItem Private v1 As Integer Private v2 As Integer Private header As Object Public Sub New(v1 As Integer, v2 As Integer, header As Object) Me.v1 = v1 Me.v2 = v2 Me.header = header End Sub Friend Function Cells() As Object Throw New NotImplementedException() End Function End Class
Write, Run & Share VB.net code online using OneCompiler's VB.net online compiler for free. It's one of the robust, feature-rich online compilers for VB.net language, running on the latest version 16. Getting started with the OneCompiler's VB.net compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as VB.net
. OneCompiler also has reference programs, where you can look for the sample code to get started with.
OneCompiler's VB.net online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample VB.net program which takes name as input and prints hello message with your name.
Public Module Program
Public Sub Main(args() As string)
Dim name as String = Console.ReadLine() ' Reading input from STDIN
Console.WriteLine("Hello " & name) ' Writing output to STDOUT
End Sub
End Module
Visual Basic is a event driven programming language by Microsoft, first released in the year 1991.
Variable is a name given to the storage area in order to identify them in our programs.
Simple syntax of Variable declaration is as follows
Dim variableName [ As [ New ] dataType ] [ = initializer ]
variableName = value
If condition-expression Then
'code
End If
If(conditional-expression)Then
'code if the conditional-expression is true
Else
'code if the conditional-expression is false
End If
If(conditional-expression)Then
'code if the above conditional-expression is true
Else If(conditional-expression) Then
'code if the above conditional-expression is true
Else
'code if the above conditional-expression is false
End If
If(conditional-expression)Then
'code if the above conditional-expression is true
If(conditional-expression)Then
'code if the above conditional-expression is true
End If
End If
Select [ Case ] expression
[ Case expressionlist
'code ]
[ Case Else
'code ]
End Select
For counter [ As datatype ] = begin To end [ Step step ]
'code
[ Continue For ]
'code
[ Exit For ]
'code
Next [ counter ]
For Each element [ As datatype ] In group
'code
[ Continue For ]
'code
[ Exit For ]
'code
Next [ element ]
While conditional-expression
'Code
[ Continue While ]
'Code
[ Exit While ]
'Code
End While
Do { While | Until } conditional-expression
'Code
[ Continue Do ]
'Code
[ Exit Do ]
'Code
Loop
Do
'Code
[ Continue Do ]
'Code
[ Exit Do ]
'Code
Loop { While | Until } conditional-expression
Procedure is a sub-routine which contains set of statements. Usually Procedures are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
Procedures are of two types.
Functions return a value when they are called.
[accessModifiers] Function functionName [(parameterList)] As returnType
'code
End Function
Sub-procedures are similar to functions but they don't return any value.
Sub ProcedureName (parameterList)
'Code
End Sub