User Tools

Site Tools


blog:2025-04-17-001

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

blog:2025-04-17-001 [2025/04/17 08:21] (current)
jethro created
Line 1: Line 1:
 +~~NOTOC~~
 +====== 2025-04-17 如何在 C# 的 RichTextBox 中僅保留顯示的最後 20 行 ======
 +  * 要在 C# 的 RichTextBox 中僅保留顯示的最後 20 行,您可以在 TextChanged 事件中檢查行數並確保只保留最後 20 行。以下是具體的實現步驟和範例程式碼:
 +===== 實現步驟 =====
 +  * 監控 TextChanged 事件:當文本變更時觸發事件。
 +  * 檢查行數:如果行數超過 20 行,則僅保留最後 20 行。
 +  * 更新 RichTextBox 的內容:根據需要更新顯示的行。
 +===== 範例程式碼 =====
 +  * 以下是一個完整的範例:以下是一個完整的範例,展示如何在 C# 的 RichTextBox 中僅保留顯示的最後 20 行:
 +  * <sxh c#>using System;
 +using System.Linq;​
 +using System.Windows.Forms;​
 +
 +public class MyForm : Form
 +{
 +    private RichTextBox richTextBox;​
 +
 +    public MyForm()
 +    {
 +        // 初始化 RichTextBox
 +        richTextBox = new RichTextBox
 +        {
 +            Width = 300,
 +            Height = 200, // 根據需要調整高度
 +            WordWrap = true, // 啟用自動換行
 +            ScrollBars = RichTextBoxScrollBars.Vertical // 垂直滾動條
 +        };
 +
 +        // 添加控制項到窗體
 +        this.Controls.Add(richTextBox);​
 +
 +        // 設置初始文本
 +        richTextBox.Text = "​這是一個示例文本。\n請輸入您的文本。\n這裡有幾行內容。\n"​ +
 +                           "​這是第四行。\n這是第五行。\n這是第六行。\n"​ +
 +                           "​這是第七行。\n這是第八行。\n這是第九行。\n"​ +
 +                           "​這是第十行。\n這是第十一行。\n這是第十二行。\n"​ +
 +                           "​這是第十三行。\n這是第十四行。\n這是第十五行。\n"​ +
 +                           "​這是第十六行。\n這是第十七行。\n這是第十八行。\n"​ +
 +                           "​這是第十九行。\n這是第二十行。\n這是第二十一行。\n"​ +
 +                           "​這是第二十二行。";​
 +
 +        // 設置 TextChanged 事件
 +        richTextBox.TextChanged += RichTextBox_TextChanged;​
 +    }
 +
 +    private void RichTextBox_TextChanged(object sender, EventArgs e)
 +    {
 +        // 獲取當前行
 +        string[] lines = richTextBox.Lines;​
 +
 +        // 如果行數超過 20 行,僅保留最後 20 行
 +        if (lines.Length > 20)
 +        {
 +            richTextBox.Lines = lines.Skip(lines.Length - 20).ToArray();​
 +        }
 +    }
 +
 +    [STAThread]
 +    static void Main()
 +    {
 +        Application.EnableVisualStyles();​
 +        Application.Run(new MyForm());
 +    }
 +}</​sxh>​
 +===== 說明 =====
 +  * RichTextBox 控制項: 此程式碼創建了一個 RichTextBox 控制項,並設置其大小、換行和滾動條屬性。
 +  * TextChanged 事件: 當用戶輸入文本時,RichTextBox_TextChanged 方法將被調用。此方法會檢查行數並確保只有最後 20 行被保留。
 +  * 行數處理:​ 使用 Skip 方法來跳過多餘的行,只保留最後 20 行。
 +===== 注意事項 =====
 +  * 此實現會在每次文本變更時進行檢查,因此如果用戶輸入的文本行數超過 20 行,將自動刪除較早的行。
 +  * 可以根據需求調整初始文本或其他屬性。
 +  * 這樣就可以確保 RichTextBox 只顯示最後 20 行文本了!
 +===== TAGS =====
 +  * 
 +====== ======
 +  * {{counter}} person(s) visited this page until now.
 +  * [[:​memo:​index|Back]]
 +====== ======
 +<​html><​!-- ​
 +PDF for A4-Portrait:​ {{pdfjs 50%,450px > xxx.pdf?​page-fit}}
 +PDF for A4-Landscape:​ {{pdfjs 500px,700px > xxx.pdf?​page-fit}}
 +PDF for iPad Note: {{pdfjs 700px,500px > xxx.pdf?​page-fit}}
 +
 +Youtube: {{youtube>​large:​XXXXX}}
 +Code Highlight: <sxh php; first-line: 70; highlight: [89,92]; title: New title attribute in action>
 +
 +--></​html>​
  
blog/2025-04-17-001.txt · Last modified: 2025/04/17 08:21 by jethro