如何在多行文本框中创建命令行

发布于 2024-12-12 03:03:01 字数 502 浏览 0 评论 0原文

我正在使用一些使用 GPIB 的电子仪器。我可以像这样与仪器进行通信:

K2400.WriteString("*IDN?", true);
textBoxK2400.Text += K2400.ReadString() + Environment.NewLine;

第一行将执行命令,在第二行中我将最后一个命令的响应添加到文本框。如何直接在文本框中写入命令并添加响应?

例如,如果用户命令在“>>”等指示符之后输入然后按 ENTER 键,响应应添加到文本框的下一行。

那么如何读取文本框的最后一行并将响应添加到新行中呢?我正在寻找类似的方法:

private void Execute(string command)
{
  K2400.WriteString(command, true);
  textBoxK2400.Text += K2400.ReadString() + Environment.NewLine;
}

I am working with some electronics instruments using GPIB. I can communicate with instruments like this:

K2400.WriteString("*IDN?", true);
textBoxK2400.Text += K2400.ReadString() + Environment.NewLine;

The first line will execute a command, and in the second line I add the response of the last command to the textbox. How can I write the command in the textbox directly and add the response?

For example, if the user command entered after an indicator like ">>" and hitting ENTER, the response should be added in the next line of textbox.

So how can I read the last line of a textbox and add the respone in a new line? I am looking for a method like:

private void Execute(string command)
{
  K2400.WriteString(command, true);
  textBoxK2400.Text += K2400.ReadString() + Environment.NewLine;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧伤还要旧人安 2024-12-19 03:03:01

使用两个文本框(文本框和列表框可能更好),但使它们看起来像“一个”文本框。如果使用 WPF,它看起来会非常漂亮,并且至少可以采用 Windows 形式。

做了一个快速测试..

在此处输入图像描述

并使用文本框的 KeyPress 事件的代码:

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.Return)
        {
            listBox1.Items.Add(textBox1.Text);
            textBox1.Text = String.Empty;
            listBox1.SelectedIndex = listBox1.Items.Count - 1;
        }
    }

Use two Text boxes(textbox and a listbox might be better) but make them look as "one" textbox.. If using WPF it could look pretty nice and in Windows form possible at least.

Did a quick test..

enter image description here

And with this code for KeyPress event for the textbox:

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.Return)
        {
            listBox1.Items.Add(textBox1.Text);
            textBox1.Text = String.Empty;
            listBox1.SelectedIndex = listBox1.Items.Count - 1;
        }
    }
可可 2024-12-19 03:03:01

你可以试试这个:

private void textBoxK2400_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Return)
    {
        string command = textBoxK2400.Text.Split('\n').LastOrDefault();
        if (!String.IsNullOrEmpty(command) && command.StartsWith(">>"))
        {
            K2400.WriteString(command.Substring(2), true);
            textBoxK2400.Text += K2400.ReadString() + Environment.NewLine;
            textBoxK2400.Text += ">>"; // It's not necessary
        }
    }
}

You could try this:

private void textBoxK2400_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Return)
    {
        string command = textBoxK2400.Text.Split('\n').LastOrDefault();
        if (!String.IsNullOrEmpty(command) && command.StartsWith(">>"))
        {
            K2400.WriteString(command.Substring(2), true);
            textBoxK2400.Text += K2400.ReadString() + Environment.NewLine;
            textBoxK2400.Text += ">>"; // It's not necessary
        }
    }
}
埋情葬爱 2024-12-19 03:03:01

private void Execute(字符串命令) { K2400.WriteString(命令,
真的); textBoxK2400.Text += K2400.ReadString() + Environment.NewLine;
}

这样。我只是建议“缓冲”文本的一部分,而不是全部,因为到最后它可能会很长。您可以将其拆分为前面的行并获取多行(即 10 行)。

并且不要忘记将字段设置为黑色,文本设置为绿色,当命令字段这样装饰时看起来更专业。

private void Execute(string command) { K2400.WriteString(command,
true); textBoxK2400.Text += K2400.ReadString() + Environment.NewLine;
}

this is it. I'd just recommend to 'buffer' a part of the text, not all, because it could be long by the end. You can split it to lines before and take a number of lines (i. e. 10).

And don't forget to make the field black and the text green, it looks much more professional when the command field is decorated such way.

迷荒 2024-12-19 03:03:01

首先我建议使用 RichTextBox。
要捕获 ENTER 您应该使用 KeyPress Event 。

private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            { 
                string LastLine = richTextBox1.Lines[richTextBox1.Lines.Length-2];
                if (LastLine.StartsWith(">>"))
                {
                    //here you can filter the LastLine
                    K2400.WriteString(LastLine, true);
                    richTextBox1.AppendText(K2400.ReadString() + Environment.NewLine);
                }
                else
                {
                    //here you can unwrite the last line
                    string[] totalLines = richTextBox1.Lines;
                    richTextBox1.Text = "";
                    for (int i = 0; i < totalLines.Length - 2; i++)
                    {
                        richTextBox1.AppendText(totalLines[i]+Environment.NewLine);
                    }
                    MessageBox.Show("That was not a valid command");
                }
            }
        }

Well first i would suggest a RichTextBox to use.
To capture ENTER you should use KeyPress Event .

private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            { 
                string LastLine = richTextBox1.Lines[richTextBox1.Lines.Length-2];
                if (LastLine.StartsWith(">>"))
                {
                    //here you can filter the LastLine
                    K2400.WriteString(LastLine, true);
                    richTextBox1.AppendText(K2400.ReadString() + Environment.NewLine);
                }
                else
                {
                    //here you can unwrite the last line
                    string[] totalLines = richTextBox1.Lines;
                    richTextBox1.Text = "";
                    for (int i = 0; i < totalLines.Length - 2; i++)
                    {
                        richTextBox1.AppendText(totalLines[i]+Environment.NewLine);
                    }
                    MessageBox.Show("That was not a valid command");
                }
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文