Print all of a scrollable textbox - vb.net

Programming in VB.NET and have another slight issue. I have a TextBox that is populated from a txt file using a StreamReader. Basically I have a print option but can only print what is shown in the TextBox, if there is more info in the TextBox further down that needs to be scrolled this is not printed (hope that makes sense!). Is there any way I get get around this so all of the information is printed?
Here's my code:
Imports System.Drawing.Printing
Public Class JobList
Private Sub JobList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim objReader As New System.IO.StreamReader("C:\test\JobLog.txt", True)
txtJL.Text = objReader.ReadToEnd
objReader.Close()
End Sub
Private Sub printText(ByVal sender As System.Object, ByVal ev As PrintPageEventArgs)
Dim font As New Font("Arial", 16, FontStyle.Regular)
ev.Graphics.DrawString(txtJL.Text, font, Brushes.Black, 100, 100)
End Sub
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Dim printDoc As New PrintDocument
AddHandler printDoc.PrintPage, AddressOf Me.printText
printDoc.Print()
End Sub
End Class

How to print from a StreamReader on MSDN, scroll down - there is an example. It may need a slight modification to print from a String, then you can pass TextBox.Text into it. Or just use AS IS and print from StreamReader - should get same results anyway.

Related

Create list box in runtime and change its color via menu in runtime

