Print to a Dot-Matrix Printer directly from VB.NET - vb.net

i am finishing a program i am writing and i have to create a printing to an Epson LQ-300+ Dot-Matrix. The printing has to print some text in some specific parts of the paper (Amount,name etc)
Can anyone point me to the right direction or post me an example since i was not able to find something in order to send directly the ASCII characters to the printer via LPT1.
Thank you.

IT Is going to be mainly trial and error a far as positioning goes, it is also going to depend on the font and wether or not you are using a Generic/Text Driver (if so the character spacing, line spacing and font are what ever the printer has been setup for). Back in the DOS days you could send out individual characters to the printer but printing in windows is page based meaning you will need to use the PrintDocument Class, handle the PrintPage event using the PrintPageEventArgs Graphic Property's PrintString Method to position the text where you need it to be.
Something like this:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If PrintDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
PrintDocument1.Print()
End If
End Sub
Private Sub PrintDocument1_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.DrawString("Hello World", New Font("Arial", 10), Brushes.Black, New Point(100, 100))
End Sub

Related

How can I save the content of a rich text box even when the form closes?

This is my first post here, so please don't judge me if I write something wrong ^^
Anyways, I've recently run into an issue with richtextboxes in Visual Basic .NET and WinForms.
Let's say I have a Main form and a Log form. The log form contains a richtextbox which functions as a log. From the main form I'm writing text to the log and I also format the lines, so they have different colors (blue for information, red for error).
Unfortunately, whenever I close and reopen the log form all text that has been written to it is lost.
I've tried saving it to a text file, but that doesn't save the colors of the text.
Is there any way I can save the text and the colors of the lines even when the form closes?
Here's what the excellent suggestion from Shrotter might look like:
Public Class Form1
Private RtfPath As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim folder As String = System.IO.Path.GetDirectoryName(Application.ExecutablePath)
RtfPath = System.IO.Path.Combine(folder, "RtbData.rtf")
If System.IO.File.Exists(RtfPath) Then
RichTextBox1.LoadFile(RtfPath)
End If
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
RichTextBox1.SaveFile(RtfPath)
End Sub
End Class
Of course, you should always wrap the loading/saving of the file in a Try/Catch block in case anything goes wrong.

How to fit a long text file field into a listbox?

I'm creating a program that is reading the information for recipes from a text file. I have the text file structured in a way that the list of instructions for each recipe are combined into one field of the text file. However, when I try to display the instructions into a listbox, it displays as one long line, and you can't see the full set of instructions. Is there anyway to manipulate that single field of the text file to continue onto the next line of the listbox when the text fills the space? I've included a picture of my code for displaying other elements of the text file line.
code
Here is what the sample field says in my text file:
In a pan sauté the shallots in olive oil. Add the brussel sprouts. Sauté until vibrant green. Add chopped bacon and balsamic vinegar if desired.
Once again, I just need help on how to get that giant field to fit to my text box. Here is a picture of my form during runtime currently. Thank you!
vb form
You've to control the drawing yourself.
From DrawMode docs, OwnerDrawVariable:
All the elements in the control are drawn manually and can differ in size.
Now, I'm setting the ItemHeight in MeasureItem event, and drawing the text in DrawItem event.
Protected Overrides Sub OnLoad(e As EventArgs)
yourListBox.DrawMode = DrawMode.OwnerDrawVariable
MyBase.OnLoad(e)
End Sub
Private Sub yourListBox_MeasureItem(sender As Object, e As MeasureItemEventArgs) Handles yourListBox.MeasureItem
e.ItemHeight = e.Graphics.MeasureString(yourListBox.Items(e.Index).ToString(), yourListBox.Font, yourListBox.Width).Height + 10
End Sub
Private Sub yourListBox_DrawItem(sender As Object, e As DrawItemEventArgs) Handles yourListBox.DrawItem
If e.Index < 0 Then Return
e.DrawBackground()
e.Graphics.DrawString(yourListBox.Items(e.Index).ToString(), yourListBox.Font, Brushes.Black, e.Bounds)
End Sub

VB.NET Printing Legal Size

This program refuses to print legal size. I have spent an embarrassing number of hours researching this problem with no luck. Help would be appreciated. The printer supports Legal printing. The "Imports System.Drawing.Printing" statement is there. If I uncomment the PrintPreviewDialog block it previews in Legal size, but prints in Normal size.
EDIT: Program simplified further. I would appreciate if someone could run this code to determine if it is a program issue or something else. Problem on two different computers, two printers, both which are used for legal size printing on a daily basis in Excel.
Imports System.Drawing.Printing
Public Class Form1
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
PrintDocument1.DefaultPageSettings.PaperSize = PrintDocument1.PrinterSettings.PaperSizes(2) '2 has been verified correct, confirmed below
'PrintDocument1.Print()
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub PrintDocument2(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
MsgBox(PrintDocument1.DefaultPageSettings.PaperSize.ToString) ' this verifies the papersize code is correct at run time
Dim drawFont As New Font("Helvetica", 10)
Dim rc1 As RectangleF = New RectangleF(250, 650, 400, 50)
e.Graphics.DrawString("Hello World", drawFont, Brushes.Black, rc1)
End Sub
End Class

Learning VB and need to fill a variable a buttons .text thats dependent on the button pushed

My goal is to have the user able to push any button and the .text of the button to be passed into a a text box. the below is a very simplified version of what i want to achieve... i know i can have each button in its own sub however doing this will create hundreds of lines of code while if what i wish to do works, it will only be a few lines...
http://i.stack.imgur.com/tuF5n.png
Sub Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
TextBox1.Text = (TextBox1.Text + ??? )
End Sub
so what would i replace the ??? with to represent the .text of the button the user pushes?
(This is using VB in Visual Studios 2013)
Thank you in advance ^.^
The sender parameter is the sender of the event. You’ll probably need to cast it before doing anything useful:
Dim button As Button = DirectCast(sender, Button)
Then you can use it:
TextBox1.Text &= button.Text

VB.NET Tool for Image Printing - Quality Ajustments

I'm developing a tool for easy picture printing with a canon selpy cp800. The Image is printed with the following methods:
Private Sub BtnPrintClick(sender As Object, e As System.EventArgs) Handles ptnPrint.Click
If PrintDialog1.ShowDialog() = DialogResult.OK Then
pdPrintImage.Print()
End If
End Sub
Private Sub PdPrintImagePrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles pdPrintImage.PrintPage
e.Graphics.DrawImage(_dPictures(_sPictures(_iActiveImage)).Picture, e.Graphics.VisibleClipBounds)
End Sub
_dPictures(_sPictures(_iActiveImage)).Picture --> object of the type image
I didn't do anything with this image. It's only loaded with the Image.FromFile() method.
Within the following image you can see my problem. This is a scan of the image printed with this method (top) and a scan of the same image printed with the windows picture viewer. You see, the first image you see the tonal errors in the background and the shadows.
Has anyone an idea to fix this?
If it isn't a bit-depth issue as Boo mentioned, it might help to set one or both of these
e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
before doing the .DrawImage.
the best quality I achieved as follows:
1) I put a picture into pdf using components iTextsharp.
2) print the pdf