[Python] Execute Python Script in VB.NET


Description
     Sometime in VB.NET we execute some shell script from third party like perl / phyton / other because it's may be have more advantage feature than .NET. this tutorial will help you to make python script can be execute from VB.NET

HowTo
1. Download Python Here (Official)
2. Write your python code , for example : print("Hello world python !!") in text file and save to .py
3. Create project at VB.NET, drag component 1 button and 1 textbox into form.
4. Double click button1, at event click button1 write this code 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim proc As Process = New Process
        proc.StartInfo.FileName = "C:\Python34\python.exe" 'Default Python Installation
        proc.StartInfo.Arguments = pathmypythonfile.py
        proc.StartInfo.UseShellExecute = False 'required for redirect.
        proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden 'don't show commandprompt.
        proc.StartInfo.CreateNoWindow = True
        proc.StartInfo.RedirectStandardOutput = True 'captures output from commandprompt.
        proc.Start()
        AddHandler proc.OutputDataReceived, AddressOf proccess_OutputDataReceived
        proc.BeginOutputReadLine()
        proc.WaitForExit()

        TextBox1.Text = Value
    End Sub

    Public sub proccess_OutputDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
        On Error Resume Next
        If e.Data = "" Then
        Else
            Value = e.Data
        End If
    End sub 

5. Create module file, and write this variable global :
    Module module
           Public Value As String
    End Module

6. Running the application, if textbox1 have populated with some string then your code was success.

[VB.NET] Embed Flash Player (.swf)


Description
     this tutorial will help you to embed flash player (swf) to VB.NET. it's simple just follow the step below.

HowTo
     1. Install Shockwave Player Here
     2. Add Component on toolbox by right click and choose item, after that select COM Components tabs then checklist Shockwave Flash Object and click ok.
     3. New component appear in toolbox (icon flash), just drag and drop it into form and give name "flashPlayer"
     4. Test the flash player by add source file (.swf file). use this code on the form load :
         
         flashPlayer.Movie = My.Application.Info.DirectoryPath & "\fileflash.swf"


if still not understand may be watch this video is the solution :) , thanks for the uploader.



[Python] CRUD in database SQLServer


Description
     This tutorial will help you to make python can connect to database SQLServer and execute some command like CRUD (CREATE, UPDATE, READ, DELETE). the SQLServer library not include in default installation package, so you must download and install manually.     

HowTo
1. Install the latest Python.
2. Download library for SQLServer (Pymssql) from this site
3. After installation successfully, now you use the library. here's sample code for CRUD.


#--------------------------------START HERE-----------------------------------------------------------
import pymssql

conn = pymssql.connect(host='localhost', user='usrnm', password='passwd', Database='mydb')
cur = conn.cursor() 

#CREATE , INSERT , UPDATE, SELECT always use execute command
cur.execute('CREATE TABLE test(id INT, name VARCHAR(100))')
cur.execute("INSERT INTO test(id,name) VALUES('3','mahendra')")
cur.execute("UPDATE test set name='rony' where id='1'")
cur.execute("DELETE from test where id='1'")
conn.commit() # don't forget to commit after manipulating database

#To retrieve data / select you can do this
cur.execute('SELECT * from test')
row = cur.fetchone()

while row:
      #Index column fields in database always from 0
      print (row[0],row[1])
      row = cur.fetchone()

conn.close() #don't forget close connection after all process CRUD complete

[VB.NET] Fade in and Fade Out Image Picture Box


Description
      this source code (function) will make you can control opacity of the image in picturebox. opacity range between 0,0 to 1,0 (it's double not integer). i am not owner of this source code but it's help me for my work.


HowTo

'Here's the function

Public Function FadeInImage(ByVal bmp As Bitmap, ByVal opacity As Single) As Bitmap
Dim bmp2 As New Bitmap(bmp.Width, bmp.Height, Imaging.PixelFormat.Format32bppArgb)
opacity = Math.Max(0, Math.Min(opacity, 1.0F))
Using ia As New Imaging.ImageAttributes
Dim cm As New Imaging.ColorMatrix
cm.Matrix33 = opacity
ia.SetColorMatrix(cm)
Dim destpoints() As PointF = {New Point(0, 0), New Point(bmp.Width, 0), New Point(0, bmp.Height)}
Using g As Graphics = Graphics.FromImage(bmp2)
g.DrawImage(bmp, destpoints, New RectangleF(Point.Empty, bmp.Size), GraphicsUnit.Pixel, ia)
End Using
End Using
Return bmp2
End Function

to call this function :
picturebox1.Image = FadeInImage(Image.FromFile(My.Application.Info.DirectoryPath & "/myimage.png"), imgOpacityValue).

[VB.NET] Open Excel File and Append Value


Description
      This source code will help you how to open excel files from VB.NET then find the LAST ROW, append some value and save the file.

HowTo
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Threading
Imports System.Globalization
Imports System.Net

Dim oexcel As Excel.Application = New Excel.Application()
Dim obook As Excel.Workbook
Dim strPath As String = "d:\myexcelfile.xlsx"
Dim strFaktur As String
Dim flagCounter As Integer

Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
obook = oexcel.Workbooks.Open(strPath, 0, False, 5, _
System.Reflection.Missing.Value, System.Reflection.Missing.Value, _
False, System.Reflection.Missing.Value, System.Reflection.Missing.Value, _
True, False, System.Reflection.Missing.Value, False)

Dim oSheets As Excel.Sheets = obook.Worksheets
Dim oSheet As Excel.Worksheet = oSheets(1)
Dim lastRow As Integer = oSheet.Rows.End(Excel.XlDirection.xlDown).Row + 1

oSheet.Range("A" & lastRow).NumberFormat = "@"
oSheet.Range("A" & lastRow).Value = "this is value in last row "

oexcel.DisplayAlerts = False
Try
obook.SaveAs("d:\myexcelfilenew.xlsx", Excel.XlFileFormat.xlOpenXMLWorkbook)
Catch e As Exception
MsgBox(e.Message & vbCrLf & "Process cancelled !!", vbCritical, "Abort")
End Try
oexcel.Quit()
oexcel = Nothing

[VB.NET] Write to Excel file


Description
     This source code is for generate excel file from vb.net, don't worry it's compatible between version excel in win XP and win 7 and it's solve the issue culture problems.


HowTo
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Threading
Imports System.Globalization
Imports System.Net

Dim oexcel As Excel.Application = New Excel.Application()
Dim obook As Excel.Workbook
Dim osheet As Object
Dim strFaktur As String

Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
oexcel = CreateObject("Excel.Application")
obook = oexcel.Workbooks.Add
osheet = obook.Worksheets(1)

'format cell
osheet.Range("A1").NumberFormat = "@"
'value cell
osheet.Range("A1").Value = "hello world"

oexcel.DisplayAlerts = False
Try
     obook.SaveAs(pathsave, Excel.XlFileFormat.xlOpenXMLWorkbook)
Catch e As Exception
     MsgBox(e.Message & vbCrLf & "Process cancelled !!", vbCritical, "Abort")
End Try

oexcel.Quit()
killCurrentExcel(oexcel) ' you can find this function in different article
oexcel = Nothing