-1

Am using the newer vb.net 8.0 and the picturebox.print command isn't working.

Public Class Form11
Private Sub BttFor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BttFor.Click
    Dim a As Double
    For a = 10 To 50 Step (2)
        PbEven.print(a)
    Next a
End Sub
  • 1
    What is *the newer vb.net 8.0*? If this is a WinForms app and `PbEven` is a PictureBox, then that Control doesn't have a `print()` method. You have to subscribe to its `Paint` event and use `Graphics.DrawString()` to draw `a.ToString()` (if that's what you want to do) – Jimi Jul 25 '20 at 11:14
  • @Jimi OP might have been using classic VB (VB6 or older), where you could call `Print()` on a PictureBox control. – 41686d6564 stands w. Palestine Jul 25 '20 at 11:24
  • Does this answer your question? [Adding text in an image vb.net](https://stackoverflow.com/questions/46021397/adding-text-in-an-image-vb-net) - See also: https://stackoverflow.com/q/3418283/8967612 – 41686d6564 stands w. Palestine Jul 25 '20 at 11:26
  • @Ahmed Abdelhameed Sure. It's the *newer* thing applied to something that is 15 years old that sounded *odd* :) I have no idea what that method did. -- The OP could also write text to a Label (to use it's AutoSize and Text Alignment features), then use `[Label].DrawToBitmap()` to create an Image. Not sure why the OP is trying to draw text on a PictureBox. Maybe to save an Image later? A better description of the expected results wouldn't hurt. – Jimi Jul 25 '20 at 12:09
  • @Jimi _"I have no idea what that method did"_ It was an _awkward_ way to dump text onto the form or onto a PictureBox (not sure if other controls supported it). Awkward because it didn't provide any useful options and the text would simply disappear once the control is redrawn. Oh, the good ol' days! :) – 41686d6564 stands w. Palestine Jul 25 '20 at 12:22
  • @Ahmed Abdelhameed Well, then it looks like: `var g = [Control].CreateGraphics(); g.DrawString("Some string", ...);`. If so, it didn't actually disappear :) – Jimi Jul 25 '20 at 12:26
  • @Jimi Yep, same idea. Except that you didn't get any of the options that you get with `DrawString`. It did handle printing on consecutive lines for you though (like Idle_Mind's answer below) so you didn't have to `MeasureString` or any of that. I'm not sure I get what do you mean by "it didn't actually disappear". – 41686d6564 stands w. Palestine Jul 25 '20 at 16:01
  • I don't have VB6 on my machines any more, so I can't test it out. I miss the old days...sometimes. ;) – Idle_Mind Jul 25 '20 at 16:03
  • 1
    @Ahmed Abdelhameed I understand, a whole piece of that comment is missing. What I meant is that this kind of habit, the use of non-persistent drawing, appears to then be *persistent* in time (based on how often you can see code *misplacing* `CreateGraphics()`). So, it looks like that event if the actual method (this `Print()` guy) doesn't exist anymore, it found its way back, in disguise, in .Net code. – Jimi Jul 25 '20 at 17:16
  • @Jimi Ah, haha! I get what you mean now. – 41686d6564 stands w. Palestine Jul 25 '20 at 22:22

1 Answers1

1

Here's the basic idea of what the old .Print() command did.

*It was really more complex than this, but I'm not that motivated this morning.

Public Class Form1

    Private lines As New List(Of String)

    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Button1.Enabled = False

        For a As Integer = 10 To 50 Step 2
            Print(a)
            Await Task.Delay(500)
        Next

        Button1.Enabled = True
    End Sub

    Private Sub Print(ByVal output As String)
        lines.Add(output)
        PictureBox1.Invalidate()
    End Sub

    Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
        Dim rowHeight = PictureBox1.Font.Size + 5
        For y As Integer = 0 To (lines.Count - 1)
            e.Graphics.DrawString(lines(y), Me.Font, Brushes.Black, New Point(10, (y * rowHeight) + 10))
        Next
    End Sub

End Class

Code in action:

enter image description here

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • Just nitpicking here... Actually, what `Print()` did is similar to calling `DrawString()` on the PictureBox's `Graphics` (not `PaintEventArgs.Graphics`). See [my comment](https://stackoverflow.com/questions/63087556/how-to-print-txt-in-a-picture-box-in-vb-net?noredirect=1#comment111561670_63087556) above and Jimi's reply to it. But of course, the way you're doing it here is better; no one wants to get that exact behavior. – 41686d6564 stands w. Palestine Jul 25 '20 at 15:55
  • @AhmedAbdelhameed, of course. The old `.Print` was indeed temporary. Just don't forget to `.Dispose()` if you obtained a Graphics from `.CreateGraphics()`. – Idle_Mind Jul 25 '20 at 16:00