首页 > 解决方案 > 当文本长于文本框宽度时禁用文本滚动

问题描述

我在表单上有一个 customTextBox1。customTextbox1多行为假,TextAlign 设置为 center。MaxLength 为 23。customTextBox1 宽度为 92。customTextBox1 字体设置为“MS ゴシック”,12F。当我在 TextBox 中键入“12345678901234567890123”时,文本滚动到最后一个字符。此外,当我单击文本时,文本突出显示为蓝色,我可以拖动到文本的左侧和右侧。

.NetFramework 3.5

我想要的是两件事:

1)当文本长于TextBox宽度时,我不想滚动到最后一个字符。我想在TextBox的右边距停止滚动。例如,当我输入“1234567890123456”时,我想显示“12345678901”并且不应显示剩余的溢出文本。

2)当我单击并拖动文本时,我只想显示“12345678901”并且也想摆脱蓝色突出显示的选择。

1)溢出文本显示 溢出文本正在显示

2)我可以单击并拖动到文本的末尾和文本的开头 我可以单击并拖动到文本的末尾和文本的开头我想防止这种情况

这是我的代码

自定义文本框

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DisabledTextSelectForm
{
    public partial class CustomTextBox : TextBox
    {
        public override bool AutoSize
        {
            get { return base.AutoSize; }
            set { base.AutoSize = value; }
        }

        public CustomTextBox()
        {
            InitializeComponent();
        }
    }
}

表格1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DisabledTextSelectForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            customTextBox1.AutoSize = true;
            customTextBox1.Size = new Size(92,21);
            customTextBox1.Multiline = false;
            customTextBox1.TextAlign = HorizontalAlignment.Center;
            customTextBox1.MaxLength = 23;
            customTextBox1.Font = new Font("MS ゴシック", 12F);


        }
    }
}

更新1:

我想做 textBox 的这种奇怪行为,因为我正在制作一个不再支持的用其他语言编写的应用程序的精确副本。所以我们必须用 C# 来编写它。这两个应用程序都将在 Windows 上运行。在旧的应用程序中,有一个文本框,用户可以在其中输入 ID 号。

1)那个文本框不显示溢出文本。如果我输入 ("12345678901234567890123") ,它只显示 "12345678901" 但如果我单击退格 [13] 次,文本以 "1234567890" 开头。所以我知道溢出文本只是没有显示。

2)我不能像在 C# textBox 中那样左右拖动文本。

不过,我设法复制了 No.1 的行为。这是我的代码

自定义文本框

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Runtime.InteropServices;

namespace DisabledTextSelectForm
{
    public partial class CustomTextBox : TextBox
    {
        public override bool AutoSize
        {
            get { return base.AutoSize; }
            set { base.AutoSize = value; }
        }

        public bool DisabledScrolling { get; set; }

        int caretPos = 0;

        public CustomTextBox()
        {
            InitializeComponent();
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            var isDigit = char.IsDigit(e.KeyChar);
            var isBackSpace = e.KeyChar == (char)Keys.Back;

            var diffWidth = 0;

            if (Text.Length >= 2)
            {
                var firstChar = TextRenderer.MeasureText(Text[0].ToString(), Font);
                var secondChar = TextRenderer.MeasureText(Text.Substring(0, 2).ToString(), Font);

                diffWidth = secondChar.Width - firstChar.Width;

                caretPos = Width / diffWidth;

            }

            if (caretPos != 0 && Text.Length >= caretPos  && DisabledScrolling)
            {
                
                if (isDigit)
                {
                    Text = Text.Length < MaxLength ? Text + e.KeyChar.ToString() : Text;
                }
                else if (isBackSpace)
                {
                    Text = Text.Substring(0,Text.Length - 1);
                }

                ScrollTo(caretPos - 1);
                e.Handled = true;

            }


            base.OnKeyPress(e);
        }

        private void ScrollTo(int scrollPosition)
        {
            if (Text.Length >= scrollPosition)
            {
                Select(scrollPosition, 0);
                ScrollToCaret();
            }
        }

    }
}


表格1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DisabledTextSelectForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            customTextBox1.DisabledScrolling = true;
            customTextBox1.AutoSize = true;
            customTextBox1.Size = new Size(92,21);
            customTextBox1.Multiline = false;
            customTextBox1.TextAlign = HorizontalAlignment.Center;
            customTextBox1.MaxLength = 23;
            customTextBox1.Font = new Font("MS ゴシック", 12F);


        }
    }
}

我知道如何禁用在 TextBox 中单击和拖动文本。

不显示溢出文本 想要禁用选择文本

标签: c#winformstextbox

解决方案


  1. 添加一个计时器,然后以 10 毫秒的间隔将其设置为启用,并在计时器的 Tick 事件中输入以下代码:
    private void timer1_Tick(object sender, EventArgs e)
    {
        textBox1.SelectionLength = 0;
        textBox1.SelectionStart = 0;
        textBox1.ScrollToCaret();
    }
  1. 在 TextBox 的 MouseMove 事件中输入以下代码:
    private void textBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        textBox1.SelectionLength = 0;
        textBox1.SelectionStart = 0;
        textBox1.ScrollToCaret();
    }
  1. 在 TextBox 的 KeyDown 事件中输入以下代码:
    private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        textBox1.SelectionStart = textBox1.Text.Length;
    }
  1. 在初始化块中添加此代码
    public Form1()
    {
        InitializeComponent();

        textBox1.HideSelection = true;
    }

推荐阅读