簡體   English   中英

限制文本框中每行的最大字符數

[英]Limit the max number of chars per line in a textbox

說我有以下內容:

<TextBox TextWrapping="Wrap" 
         AcceptsReturn="True" 
         AcceptsTab="True" 
         MaxLines="3000"/>

有沒有辦法可以將每行的最大字符數限制為60?

我已經看到了通過keydown事件做到這一點的方法,但這似乎並不是萬無一失的(即在長文本塊中粘貼的內容)。

選擇單聲道間隔字體。 並計算具有60個字符的文本框的寬度。

鑒於您正在響應keydown事件,我假設您要確保TextBox后面的字符串遵循“每行60個字符”規則。 如果是這種情況,您應該在TextBox上創建一個訂閱TextChanged事件的事件。 在那里你可以修復文本,並截斷或拆分太長的行。

(編輯)要解決顯示部分,你可以像Kafuka建議的那樣:只需將盒子放寬到足以容納60個字符,如果你想確定,可以使用等寬字體。 如果你確定字符串是正確的,這應該很容易排成一行。

我真的不認為你可以在包裝時這樣做,因為換行會改變當前行以適應TextBox 即使在使用記事本時 ,也無法在啟用Word Wrap時查看狀態欄 ,因為在行時很難獲得當前行索引及其長度。

我已設法設置每行的最大字符數,而TextWrapping屬性設置為NoWrap 您首先需要獲取當前行索引的長度。 然后,如果它是59或更多,處理輸入。

<TextBox Name="textBox1"
         TextWrapping="NoWrap" 
         AcceptsReturn="True" 
         AcceptsTab="True" 
         MaxLines="3000"
         KeyDown="textBox1_KeyDown"/>
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    //Initialize a new int of name CurrentLine to get the current line the user is on
    int CurrentLine = textBox1.GetLineIndexFromCharacterIndex(textBox1.Text.Length);

    //Continue if the length of the current line is more or equal to 59
    if (textBox1.GetLineLength(CurrentLine) >= 59) 
    {
        //Don't insert the character
        e.Handled = true; 
    }
}

謝謝,
我希望這個對你有用 :)

我知道這是一個非常晚的答案,但是找到這個的人可以得到一個很好的答案,例如Ctrl + C和東西:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    foreach (string line in textBox1.Lines)
    {
        if (line.Length > 60)
        {
            textBox1.Undo();
        }
    }

    textBox1.ClearUndo();
}

請注意,這確實意味着您不能再在文本框中使用Ctrl + Z,但如果這不打擾您,這是一個不錯的選擇,因為它適用於任何字體。

編輯這不適用於wpf文本框,只有Windows形成文本框

我今天看了幾個解決方案。 他們要么根本不工作,要么他們工作,他們不按我認為應該的方式工作。 光標位置的奇數或包裝不正確。 這是我的“解決方案”,我希望它能幫助將來的某個人。 它包裝,不打開,並保留任何現有的新行。 我將此示例設置為60個字符寬,並且在此示例之外使用bool isBusyUpdating以防止在更新進行時再次觸發。

        txtNotes.HorizontalContentAlignment = HorizontalAlignment.Left;
        txtNotes.VerticalContentAlignment = VerticalAlignment.Top;
        txtNotes.TextWrapping = TextWrapping.NoWrap;
        txtNotes.AcceptsReturn = true;
        txtNotes.TextChanged += delegate (object o, TextChangedEventArgs args)
        {
            //args.Handled = true;
            TextBox thisText = (TextBox)args.Source;
             if (!isBusyUpdating)
            {
                isBusyUpdating = true;
                string updatedText = "";
                bool blnUpdate = false;
                int curPos = thisText.CaretIndex;

                foreach (string thisLine in thisText.Text.Split('\n'))
                {

                    if (thisLine.Length > 60)
                    {
                        blnUpdate = true;
                        updatedText = updatedText + 
                            thisLine.Substring(0, 60)
                            .Replace("\n", "") +
                                      "\n" + thisLine.Substring(60);
                        curPos++;
                    }
                    else
                    {
                        updatedText = updatedText + thisLine + "\n";
                    }
                }

                if (blnUpdate)
                {
                    thisText.Text = updatedText;
                    thisText.CaretIndex = curPos-1;
                }
                isBusyUpdating = false;

            }

        };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM