Solved

Printing to a Form in C#.NET

Posted on 2007-04-03
5
279 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
  • 2
5 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 50 total points
Comment Utility
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 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 50 total points
Comment Utility
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
Comment Utility
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 62

Expert Comment

by:Fernando Soto
Comment Utility
Hi Bob;

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

Thanks;

Fernando
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now