Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / C#

Embedding a Console in a C# Application

Rate me:
Please Sign up or sign in to vote.
4.91/5 (151 votes)
21 Mar 2015MIT4 min read 622.2K   27.7K   403  
Embed a functional console window in a C# application
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ConsoleControlSample
{
  public partial class FormConsoleControlSample : Form
  {
    public FormConsoleControlSample()
    {
      InitializeComponent();
    }

    private void FormConsoleControlSample_Load(object sender, EventArgs e)
    {
      //  Update the UI state.
      UpdateUIState();
    }

    /// <summary>
    /// Handles the Click event of the toolStripButtonNewProcess control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private void toolStripButtonNewProcess_Click(object sender, EventArgs e)
    {
      //  Create the new process form.
      FormNewProcess formNewProcess = new FormNewProcess();

      //  If the form is shown OK, start the process.
      if (formNewProcess.ShowDialog() == DialogResult.OK)
      {
        //  Start the proces.
        consoleControl.StartProcess(formNewProcess.FileName, formNewProcess.Arguments);

        //  Update the UI state.
        UpdateUIState();
      }
    }

    /// <summary>
    /// Handles the Click event of the toolStripButtonStopProcess control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private void toolStripButtonStopProcess_Click(object sender, EventArgs e)
    {
      consoleControl.StopProcess();
      
      //  Update the UI state.
      UpdateUIState();
    }

    private void UpdateUIState()
    {
      //  Update the state.
      if (consoleControl.IsProcessRunning)
        toolStripStatusLabelConsoleState.Text = "Running " + System.IO.Path.GetFileName(consoleControl.CurrentProcess.MainModule.FileName);
      else
        toolStripStatusLabelConsoleState.Text = "Not Running";

      //  Update toolbar buttons.
      toolStripButtonRunCMD.Enabled = !consoleControl.IsProcessRunning;
      toolStripButtonNewProcess.Enabled = !consoleControl.IsProcessRunning;
      toolStripButtonStopProcess.Enabled = consoleControl.IsProcessRunning;
      toolStripButtonShowDiagnostics.Checked = consoleControl.ShowDiagnostics;
      toolStripButtonInputEnabled.Checked = consoleControl.IsInputEnabled;
    }

    private void toolStripButtonShowDiagnostics_Click(object sender, EventArgs e)
    {
      consoleControl.ShowDiagnostics = !consoleControl.ShowDiagnostics;
      UpdateUIState();
    }

    private void timerUpdateUI_Tick(object sender, EventArgs e)
    {
      UpdateUIState();
    }

    private void toolStripButtonInputEnabled_Click(object sender, EventArgs e)
    {
      consoleControl.IsInputEnabled = !consoleControl.IsInputEnabled;
      UpdateUIState();
    }

    private void toolStripButtonRunCMD_Click(object sender, EventArgs e)
    {
      consoleControl.StartProcess("cmd", null);
      UpdateUIState();
    }

    private void toolStripButtonClearOutput_Click(object sender, EventArgs e)
    {
      consoleControl.ClearOutput();
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer
United Kingdom United Kingdom
Follow my blog at www.dwmkerr.com and find out about my charity at www.childrenshomesnepal.org.

Comments and Discussions