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
Related
I'm trying to have two options to draw a border around a picture box.
I can click on the picturebox to highlight but now would like to be able to use a button to do the same thing.
Private Sub imgLabel_Click(sender As Object, e As EventArgs) Handles imgLabel.Click
Dim BorderBounds As Rectangle = DirectCast(sender, PictureBox).ClientRectangle BorderBounds.Inflate(-1, -1)
ControlPaint.DrawBorder(DirectCast(sender, PictureBox).CreateGraphics, BorderBounds, Color.Orange, ButtonBorderStyle.Solid)
If Not (HighLightededPictureBox Is Nothing) Then
HighLightededPictureBox.Invalidate()
End If
'Rememeber the last highlighted PictureBox
HighLightededPictureBox = CType(sender, PictureBox)
When I try to add a button click I get the Exception Unhandled - System.windows.forms.button to type System.windows.forms.picturebox error.
I have tried to add the button click event after "Handles" which causes the above error.
Private Sub imgLabel_Click(sender As Object, e As EventArgs) Handles imgLabel.Click, button1.click
I'm pretty new to programming and my searching results are turning anything up of value. I don't fully understand Ctypes/Directcasts.
Any help is greatly appreciated.
Ok, so I ended up going with this...
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim boxsize As New Size(192, 169)
Dim recpoint As New Point(0, 0)
Dim myrectangle As New Rectangle(recpoint, boxsize)
myrectangle.Inflate(-3, -3)
Dim G As Drawing.Graphics = PictureBox1.CreateGraphics
Dim Pen As New Pen(Color.Orange, 5)
G.DrawRectangle(Pen, myrectangle)
End Sub
Seems to be working ok, but requires a lot of manual entry of points. I have 6 more of these.
You could try
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
imgLabel_Click(imgLabel, nothing)
End Sub
I'm trying to print a progressbar's percentage to a textbox. When ever I run my program, nothing appears in the textbox. This is my code:
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
ProgressBar1.Maximum = TextBox1.Text
End Sub
Help is very appreciated! Thank you.
Here is some code that i have written up for you to have a look at, it should lead you in the right direction and help you on your way :)
Imports System.ComponentModel
Public Class Form1
''This will display the information to the textbox and will also load a progressbar(you can change it to something else beside a textbox too eg label, windows form title and so on).
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
TextBox1.Text = e.ProgressPercentage & "%"
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
''This is make the backgroundworker start at load up(change it to a button if need be
CheckForIllegalCrossThreadCalls = False
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
''This is the example that i created to show you how to set a task.
For i = 0 To 10000
TextBox1.Text = i
BackgroundWorker1.ReportProgress(i)
System.Threading.Thread.Sleep(500)
Next
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
''once the task is complete it will show a messagebox and reset the progressbars value to 0 so its not full when the task is compelete.
MessageBox.Show("Completed")
ProgressBar1.Value = 0
End Sub
End Class
Let me know how you go, i live in a country where i cant access the website link that you posted.Happy Coding
UPDATE: do check out the backgroundworkers on google, there are a lot of tutorials to help you :)
I'm very frustrated trying to get my code to work.
I'm trying to have a selected item in a listbox removed also in the textbox.
Getting ready to remove text;
Removed the text;
But it's still in the textbox.
Here is my code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text)
TextBox2.Text += TextBox1.Text & vbNewLine
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
'
'//HOW TO REMOVE THE SELECTED TEXT IN THE LISTBOX ALSO REMOVED IN THE TEXTBOX2??
'
'
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim filenames As String = "C:\log\log.txt"
My.Computer.FileSystem.WriteAllText(filenames, TextBox2.Text, False)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim filenames As String = "C:\log\log.txt"
If My.Computer.FileSystem.FileExists(filenames) Then
TextBox2.Text = My.Computer.FileSystem.ReadAllText(filenames)
Dim items()
items = TextBox2.Lines()
For Each item In items
ListBox1.Items.Add(item)
Next
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Clipboard.SetText(ListBox1.SelectedItem)
End Sub
End Class
The worst part is that every time I try to look it up online, there are no errors until I click the button that says 'Value Cannot Be Null'
It happened every single time.
Please, before you mash the -1 button, at least tell me why. I'm new to this.
This should work for you
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text.Replace(ListBox1.Items(ListBox1.SelectedIndex), Nothing)
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
End Sub
End Class
Many developer can write real time tooltip for him self.
So, if you don't understanding my question, here is example
I want the tooltip following my cursor with smooth, can anybody tell me the code?
This will do what you asked for. As I said, you simply have to show the ToolTip yourself and not rely on the system to do it for you.
Private Sub Button1_MouseMove(sender As Object, e As MouseEventArgs) Handles Button1.MouseMove
Dim text = e.Location.ToString()
If text <> Me.ToolTip1.GetToolTip(Me.Button1) Then
Me.ToolTip1.Show(e.Location.ToString(), Me.Button1, New Point(e.X + 20, e.Y + 20))
End If
End Sub
Private Sub Button1_MouseLeave(sender As Object, e As EventArgs) Handles Button1.MouseLeave
Me.ToolTip1.Hide(Me.Button1)
End Sub
If you want the tooltip to follow the mouse, maybe use a timer and something like this
Public Class Form1
Private toolTipMsg As String = "Busy, Please Wait..." & vbCrLf & "Line2 Blah bla bla."
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ToolTip1.Active = True
ToolTip1.IsBalloon = False
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
' get cursor position
Dim cp = Cursor.Position
' offset Y so tooltip is under mouse
cp.Y += CInt(Cursor.Size.Height * 1.5)
' show tooltip
ToolTip1.Show(toolTipMsg, Me, PointToClient(cp))
End Sub
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.