Tayla Wilson
Tayla Wilson

Reputation: 494

Console Input from Windows Form Controls

I am creating an application that receives output from a console and displays it in a more user friendly view through a list box using windows forms. I've done this by starting the process of a .jar file and using this code:

 public void DataReceived(object sender, DataReceivedEventArgs e)
    {
        // e.Data is the line which was written to standard output
        if (e.Data != null)
        {
            // textBox1.Invoke((MethodInvoker)delegate { textBox1.Text = textBox1.Text + e.Data; });
            Invoke(new MethodInvoker(delegate { listBox1.Items.Add(e.Data); }));
        }
    }
    public void StartServer()
    {
        Process p = new Process();
        p.StartInfo = new ProcessStartInfo("java", @"-Xmx1024M -jar " + UltimateMinecraftServerCreator.Properties.Settings.Default.jarname);
        p.StartInfo.WorkingDirectory = UltimateMinecraftServerCreator.Properties.Settings.Default.jarlocator;
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.CreateNoWindow = true;
        p.OutputDataReceived += DataReceived;
        p.Start();
        p.BeginOutputReadLine();
    }

I'm wondering if on this same form if it would be possible to allow users to click buttons that will send a command to the console as well as them being able to type commands if they know them.

Thanks!

Upvotes: 2

Views: 449

Answers (1)

Tayla Wilson
Tayla Wilson

Reputation: 494

Finally worked it out! Was pretty simple really, I moved the declaration of Process p above all the controls code so it became universal and was able to use p.StandardInput.WriteLine("");

Upvotes: 1

Related Questions