VB.NET Printing Legal Size - vb.net

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

Related

Windows Media Player VB.net

I have a WMP in my vb.net project and I wanted to load the next media automatically after the first is finnished I did some research on googel and found the simple to understand code as per below.
Private Sub AxWindowsMediaPlayer1_PlayStateChange(ByVal sender As System.Object, ByVal e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) Handles AxWindowsMediaPlayer1.PlayStateChange
If AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsStopped Then
AxWindowsMediaPlayer1.URL = ("Test2.mp4")
MessageBox.Show("Playing End")
End If
End Sub
I however cant get it to automatically play the next (Test2.mp4) unless I have the messagebox pop-up. I discovered this purely by accident. However I dont want the Messagebox to pop-up everytime a new Mp4 file is ready to be played. Dose anybody know what is going on here and how I can fix this?
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim oTask01 As Threading.Thread
oTask01 = New Threading.Thread(AddressOf oStarting01)
oTask01.Start()
Dim omessagebox As MessageBox = Nothing
omessagebox.Show("Playing End", "", MessageBoxButtons.OK)
oTask01.Abort()
End Sub
Private Function oStarting01() As Byte
While True
System.Windows.Forms.SendKeys.SendWait(vbCr)
End While
Return 0
End Function
End Class
Hi, try with this code. It works. Diving more deeply in Windows system and subsystems is not an easy task, not at least for me. I hope you get what you were looking for for your software. Thank you very much. Happy codding!. :)
Private Sub AxWindowsMediaPlayer1_PlayStateChange(ByVal sender As
System.Object, ByVal e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent)
Handles AxWindowsMediaPlayer1.PlayStateChange
If AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsStopped Then
AxWindowsMediaPlayer1.URL = ("Test2.mp4")
'MessageBox.Show("Playing End") 'This line was commented because is not neccesary in this fragment of code
End If
End Sub
Hi, if I have understood well to you, you wanted to supress the message box. It is got doing a comment's line with "'". I hope you like it and continue enjoying with computers and software. Thank you very much and happy codding. :)

Drag and drop an image into a RichTextBox

I am updating code done in VB 4, where I have a RichTextBox. I need to be able to drag-and-drop an image from Windows Explorer into the RTB. Unfortunately, I am unable to get the drag-and-drop to work.
I've created a much more simple Windows Form program to try to resolve this, but have made no progress. I begin by setting AllowDrop to True.
Public Sub New()
InitializeComponent()
Me.DragAndDropTextBox.AllowDrop = True
End Sub
I then create handlers for the RTB. These are taken directly from MSDN.
Private Sub DragAndDropTextBox_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles DragAndDropTextBox.DragEnter
' Check the format of the data being dropped.
If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
' Display the copy cursor.
e.Effect = DragDropEffects.Copy
Else
' Display the no-drop cursor.
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub DragAndDropTextBox_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles DragAndDropTextBox.DragDrop
System.Windows.Forms.DragEventArgs) Handles DragAndDropTextBox.DragDrop
Dim img As Image
img = Image.FromFile(e.Data.GetData(DataFormats.FileDrop, False))
Clipboard.SetImage(img)
Me.DragAndDropTextBox.SelectionStart = 0
Me.DragAndDropTextBox.Paste()
End Sub
When I grab an image in Explorer and drag it over my window, I get the circle with a slash. I have put breakpoints on the first line of each of the handlers, and they are never reached. I have looked at several pages, and they all seem to give the same process, so I must be missing something simple.
I am not worried right now about pasting the image into the text box; I know I need to work on that. I am only trying to capture the image, but the handler methods do not seem to be getting called.
UPDATE
After quite a bit of experimentation, I found that the actual issue is with my Visual Studio 2010, which I always run as administrator. When I run the program from an exe, the drag-and-drop works. When I try running from VS in debug, it does not. Has anyone experienced this before?
If anyone could shed some light on this, I would be very grateful.
Try getting rid of the InitializeComponent() call in your Sub New function. When I did that, I was able to detect the DragEnter event. Here is the code I tested (I created a simple WinForm and put a RichTextBox on it called DragAndDropTextBox):
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DragAndDropTextBox.AllowDrop = True
End Sub
Private Sub DragAndDropTextBox_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DragAndDropTextBox.DragEnter
Debug.Print("Entering text box region")
' Check the format of the data being dropped.
If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
' Display the copy cursor.
e.Effect = DragDropEffects.Copy
Else
' Display the no-drop cursor.
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub DragAndDropTextBox_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DragAndDropTextBox.DragDrop
Dim img As Image
img = Image.FromFile(e.Data.GetData(DataFormats.FileDrop, False))
Clipboard.SetImage(img)
Me.DragAndDropTextBox.SelectionStart = 0
Me.DragAndDropTextBox.Paste()
End Sub
End Class
The InitializeComponent() call should appear in your code (I believe) when you add your own custom controls to a form. Otherwise, I don't think you need to call it.
It turned out that the Drag-And-Drop was working when running the code from an exe, but not from within Visual Studio. More searching turned up this answer, which states that Drag-And-Drop does not work in Visual Studio when it is run as an Administrator. I ran it with normal permissions, and the code worked.

