?
Solved

Printing to a Form in C#.NET

Posted on 2007-04-03
5
Medium Priority
?
290 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 150 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 150 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 75 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

801 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