Solved

Printing to a Form in C#.NET

Posted on 2007-04-03
5
288 Views
Last Modified: 2013-12-16
Hello,

Why doesn't Console.WriteLine("hello") write anything to the form from which I'm calling it (C#.NET)?  What's the best way to tell the program to print to the form?
0
Comment
Question by:NigelRocks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 50 total points
ID: 18847522
To print on the form you need a Graphics context.

For temporary graphics writes use:

    Graphics g = this.CreateGraphics();
    g.DrawString(...stuff in here...);
    g.Dispose();

These will get erased when the form is refreshed such as if someone minimized it or if another form obscured it.

For persistent graphics use the graphics provided in the "e" parameter of the forms Paint() event...
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 50 total points
ID: 18847528
Hi NigelRocks;

The reason is that Console.Writeline writes to a console window. When you are using a Windows Form application there is no Console. When executing the code in a Windows Form and from the development IDE Console messages go to the Output window.

To display messages you can place a textBox  on the form and set its MultiLine property to true and then expand the textBox so that multiple lines can be displayed in it and use code like this

            textBox1.AppendText("Hello World\n");
            textBox1.AppendText("Another Hello World Message\n");

to write to it.

Fernando
0
 
LVL 2

Assisted Solution

by:mahanatti
mahanatti earned 25 total points
ID: 18862569
Console.WriteLine can be used to write to the console window in a Console Application but not to Windows Application since it uses GUI but not Console.

To print your form you could use the PrintDocument class in the System.Drawing.Printing namespace. Please refer http://www.codeproject.com/csharp/printform.asp that has a sample.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 19252884
Hi Bob;

I believe this should be a point split. Everyone provided valid information about the question.

Thanks;

Fernando
0

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question