Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a console like element to a c# winforms program

Tags:

c#

.net

winforms

I have a program that monitors debug messages and I have tried using a TextBox and appended the messages to it but it doesn't scale very well and slows way down when the number of messages gets large. I then tried a ListBox but the scrolling was snapping to the top when appending new messages. It also doesn't allow for cut and paste like the text box does.

What is a better way to implement a console like element embedded in a winforms window.

Edit: I would still like to be able to embed a output window like visual studio but since I can't figure out an easy way here are the two solutions I use. In addition to using the RichTextBox which works but you have to clear it every now and then. I use a console that I pinvoke. Here is a little wrapper class that I wrote to handle this.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Con
{
   class Ext_Console 
   {
      static bool console_on = false;

      public static void Show(bool on,string title)
      {
         console_on = on;
         if (console_on)
         {
            AllocConsole();
            Console.Title = title;
            // use to change color
            Console.BackgroundColor = System.ConsoleColor.White;
            Console.ForegroundColor = System.ConsoleColor.Black;

         }
         else
         {
            FreeConsole();
         }
      }

      public static void Write(string output)
      {
         if (console_on)
         {
            Console.Write(output);
         }
      }

      public static void WriteLine(string output)
      {
         if (console_on)
         {
            Console.WriteLine(output);
         }
      }

      [DllImport("kernel32.dll")]
      public static extern Boolean AllocConsole();
      [DllImport("kernel32.dll")]
      public static extern Boolean FreeConsole();
   }
}


// example calls
Ext_Console.Write("console output  ");
Ext_Console.WriteLine("console output");
Ext_Console.Show(true,"Title of console");


like image 777
Rex Logan Avatar asked Oct 31 '08 01:10

Rex Logan


People also ask

How do I add a console application to Windows form?

You need to create a Windows application. Then right click the project and go to properties and set the Output Type for the application to Console Application. This will then not only show your form but also the Console Window. So now you can use the Console class to read in or display messages to that console Window.

Where does console Writeline go C#?

It goes to the console (standard output) or to the stream that the console is set to.


Video Answer


1 Answers

RichTextBox has an AppendText method that is fast. And it can handle large text well.
I believe it is the best for what you need.

like image 173
Ovidiu Pacurar Avatar answered Sep 28 '22 02:09

Ovidiu Pacurar