Form_Load doesn't execute in application

I am new to Visual Basic. I have installed Microsoft Visual Studio 2010. Created a new Windows Form Application. As an example, I made a simple program which will ask the end user to input 2 numbers and allow them to either add them or subtract the second number from the first one and display the output in a Textbox.
Now, I added another Subroutine which would be executed automatically when the Windows Form loads. This would calculate the width of the output Textbox and the Form Width and display at the bottom.
This is how the code looks like right now:
Public Class Form1
' Run this Subroutine initially to display the Form and Text box width
Private Sub Form_Load()
Label5.Text = TextBox3.Width
Label7.Text = Me.Width
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As Integer
Dim b As Integer
a = TextBox1.Text
b = TextBox2.Text
TextBox3.Text = a + b
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim a As Integer
Dim b As Integer
a = TextBox1.Text
b = TextBox2.Text
TextBox3.Text = a - b
End Sub
End Class
While everything works correctly for the addition and subtraction, it does not display the Form and output Textbox width in the Windows Form.
I think, Form_Load() is not executing properly.
I also tried, Form_Activate() but that did not work either.
Once I am able to do this, I would like to extend this concept to resize the output Textbox along with the Form resize. However, for the purpose of understanding I wanted to see if I can execute Form_Load() successfully.
Thanks.
Form_Load doesn’t execute. For now, it’s just any other method. In order to tell VB to make this method handle the Load event, you need to tell it so:
Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Loasd
Label5.Text = TextBox3.Width
Label7.Text = Me.Width
End Sub
(And add the required parameters for the event.)
A few other remarks:
Ensure that Option Strict On is enabled in your project options at all times. This will make the compiler much stricter with your code and flag more errors. This is a good thing since these errors are potential bugs. In particular, your code is very lax with conversions between different data types, these should be made explicit.
Initialise variables when you declare them, don’t assign a value in a separate statement. That is, write this:
Dim a As Integer = Integer.Parse(TextBox1.Text)
(Explicit conversion added as well.)
If you want to make a control fill the form, you can just set its Dock property appropriately in the forms editor, instead of having to program this manually.
You need to add the Handle so the app executes it automatically:
Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
'...
End Sub

Print to a Dot-Matrix Printer directly from 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

Trying to draw on a Picture Box, but nothing appears

I'm converting a VB6 application to VB.Net that draws on picture boxes. Naturally I read the fine manual and turn up this example here. I therefore produced a little project with a form containing only a picture box and tried the following:-
Private Sub Picture1_paint(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles PictureBox1.Paint
Dim mygraphics As Graphics
mygraphics = PictureBox1.CreateGraphics
Dim pen As New Drawing.Pen(System.Drawing.Color.Red, 1)
mygraphics.DrawEllipse(pen, 0, 0, 100, 100)
pen.Dispose
End Sub
just like it says. But on running the application, the box turns up blank. Searching for help turned up a suggestion here that I should use a Frame instead, but the result was the same. I have checked that I'm not drawing in the background colour, and that the function is actually invoked.
What have I overlooked?
Paint handler has invalid type for EventArgs. It should be System.Windows.Forms.PaintEventArgs
Use e.Graphics property to obtain graphics instance.
mygraphics = e.Graphics
Reference Link MSDN - Control.Paint Event
I think e is of PainEventArgs type, with already contains a graphics object in e.Graphics. Use that instead.
Public Class Form1
Private Sub PictureBox1_Paint(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim pen As New Pen(Color.Red, 1)
e.Graphics.DrawEllipse(pen, 0, 0, 100, 100)
pen.Dispose()
End Sub
End Class