使文本适合richTextBox

问题描述 投票:0回答:1

我正在寻找一些解决方案来使文本适合框架。我不希望框架是圆形的。我只需要允许用户在其中输入文本。

尝试用 C# 和 .net 形式编写程序。 问题是我不知道该使用什么,因为我用多行和自动换行检查了 richTextBox,但是某个阶段的格式变得可滚动,这是我不想要的。文本应该只适合该图形。

我有什么:

What i've got

我需要什么:

What i need

我希望文本仅适合给定格式的大小而不滚动。

我已经尝试使用 RichTextBox,但用户仍然可以在半行中输入一些内容,结果会将大量文本发送到文本被剪切的行。

       
public class CustomTextBox : RichTextBox
{
    public CustomTextBox()
    { 
        Location = new Point(54, 50);
        Multiline = true;
        WordWrap = true;
        Size = new Size(600, 120);
        Dock = DockStyle.None;
        BorderStyle = BorderStyle.None;
        ScrollBars = RichTextBoxScrollBars.None;
        ForeColor = Color.Black;
        BackColor = Color.Yellow;
    }
}
private void InitializeForm()
{
    CustomTextBox customBox = new CustomTextBox()
    customBox.TextChanged += CustomBox_TextChanged;
    this.Controls.Add(customBox)
}

private void CustomBox_TextChanged(object sender, EventArgs e)
{
    RichTextBox rtb = sender as RichTextBox;
    
    int lastCharIndex = rtb.TextLength - 1;
    if (lastCharIndex <0)
    {
        return;
    }

    Point lastCharPosition = rtb.GetPositionFromCharIndex(lastCharIndex);
    if (lastCharPosition.Y + rtb.Font.Height >= rtb.ClientSize.Height && lastCharPosition.X + rtb.Font.SizeInPoints >= rtb.ClientSize.Width)
    {
        rtb.ReadOnly = true;
    }
c# winforms
1个回答
0
投票

您可以测试一些建议和修改后的自定义控件。

创建控件时,需要在指定的容器中指定

Location

Size
 属性。控件本身不关心其父级内部的布局。

private void InitializeForm() { var customBox = new CustomTextBox() { Font = new Font("Segoe UI", 20f, FontStyle.Regular, GraphicsUnit.Point); Location = new Point(54, 50); Size = new Size(600, 120); } Controls.Add(customBox) }
根据目前的情况,我建议:

    防止用户在此控件内粘贴文本。您
  • 可以在粘贴操作后截断文本,但考虑到它要包含的有限文本,这可以减少您必须执行的测试/检查的数量
  • 防止控件滚动到文本的最后一行。 TextBoxBase 控件默认配置为自动滚动到底部,以便在添加新文本时显示下一个空行。您可能不希望这样,因此从
  • ES_AUTOVSCROLL
     中的控件样式中删除了 
    ES_AUTOHSCROLL
    CreateParams
  • 要限制用户可以输入的文本,您可以选择一些选项,例如跟踪最后一个字符的逻辑位置,以检查它是否仍在 ClientRectangle 的范围内。在这里,我只是测量按下某个键时的文本,以查看输入新字符或生成换行后的文本,并在新内容导致文本溢出时抑制该键。

public class CustomRichTextBox : RichTextBox { const int ES_AUTOVSCROLL = 0x0040; const int ES_AUTOHSCROLL = 0x0080; TextFormatFlags flags = TextFormatFlags.WordBreak | TextFormatFlags.TextBoxControl; public CustomRichTextBox() { BorderStyle = BorderStyle.None; ScrollBars = RichTextBoxScrollBars.None; ForeColor = Color.Black; BackColor = Color.Yellow; } protected override CreateParams CreateParams { get { var cp = base.CreateParams; // Disable auto-scroll to last insert line cp.Style &= ~(ES_AUTOVSCROLL | ES_AUTOHSCROLL); return cp; } } protected override bool ProcessCmdKey(ref Message m, Keys keyData) { if (keyData == (Keys)Shortcut.CtrlV || keyData == (Keys)Shortcut.ShiftIns) { return true; // Suppress CTRL+V & SHIFT+INS } return base.ProcessCmdKey(ref m, keyData); } protected override void OnKeyDown(KeyEventArgs e) { if (!IsInputKey(e.KeyData) && e.KeyCode != Keys.Delete) { var newText = new StringBuilder(Text, TextLength + 2) .Insert(SelectionStart, e.KeyCode == Keys.Enter ? "\nM" : (char)e.KeyCode); var textHeight = TextRenderer.MeasureText(newText.ToString(), Font, ClientSize, flags).Height; if (textHeight > (ClientSize.Height + 1)) { // (0 to n-1) + 1 e.SuppressKeyPress = true; } } base.OnKeyDown(e); } }
    
© www.soinside.com 2019 - 2024. All rights reserved.