I need to write this small program in visual basic, but i face a problem which is i can't change the color or add list form text file during runtime.
this is picture of what i wont to do
http://i62.tinypic.com/30tghh0.png
And this is the code i wrote so far,,
Public Class Form1
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
Dim listBox1 As New System.Windows.Forms.ListBox
listBox1.Text = "New List"
listBox1.Location = New Point(25, 25)
listBox1.Size = New Size(380, 280)
Me.Controls.Add(listBox1)
OpenToolStripMenuItem.Enabled = True
SaveToolStripMenuItem.Enabled = True
SaveAsToolStripMenuItem.Enabled = True
CloseToolStripMenuItem.Enabled = True
EditToolStripMenuItem.Enabled = True
End Sub
Private Sub ExirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExirToolStripMenuItem.Click
End
End Sub
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
If (OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
Dim myfile As String = OpenFileDialog1.FileName
Dim allLines As String() = File.ReadAllLines(myfile)
For Each line As String In allLines
ListBox1.Items.Add(line)
Next
End If
End Sub
End Class
The problem as you see is in OpenToolStripMenuItem sub with line ListBox1.Items.Add(line)
is there is no listbox1 because is not created yet.the same with color, save and the rest.
so, please help me to solve it.
Move the declaration of ListBox1 out to the Class level:
Public Class Form1
Private ListBox1 As System.Windows.Forms.ListBox
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
ListBox1 = New System.Windows.Forms.ListBox
...
End Sub
...
End Class
*But why create it at run-time like this? You could add it at design-time and set the Visible() property to False. When the New button is clicked, you change Visible() to True. Unless you need a new ListBox to be created each time so you have more than one? If so, how will they be laid out?...

Make Print From Textbox stay in paper margins? (w/PageSetup)

I've been having lots of problems getting the print dialog and page set up to work together to print text from a textbox, ive spent hours doing research and trying everything, I cannot get it to work, can someone please help me, and give me the code to print a document from a textbox and also get page set up to work with it (just like in notepad)
Here is my code thats not working
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If (PrintDialog1.ShowDialog() = DialogResult.OK) Then
PrintDocument1.Print()
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
PageSetupDialog1.ShowDialog()
PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim textArea As New Rectangle()
e.Graphics.DrawString(TextBox1.Text, TextBox1.Font, Brushes.Black, 100, 100)
End Sub
Thanks
If you use a PrintDocument you can draw text to a Rectangle and it will wrap naturally. You can also employ a StringFormat object for other formatting help.
Private Sub printDoc_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs) Handles printDoc.PrintPage
Dim textArea As New Rectangle(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
e.Graphics.DrawString(textbox1.Text, New Font("Consolas", 10), Brushes.Navy, textArea)
'other stuff
End Sub

Resizing userControls in newly initiated TabPage

When a new TabPage is added in my TabControls, I would like it to autosize to Form1's current height/width. I can do this on Form Load. However, when I add a new tab then resize the window, it does nothing. Thanks for any helpful input.
Imports System.IO
Public Class Form1
'The Global Variables
Dim theControls As New theControls
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'For Auto Sizing theControls to Form1
TabPage1.Controls.Add(theControls)
theControls.theBrowser.Navigate("http://google.com")
End Sub
Private Sub Form1_SizeChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged
'For Auto Sizing theControls to Form1
Me.TabControl1.Width = Me.Width - 20
Me.TabControl1.Height = Me.Height
theControls.Width = Me.TabControl1.Width - 20
theControls.Height = Me.TabControl1.Height - 20
End Sub
Private Sub TabControl1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.Click
'Add new tab with the same controls.
Dim theNewTab As New TabPage
Dim theOtherControls As New theControls
Dim theTabCounter As Integer = TabControl1.TabPages.Count
Dim theSelectedTab As String = TabControl1.SelectedTab.Text
If theSelectedTab = "+" Then
TabControl1.TabPages.Insert(theTabCounter - 1, theNewTab)
theNewTab.Controls.Add(theOtherControls)
theControls.theBrowser.Navigate("http://google.com")
theOtherControls.theBrowser.Navigate("http://google.com")
TabControl1.SelectTab(theTabCounter - 1)
End If
End Sub
End Class
The tabPageCounter variable can be ignore. I started to think a For Each loop would be needed but I think there's an easier way. From what I have found on the web, I have gotten to this Dock function but I guess I'm not exactly clear on how to use it...
Do it in Form_sizechanged event ..
Private Sub Form1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
Me.TabControl1.Width = Me.Width - 20
myUserControl.Width = Me.TabControl1.Width -20
End Sub

Showing Textbox ToolTip

I am trying to work in the tooltip in vb.net. What I am trying to do is whatever I write the text in the textbox control show it in the tooltip. I am able to show text in tooltip but my question is when I edit input text it will show old and new text in the popup tooltip. Here is what I have done so far.
Public Class Form1
Dim s As String = ""
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
s = TextBox1.Text
Dim tooltip1 As System.Windows.Forms.ToolTip = New System.Windows.Forms.ToolTip()
tooltip1.SetToolTip(Button1, s)
End Sub
End Class
Thank you.
It's hard to figure out why this is useful, but try using the TextChanged event of the textbox to update the tooltip:
Private _ToolTip As New ToolTip()
Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) _
Handles TextBox1.TextChanged
_ToolTip.Show(TextBox1.Text, TextBox1)
End Sub

how can I tell if someone printed from printPreview?

Using printPreview As New PrintPreviewDialog()
' Dim x As New Printing.PrintDocument()
' AddHandler x.PrintPage, AddressOf PrintData
printPreview.Document = Me.CurrentDocument
If ShowPrinterSetup Then
Dim x As New PrintDialog()
x.Document = CurrentDocument
x.ShowDialog(Anchor)
End If
whichPage = 0
Return printPreview.ShowDialog(Anchor)
End Using
So far no matter what I clicked in printpreview, the showdialog returns cancel?
How can I tell if the user did print? I'd like to clear the print queue of items if they did actually print to a printer or ask them if I should clear it, but only if they actually did print something.
You can get the result of the print job from the CurrentDocument EndPrint event
Private WithEvents CurrentDocument As New PrintDocument
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Using printPreview As New PrintPreviewDialog()
printPreview.Document = Me.CurrentDocument
printPreview.ShowDialog()
End Using
End Sub
Private Sub document_EndPrint(ByVal sender As Object, ByVal e As PrintEventArgs) Handles CurrentDocument.EndPrint
If e.PrintAction = PrintAction.PrintToPrinter Then
MsgBox("Document Printed to Printer")
End If
End Sub