lunes, 21 de mayo de 2012

Generar codigos de Barras en Visual Basic .Net

Imagen de la Aplicación:

Resultado de la impresion:



Para generar codigos de barras instalaremos previamente la fuente:3 of 9 Barcode

y el codigo de la aplicacion a continuación...



Imports System.Drawing.Printing

Public Class Form1
Dim Contenido As PrintPageEventArgs
Dim WithEvents Documento As New PrintDocument
Dim impresora As New PrintDialog


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Configurar()
Documento.Print()
End Sub

Private Sub Documento_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles Documento.PrintPage
Contenido = e
Contenido.Graphics.DrawString("*" & Me.TextBox1.Text & "*", New Font("3 of 9 Barcode", 14), Brushes.Black, 50, 300)
Contenido.Graphics.DrawString(Me.TextBox1.Text, New Font("Arial", 7), Brushes.Black, 90, 315)
Contenido.HasMorePages = False
End Sub

Private Sub Configurar()
impresora.Document = Documento
impresora.ShowDialog()
Documento.PrinterSettings = impresora.PrinterSettings
End Sub

End Class

Numero a Letras

Este sencillo codigo nos permite convertir numeros a letras:
Se copia y pega la funcion en tu codigo fuente:

Public Function Num2Text(ByVal value As Double) As String
Select Case value
Case 0 : Num2Text = "CERO"
Case 1 : Num2Text = "UN"
Case 2 : Num2Text = "DOS"
Case 3 : Num2Text = "TRES"
Case 4 : Num2Text = "CUATRO"
Case 5 : Num2Text = "CINCO"
Case 6 : Num2Text = "SEIS"
Case 7 : Num2Text = "SIETE"
Case 8 : Num2Text = "OCHO"
Case 9 : Num2Text = "NUEVE"
Case 10 : Num2Text = "DIEZ"
Case 11 : Num2Text = "ONCE"
Case 12 : Num2Text = "DOCE"
Case 13 : Num2Text = "TRECE"
Case 14 : Num2Text = "CATORCE"
Case 15 : Num2Text = "QUINCE"
Case Is < 20 : Num2Text = "DIECI" & Num2Text(value - 10)
Case 20 : Num2Text = "VEINTE"
Case Is < 30 : Num2Text = "VEINTI" & Num2Text(value - 20)
Case 30 : Num2Text = "TREINTA"
Case 40 : Num2Text = "CUARENTA"
Case 50 : Num2Text = "CINCUENTA"
Case 60 : Num2Text = "SESENTA"
Case 70 : Num2Text = "SETENTA"
Case 80 : Num2Text = "OCHENTA"
Case 90 : Num2Text = "NOVENTA"
Case Is < 100 : Num2Text = Num2Text(Int(value \ 10) * 10) & " Y " & Num2Text(value Mod 10)
Case 100 : Num2Text = "CIEN"
Case Is < 200 : Num2Text = "CIENTO " & Num2Text(value - 100)
Case 200, 300, 400, 600, 800 : Num2Text = Num2Text(Int(value \ 100)) & "CIENTOS"
Case 500 : Num2Text = "QUINIENTOS"
Case 700 : Num2Text = "SETECIENTOS"
Case 900 : Num2Text = "NOVECIENTOS"
Case Is < 1000 : Num2Text = Num2Text(Int(value \ 100) * 100) & " " & Num2Text(value Mod 100)
Case 1000 : Num2Text = "MIL"
Case Is < 2000 : Num2Text = "MIL " & Num2Text(value Mod 1000)
Case Is < 1000000 : Num2Text = Num2Text(Int(value \ 1000)) & " MIL"
If value Mod 1000 Then Num2Text = Num2Text & " " & Num2Text(value Mod 1000)
Case 1000000 : Num2Text = "UN MILLON"
Case Is < 2000000 : Num2Text = "UN MILLON " & Num2Text(value Mod 1000000)
Case Is < 1000000000000.0# : Num2Text = Num2Text(Int(value / 1000000)) & " MILLONES "
If (value - Int(value / 1000000) * 1000000) Then Num2Text = Num2Text & " " & Num2Text(value - Int(value / 1000000) * 1000000)
Case 1000000000000.0# : Num2Text = "UN BILLON"
Case Is < 2000000000000.0# : Num2Text = "UN BILLON " & Num2Text(value - Int(value / 1000000000000.0#) * 1000000000000.0#)
Case Else : Num2Text = Num2Text(Int(value / 1000000000000.0#)) & " BILLONES"
If (value - Int(value / 1000000000000.0#) * 1000000000000.0#) Then Num2Text = Num2Text & " " & Num2Text(value - Int(value / 1000000000000.0#) * 1000000000000.0#)
End Select
End Function

Para invocar a la Funcion:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim D As Double = CDbl(Me.TextBox1.Text)
Dim parteEntera As Integer = Math.Truncate(D)
Dim parteDecimal As Integer = Math.Round(((D - parteEntera) * 100), 2)

If (parteDecimal > 0) Then
Me.Label1.Text = Num2Text(parteEntera) & " PESOS " & parteDecimal & "/100 MN"
Else
Me.Label1.Text = Num2Text(parteEntera) & "PESOS 00/100 MN"
End If
End Sub

Impresora de Tickets!!!

Este codigo me fue muy util en la impresion de Ticket para un sistema de punto de venta con impresora de Tickets o miniimpresora:

'Se declaran las constantes ya las funciones API del Sistema


Public Const GENERIC_WRITE = &H40000000 
Public Const OPEN_EXISTING = 3 
Public Const FILE_SHARE_WRITE = &H2 
Public LPTPORT As String 
Public hPort As Integer, hPortP As IntPtr 
Public retval As Integer

Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Integer,ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As IntegerPublic Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Integer) As IntegerPublic Structure SECURITY_ATTRIBUTES 
Private nLength As Integer 
Private lpSecurityDescriptor As Integer 
Private bInheritHandle As Integer 
End Structure 

Ya despues en un boton copiamos y pegamos esto:
Dim SA As SECURITY_ATTRIBUTES 
Dim outFile As FileStream 
LPTPORT = "LPT1" 
hPort = CreateFile(LPTPORT, GENERIC_WRITE, FILE_SHARE_WRITE, SA,OPEN_EXISTING, 0, 0) 
hPortP = New IntPtr(hPort) 

Dim Safe As New Microsoft.Win32.SafeHandles.SafeFileHandle(hPortP, True) 
outFile = New System.IO.FileStream(Safe, IO.FileAccess.Write) 

Dim fileWriter As New StreamWriter(outFile) 
fileWriter.WriteLine(" " & " Nombre de Empresa") 
fileWriter.WriteLine(" " & " Direccion, etc ")
fileWriter.WriteLine(" " & " Fecha : " & Date.Today) 
fileWriter.WriteLine(" " & "---------------------------------------") 
fileWriter.WriteLine(" " & "Articulo " & " Precio " & " Cantidad " & " Total ") 
fileWriter.WriteLine(" " & "---------------------------------------")
'Aqui puede ir un ciclo For que lea el contenido de un DataSet que contenga los datos a imprimir por ejemplo
'O bueno de donde quieran imprimir los datos
fileWriter.WriteLine(" " & "---------------------------------------")fileWriter.WriteLine(" " & " Total$ " & var_Total) 'Aqui obviamente la variable donde tienen el totalfileWriter.WriteLine(" " & "Gracias Por Su Compra")fileWriter.Flush()Funciona al 100%