Solved

Printing to a Form in C#.NET

Posted on 2007-04-03
5
285 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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