c# - 当文本长于文本框宽度时禁用文本滚动
问题描述
我在表单上有一个 customTextBox1。customTextbox1多行为假,TextAlign 设置为 center。MaxLength 为 23。customTextBox1 宽度为 92。customTextBox1 字体设置为“MS ゴシック”,12F。当我在 TextBox 中键入“12345678901234567890123”时,文本滚动到最后一个字符。此外,当我单击文本时,文本突出显示为蓝色,我可以拖动到文本的左侧和右侧。
.NetFramework 3.5
我想要的是两件事:
1)当文本长于TextBox宽度时,我不想滚动到最后一个字符。我想在TextBox的右边距停止滚动。例如,当我输入“1234567890123456”时,我想显示“12345678901”并且不应显示剩余的溢出文本。
2)当我单击并拖动文本时,我只想显示“12345678901”并且也想摆脱蓝色突出显示的选择。
这是我的代码
自定义文本框
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 中单击和拖动文本。
解决方案
- 添加一个计时器,然后以 10 毫秒的间隔将其设置为启用,并在计时器的 Tick 事件中输入以下代码:
private void timer1_Tick(object sender, EventArgs e)
{
textBox1.SelectionLength = 0;
textBox1.SelectionStart = 0;
textBox1.ScrollToCaret();
}
- 在 TextBox 的 MouseMove 事件中输入以下代码:
private void textBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
textBox1.SelectionLength = 0;
textBox1.SelectionStart = 0;
textBox1.ScrollToCaret();
}
- 在 TextBox 的 KeyDown 事件中输入以下代码:
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
textBox1.SelectionStart = textBox1.Text.Length;
}
- 在初始化块中添加此代码
public Form1()
{
InitializeComponent();
textBox1.HideSelection = true;
}
推荐阅读
- css - 仅使用 CSS 的 Google Chrome 下载文件
- ace-editor - Ace 编辑器多个光标
- c++ - 编写一个将 LLVM IR 文件作为命令行参数的程序
- vuejs2 - 在 Vue2 中以另一种方法检索 e.target.files
- java - 使用 Xero 的 Java SDK 支持多个私有应用的身份验证?
- android - 我想用 FCM 在 android studio 中创建音频呼叫应用程序是否可以
- android - 未定义对“cnss_get_platform_cap”的引用
- nginx - 只有当主进程以超级用户权限运行时,“user”指令才有意义
- node.js - 在mongodb中保存小数点后4位
- javascript - 我想知道如何修复触摸友好的滑块