0

I'm trying to find ideas on how to accomplish recreating the console in a richtextbox. The first idea I've found is to redirect the output from an invisible console app running simultaneously. Second, using two text boxes, one for output, one for input.

The problem I've had with both of these is that I don't know which event I should be subscribing to. Yes I have looked all over the internet all day for this, but the closest I found was someone doing it in VB. So my questions:

  1. Are there any different ideas than the ones I listed?
  2. Is it possible to have only one rTextBox for both input and output?
  3. If redirection is the best solution, could anyone please explain how I set up the redirect; I have created a separate file (poker.cs), which is setup to run through the console. If you look at the top answer here: Redirect console output to textbox in separate program he has two functions, but I have no idea how to set it up in regards to my richTextBox and running a .cs file; it could also be that I was using the wrong event. I had been running it from load.

I would absolutely prefer to have the richTextBox run as if it were a console. I know it can be done. I want to believe.

Community
  • 1
  • 1
Ryan Gray
  • 1
  • 1
  • Forget about console, if you search for "console" you will end with console programs, not Forms programs, hook to the KeyPress event of the second text box, if it's the return key take the value of the text and add it to the textbox, that's all – Gusman Feb 07 '16 at 02:27
  • Why are you so angry Gusman? i don't know what you're saying. – Ryan Gray Feb 07 '16 at 02:37
  • Ehm, sorry if it seemed to be angry, anything but it, it was a true recomendation, i was only being explicit as it's a comment, dont get it wrong! :D – Gusman Feb 07 '16 at 02:39
  • Let me add an answer in conditions ;) – Gusman Feb 07 '16 at 02:40
  • ok thanks, this is day 2 of GUI for me, i worked alot in C++ so alot of these concepts are lost on me unless I see code. I appreciate the help Gusman – Ryan Gray Feb 07 '16 at 02:44

2 Answers2

0

Don't search for the term "console" or it will guide you to wrong posts, it will lead to console programs and not to forms program like the ones you're creating.

It's very easy to redirect the input from a textbox to another one, first of all hook to the KeyUp event of the second text box, the one where the user does it's input.

There you can check if the pressed key is return, in that case pick the text of the textbox, append it to the text of the first textbox and clear the text at the second text box, someting like this:

    private void inputTextBox_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Return)
        {

            consoleRichBox.Text += inputTextBox.Text + "\r\n";
            inputTextBox.Text = "";
            e.Handled = true;

        }
    }
Gusman
  • 14,905
  • 2
  • 34
  • 50
  • If you want it to look like a console put both boxes one after other and change the BorderStyle to None, then you can add them in a Panel if you want to add a border – Gusman Feb 07 '16 at 02:51
  • This is fantastic Gusman, let me play around with it for a bit and see what i can do. – Ryan Gray Feb 07 '16 at 03:04
  • Gusman, my brain isn't working right. I am calling my .cs file (which is a console poker game) from the Form1_Load(); this program needs multiple string and integer inputs from the user. I can pass off the user input into a public variable, but I'm confused how I'm supposed to use this more than once. – Ryan Gray Feb 07 '16 at 03:59
0

this gives you output of cammand prompts:

using System.Diagnostics;
private static string CaptureCommandPromptOutput(string command, string argument)
{
    ProcessStartInfo info = new ProcessStartInfo(command, argument);
    info.RedirectStandardOutput = true;
    info.UseShellExecute = false;
    info.CreateNoWindow = true;
    Process p = new Process();
    p.StartInfo = info;
    p.Start();
    return p.StandardOutput.ReadToEnd();
}  

and use it like:

richTextBox1.Text = CaptureCommandPromptOutput("cmd", "dir C:\");
  • thank you very much, i think i might be able to use this, but i wanted to run the program internally instead of calling an exe. but thanks to you, I now understand how the redirect ultimately works. – Ryan Gray Feb 07 '16 at 03:05