在 RichTextBox 顶部插入

发布于 2024-08-12 15:51:59 字数 338 浏览 2 评论 0原文

这段代码有什么问题吗?尝试让我的文本插入到文本框的开头而不是底部。

private void execute_Click(object sender, EventArgs e){
  startFinshBox.Text = "Start Time: " + printTime()+"";
  startFinshBox.Text.Insert(0,printTime()+": Retrieving Results...\n");
}

但它不会将第二行插入到 rtb 中。我也尝试过 startFinishBox.SelectionStart = 0 ,但没有什么区别。我还缺少其他东西吗?

谢谢,齐

Whats wrong with this code? Trying to get my text to insert at the beginning of the textbox rather than at the bottom.

private void execute_Click(object sender, EventArgs e){
  startFinshBox.Text = "Start Time: " + printTime()+"";
  startFinshBox.Text.Insert(0,printTime()+": Retrieving Results...\n");
}

But it will not insert the second line into the rtb. I have tried with startFinishBox.SelectionStart = 0 as well, and it made no difference. Am I missing something else?

Thanks, Psy

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

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

发布评论

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

评论(3

软糖 2024-08-19 15:51:59

startFinshBox.Text 是一个字符串,它是 C# 中的不可变类型。 string.Insert() 将返回修改后的字符串作为结果,但在您的代码中您将丢弃它。要使其工作,您必须将代码更改为:

private void execute_Click(object sender, EventArgs e){
  startFinshBox.Text = "Start Time: " + printTime()+"";
  startFinshBox.Text = startFinshBox.Text.Insert(0,printTime()+": Retrieving Results...\n");
}

startFinshBox.Text is a string, which is an immutable type in C#. string.Insert() will return the modified string as a result, but it your code you discard it. To make it work, you have to change the code to:

private void execute_Click(object sender, EventArgs e){
  startFinshBox.Text = "Start Time: " + printTime()+"";
  startFinshBox.Text = startFinshBox.Text.Insert(0,printTime()+": Retrieving Results...\n");
}
假装不在乎 2024-08-19 15:51:59

TextBox 上的 SelectionStart 属性将确定从何处选择或插入文本。

使用此代码在文本框控件的开头插入代码:

TextBox.SelectionStart = 0;
TextBox.SelectedText = "Start time: " + printTime();

The SelectionStart property on a TextBox will determine where text will be selected or inserted from.

Use this code to insert code at the start of the text box control:

TextBox.SelectionStart = 0;
TextBox.SelectedText = "Start time: " + printTime();
好倦 2024-08-19 15:51:59

ShowText 在 RichTextBox 顶部插入一个新行。
Invoke 使您能够从另一个线程调用它。
LineLimit 在达到 LineLimit 后删除最后一行。
如果将 RichTextBox_ContentsResized 事件处理程序添加到控件中,它会自动调整其大小。

    public bool? ShowText(string pText)
    {
        try
        {
            if (InvokeRequired)
                return Invoke(new Func<string, bool?>(ShowText), pText) as bool?;

            rtbRemoteControl.Text = rtbRemoteControl.Text.Insert(0, $"{DateTime.Now:HH:mm:ss} - {pText}\n");

            var lLineCount = rtbRemoteControl.Lines.Length;
            const int lLineLimit = 500;
            if (lLineCount > lLineLimit)
                rtbRemoteControl.Lines = rtbRemoteControl.Lines.Take(lLineCount - 1).ToArray();
        }
        catch
        {
            return false;
        }
        return true;
    }

    private void RichTextBox_ContentsResized(object pSender, ContentsResizedEventArgs e)
    {
        const int margin = 5;
        var lSender = pSender as RichTextBox;
        if (lSender == null)
            return;
        lSender.ClientSize = new Size(e.NewRectangle.Width + margin,  e.NewRectangle.Height + margin);
    }

ShowText inserts a new Line at top of the RichTextBox.
Invoke enables you to call it from another thread.
LineLimit removes the last line after the LineLimit is reached.
If you add the RichTextBox_ContentsResized eventhandler to your control, its adjusting its size automatically.

    public bool? ShowText(string pText)
    {
        try
        {
            if (InvokeRequired)
                return Invoke(new Func<string, bool?>(ShowText), pText) as bool?;

            rtbRemoteControl.Text = rtbRemoteControl.Text.Insert(0, 
quot;{DateTime.Now:HH:mm:ss} - {pText}\n");

            var lLineCount = rtbRemoteControl.Lines.Length;
            const int lLineLimit = 500;
            if (lLineCount > lLineLimit)
                rtbRemoteControl.Lines = rtbRemoteControl.Lines.Take(lLineCount - 1).ToArray();
        }
        catch
        {
            return false;
        }
        return true;
    }

    private void RichTextBox_ContentsResized(object pSender, ContentsResizedEventArgs e)
    {
        const int margin = 5;
        var lSender = pSender as RichTextBox;
        if (lSender == null)
            return;
        lSender.ClientSize = new Size(e.NewRectangle.Width + margin,  e.NewRectangle.Height + margin);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文