Click here to Skip to main content
15,910,886 members

Comments by IanSp (Top 4 by date)

IanSp 5-Dec-18 11:48am View    
Hmmm, I've seen this suggestion many times before. However, VBA is still essential to my work with Excel, and I have a big library of VB6 code to convert. As programming is only a part time activity learning a new language seemed like 2 giant steps for me instead just 1 (although I still seem to have to touch virtually every line of my legacy VB6 code). I just hope MS continue to support it... just as I have invested the time to learn it. However, once I am able to get to full functionality in VB.Net I might consider your suggestion, converting VB.Net to C# might be a piece of cake compared to converting VB6 (to anything).
IanSp 5-Dec-18 11:06am View    
Your last point is now captured, thanks. I'm a long time VB6 & VBA programmer. I only program to make my engineering calculations more accessible to others, but VB6 is now becoming difficult to support on Win 10 so I'm now forcing myself to learn VB.Net. It was really tough at first but now I'm loving it... Your perspectives are invaluable. Thanks again!
IanSp 5-Dec-18 10:26am View    
Perfect, thank you! I just needed to know that this was doable and your reply gave me enough to work out the details myself. I created a common GenerateReport() subroutine passing the graphics object and a flag as paramaters:
 
Sub GenerateReport(graphicsTarget As Object, OnForm As Boolean)

    Dim drawPen As Pen
    Dim drawBrush As SolidBrush
    Dim drawFont As Font
    'Dim drawObject As Graphics = graphicsTarget.CreateGraphics()      ' Create the graphics object for the page
    Dim drawString As String = "Sample Text"

    graphicsTarget.Clear(Color.White)   'clears the current screen

    If OnForm Then
      drawFont = New Font("Arial", 16, FontStyle.Bold)
      drawBrush = New SolidBrush(Color.BlueViolet)
      graphicsTarget.DrawString("text in a box", drawFont, drawBrush, 150, 50)
      drawPen = New Pen(Color.Black, 3)
      graphicsTarget.DrawRectangle(drawPen, 120, 50, 200, 25)
    End If
    drawPen = New Pen(Color.Purple, 5)
    drawFont = New Font("Calibri", 20, FontStyle.Italic)
    drawBrush = New SolidBrush(Color.Red)
    graphicsTarget.DrawString("text above a line", drawFont, drawBrush, 150, 290)
    graphicsTarget.DrawLine(drawPen, 150, 320, 350, 320)

    drawFont.Dispose()
    drawBrush.Dispose()
    'drawObject.Dispose()

  End Sub
 

And then called from each as follows:
 Sub WriteOnFrom()
    ' writes to form 

    Dim OnForm As Boolean = True
    With frmOutput
      .Visible = True
      .WindowState = FormWindowState.Normal
      .PrintPreviewControl.Visible = False
      GenerateReport(frmOutput.CreateGraphics, OnForm)
    End With
  End Sub

  Private Sub PrintOutput_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles Me.PrintPage
    'Prepares printed document

    Dim OnForm As Boolean = False
    GenerateReport(e.Graphics, OnForm)
    'e.HasMorePages = False

  End Sub


I did notice in my development project the issue with the graphics being lost as you mention, but it does not occur in this example (not sure why). I solved it in my project by simply calling GenerateReport() every time a Paint event is triggered. Your comments make me think I can improve this. I shall try and figure this out myself but if you can point me in the best direction I would surely appreciate it.
IanSp 5-Dec-18 9:44am View    
Thank you for your reply. In the interests of keeping this question simple I didn't list all the reasons why I'd prefer to not use the PrintPreview control on my form, but I'm not excluding it for now. However, if I was to use it, how to I pass a conditional to the PrintPage procedure so I can print the full output to the printer and only parts of it (subset) to the PrintPreview control on form, I can't seem to find a way to pass a flag to the PrintPage subroutine. Your insight would be very helpful.