[WinForm]求解决方案。限定TextBox的输出显示范围。注意:不是录入!

ATGO 2008-07-22 09:02:43
如图所示:TextBox可录入所有字符,而且长度不限定。只限定在界面输出的时候的范围。
图中所示的TextBox是录入了A - Z的效果。求这个的解决方案。

最好是能在TextBox设置或继承TextBox的。
...全文
123 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangqy2008 2008-08-04
  • 打赏
  • 举报
回复
up
ATGO 2008-08-04
  • 打赏
  • 举报
回复
还没看到最佳解决方案。
helloDongXiu 2008-08-01
  • 打赏
  • 举报
回复
up
Boy_Wish 2008-08-01
  • 打赏
  • 举报
回复
Private m_TextWidth As Integer = 0
Private m_Text As String = ""



Public Overridable Property InTextWidth() As Integer
Get
Return m_TextWidth
End Get
Set(ByVal value As Integer)
m_TextWidth = value
End Set
End Property

Public Shadows Property Text() As String
Get
Return m_Text
End Get
Set(ByVal value As String)
m_Text = value
Trace.WriteLine(value)
End Set
End Property

Protected Overrides Sub OnKeyPress(ByVal e As Windows.Forms.KeyPressEventArgs)

If m_TextWidth <> 0 And m_TextWidth < m_Text.Length Then
e.Handled = True
End If

m_Text = MyBase.Text
MyBase.OnKeyPress(e)
Trace.WriteLine(e.KeyChar)
End Sub

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)

'カスタム描画コードをここに追加します。
End Sub
eliphe 2008-07-30
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 Boy_Wish 的回复:]
继承textbox 处理它的onpaint 事件 起出范围的不画出来
[/Quote]

为什么要画出来,这个时候再来过滤数据,不是更好吗?

楼主可以考虑。
ATGO 2008-07-29
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 chenxu4277 的回复:]
在g.FillRectangle(b, new Rectangle(_unvisibleWidth, 0, this.Width,this.Height));
前面加入判断 if(this.Focused!=true)


C# code
private void DrawLine()
{

Graphics g = this.CreateGraphics();
using (SolidBrush b = new SolidBrush(Color.White))
{
if(this.Focused!=true)
g.FillRectangle(b, new Recta…
[/Quote]



录的时候已经限制的了。
chenxu4277 2008-07-29
  • 打赏
  • 举报
回复
在g.FillRectangle(b, new Rectangle(_unvisibleWidth, 0, this.Width,this.Height));
前面加入判断 if(this.Focused!=true)



private void DrawLine()
{

Graphics g = this.CreateGraphics();
using (SolidBrush b = new SolidBrush(Color.White))
{
if(this.Focused!=true)
g.FillRectangle(b, new Rectangle(_unvisibleWidth, 0, this.Width,this.Height));
}
}
chenxu4277 2008-07-29
  • 打赏
  • 举报
回复

public partial class LengthTextBox : TextBox
{
public LengthTextBox()
{

this.Width = 100;

}
private int _unvisibleWidth = 50;
/// <summary>
/// 开始不显示的长度
/// </summary>
public int UnVisibleWidth
{
get
{
return this._unvisibleWidth;
}
set
{
this._unvisibleWidth = value;
this.Invalidate();
}
}
private const int WM_PAINT = 0xF;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_PAINT)
{
DrawLine();
}
}

private void DrawLine()
{

Graphics g = this.CreateGraphics();
using (SolidBrush b = new SolidBrush(Color.White))
{
g.FillRectangle(b, new Rectangle(_unvisibleWidth, 0, this.Width,this.Height));
}
}
}




仿照写了一下,效果不是很好,
ATGO 2008-07-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 Boy_Wish 的回复:]
继承textbox 处理它的onpaint 事件  起出范围的不画出来
[/Quote]

能给个简单的代码吗?谢谢了
Boy_Wish 2008-07-25
  • 打赏
  • 举报
回复
继承textbox 处理它的onpaint 事件 起出范围的不画出来
liumang520 2008-07-23
  • 打赏
  • 举报
回复
up
ATGO 2008-07-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 changjiangzhibin 的回复:]
录完后,在事件(如KeyPressDown)中处理一下,再呈现出来
[/Quote]

不处理。有其它办法吗?
changjiangzhibin 2008-07-22
  • 打赏
  • 举报
回复
录完后,在事件(如KeyPressDown)中处理一下,再呈现出来
caiyizhen808 2008-07-22
  • 打赏
  • 举报
回复
呵呵

8,834

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 组件/控件开发
社区管理员
  • 组件/控件开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