none
Need guidance on finding simple ways to print out results in C#. RRS feed

  • Question

  • In Python it's easy, when developing a program, to print out an array or calculation results. 

    But in C# everything I've read and tried is very cumbersome and requires programming knowledge I don't yet have. 

    I've been using Rod Stephen's "C# Programming with Visual Studio 2010" for the past two years.  I have successfully printed out a form and what's on it and a few other minor things.  That's it.

    Any guidance would be appreciated.

    Thanks,

    Ron

    • Moved by litdev Sunday, December 2, 2018 1:32 PM
    Sunday, December 2, 2018 12:46 PM

All replies

  • When you say "print out" do you mean on a printer or on the computer screen in some way?
    Sunday, December 2, 2018 4:05 PM
  • In Python it's easy, when developing a program, to print out an array or calculation results. 

    But in C# everything I've read and tried is very cumbersome and requires programming knowledge I don't yet have. 

    I've been using Rod Stephen's "C# Programming with Visual Studio 2010" for the past two years.  I have successfully printed out a form and what's on it and a few other minor things.  That's it.

    Any guidance would be appreciated.

    Thanks,

    Ron

    Hi,

    We are still misunderstanding you , Any way you can print every text box or whatever on your form like this  

    (VB.net code you can convert it to C# easily)

     Private Sub btnPrintInv_Click(sender As Object, e As EventArgs) Handles btnPrintInv.Click
           
            PrescriptionStr = vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine &
                                  "               " & "Name : " & txtVisName.Text & vbCrLf & vbCrLf &
                                  "               " & "Visit No. : " & txtVisNo.Text & "     " & "Patient ID : " & txtVisPatNo.Text & "          " & lblcurTime.Text & vbCrLf & vbCrLf & vbCrLf & vbCrLf &
                                  "                                        " & " PLEASE FOR " & vbCrLf & vbCrLf & vbCrLf & vbCrLf &
                                  "                    * " & cbxInvest.Text & vbCrLf & vbCrLf & vbCrLf &
                                  "                    * " & cbxInvest1.Text & vbCrLf & vbCrLf & vbCrLf &
                                  "                    * " & cbxInvest2.Text & vbCrLf & vbCrLf & vbCrLf &
                                  "                    * " & cbxInvest3.Text & vbCrLf & vbCrLf & vbCrLf &
                                  "                    * " & cbxInvest4.Text & vbCrLf & vbCrLf & vbCrLf &
                                  "                    * " & cbxInvest5.Text & vbCrLf & vbCrLf & vbCrLf
           
            If txtVisName.Text <> "" And cbxInvest.Text <> "" Then
                PrintDocument1.Print()
            End If
    
        End Sub
    
        Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
            Dim numChars As Integer
            Dim numLines As Integer
            Dim stringForPage As String
            Dim strFormat As New StringFormat()
            Dim PrintFont As Font
    
            PrintFont = Label6.Font : PrintFont = Label12.Font
            PrintFont = txtVisName.Font : PrintFont = txtVisPatNo.Font : PrintFont = cbxDia.Font
            PrintFont = lblcurTime.Font : PrintFont = txtVisNo.Font
            PrintFont = cbxDrug1.Font : PrintFont = cbxPlan1.Font : PrintFont = cbxDrug2.Font : PrintFont = cbxPlan2.Font
            PrintFont = cbxDrug3.Font : PrintFont = cbxDrug4.Font : PrintFont = cbxPlan4.Font : PrintFont = cbxDrug5.Font
            PrintFont = cbxPlan5.Font : PrintFont = cbxDrug6.Font : PrintFont = cbxPlan6.Font : PrintFont = cbxDrug7.Font
            PrintFont = cbxPlan7.Font : PrintFont = cbxDrug8.Font : PrintFont = cbxPlan8.Font  'PrintFont = Label13.Font
            PrintFont = cbxPlan9.Font : PrintFont = cbxDrug10.Font : PrintFont = cbxPlan10.Font
            'PrintFont = txtDrug10.Font :PrintFont = txtDrug11.Font : PrintFont = txtPlan11.Font : PrintFont = txtDrug12.Font : PrintFont = txtPlan12.Font
    
            PrintFont = cbxInvest.Font : PrintFont = cbxInvest1.Font : PrintFont = cbxInvest2.Font
            PrintFont = cbxInvest3.Font : PrintFont = cbxInvest4.Font : PrintFont = cbxInvest5.Font
    
            Dim rectDraw As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
            Dim sizeMeasure As New SizeF(e.MarginBounds.Width, e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics))
            strFormat.Trimming = StringTrimming.Word
            e.Graphics.MeasureString(PrescriptionStr, PrintFont, sizeMeasure, strFormat, numChars, numLines)
            stringForPage = PrescriptionStr.Substring(0, numChars)
            e.Graphics.DrawString(stringForPage, PrintFont, Brushes.Black, rectDraw, strFormat)
            If numChars < PrescriptionStr.Length Then
                PrescriptionStr = PrescriptionStr.Substring(numChars)
                e.HasMorePages = True
            Else
                e.HasMorePages = False
            End If
    
        End Sub

    and you can you use report viewer(It has a lot of options - print directly or via export to PDF ,Excel or Word ) to perform your mission or if you use datagridview you can export it to Excel then print it easily 

    Hope this helps you

     

    Regards From Amr_Aly


    • Edited by Amr_Aly Sunday, December 2, 2018 4:54 PM
    Sunday, December 2, 2018 4:41 PM
  • Hi Ron2854,

    Thank you for posting here.

    For your question,  where and which way you want to print?

    More details would be helpful for us to give you some suggestions.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 5, 2018 8:29 AM
    Moderator
  • The simplest way to print results is to first know what you want to print and where.

    If you're on a ConsoleApp project, you just neet to use 

    Console.Write() or Console.WriteLine()

    if it is a library, I don't know if people do that, normally on dll I make a method with a non void return type, and then I catch the return info into a object of the same type or MessageBoxes.

    And if is a GUI, it will depend if you want to print on a control or on the MessageBox.
    if is MessageBox: 

    System.Windows.Forms.MessageBox.Show();

    if its on a control, you have to know which property receives the data you want to print.

    Controls that deals with text directly, have always (I think) a .Text property that you can get and set the text.

    Controls that also deal with text, but In a collection way, normally have a .Items property or .Add method.


    More than that, we (maybe just me) need to know where to print


    BP-LP 2005/2016 @ll rights reserved

    Tuesday, December 11, 2018 10:40 AM