怎样限制RichTextBox输入

thw19850316 2008-03-03 11:52:21
怎样限制RICHTEXTBOX输入的长度,内容包括中文,英文,数字等其他字符,也可以包括图片,但是输入不能超过RICHTEXTBOX的宽度(RICHTEXTBOX.WIDTH固定),怎样做?请高手帮忙,谢谢!
...全文
392 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
thw19850316 2008-03-07
  • 打赏
  • 举报
回复
呵呵,是的
麻烦你了!
cbm6666 2008-03-06
  • 打赏
  • 举报
回复
如果只有设定并判断 "每一行" 的各别总字数与符号等不能超过设定的宽度那就很简单了,

有图片的话, 这个文档肯定是 .rtf 因为只有 .rtf 才能存图,理论上难度不大, 明天我试着做看,今天太晚了.

fengkoulangjian 2008-03-06
  • 打赏
  • 举报
回复
学习~
cbm6666 2008-03-06
  • 打赏
  • 举报
回复
哎,晕倒了,一个图片插入文字里面,不管图多大也只占一个字符, 怎么会这样呢 ?

要换个思路用光标位置了.


thw19850316 2008-03-06
  • 打赏
  • 举报
回复
好的,没关系!
cbm6666 2008-03-06
  • 打赏
  • 举报
回复
呵呵...没有十足的把握, Richtextbox也不常用,吃完饭就试看吧.



thw19850316 2008-03-06
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 cbm666 的回复:]
如果只有设定并判断 "每一行" 的各别总字数与符号等不能超过设定的宽度那就很简单了,

有图片的话, 这个文档肯定是 .rtf 因为只有 .rtf 才能存图,理论上难度不大, 明天我试着做看,今天太晚了.
[/Quote]

就是一行了,呵呵,谢谢啊!
thw19850316 2008-03-05
  • 打赏
  • 举报
回复
等了我好几天了啊。。。
thw19850316 2008-03-05
  • 打赏
  • 举报
回复
等待中。。。
SamG9527 2008-03-04
  • 打赏
  • 举报
回复
protected override void WndProc(ref System.Windows.Forms.Message m)
{}
中間用正則表達式判斷
thw19850316 2008-03-04
  • 打赏
  • 举报
回复
Private Sub TrueLength
Dim ret As Long
Dim intStart As Integer, intEnd As Integer, intStartX As Integer, intEndX As Integer

txtTotal.SelStart = 0
txtTotal.SelLength = Len(txtTotal.Text)

ret = SendMessage(txtTotal.hWnd, EM_GETSEL, 0, 0)
intStart = ret And 65535
intEnd = (ret And &HFFFF0000) / (2 ^ 16)
ret = SendMessage(txtTotal.hWnd, EM_POSFROMCHAR, intStart, 0)
intStartX = ret And 65535
ret = SendMessage(txtTotal.hWnd, EM_POSFROMCHAR, intEnd + 1, 0)
intEndX = ret And 65535
length = intEndX - intStartX
End Sub
这段代码可以得到实际宽度,但是放到Keypress事件中就不行了,
txtTotal.SelStart = 0
txtTotal.SelLength = Len(txtTotal.Text),因为这两句导致只能输入一个字符

用户 昵称 2008-03-04
  • 打赏
  • 举报
回复
俺用richecit做不到
thw19850316 2008-03-04
  • 打赏
  • 举报
回复
哦,那是否可以用RICHTEXT做到啊?
用户 昵称 2008-03-04
  • 打赏
  • 举报
回复
写示例的时候是用的text,测试正常,换成了richedit发现richedit的字符是可以随意的,所以不正确了,跟是否中文无关的。
thw19850316 2008-03-04
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 superwat 的回复:]
protected override void WndProc(ref System.Windows.Forms.Message m)
{}
中間用正則表達式判斷
[/Quote]

不会,可以帮我写出来吗?谢谢!
用户 昵称 2008-03-03
  • 打赏
  • 举报
回复
单行还是多行?
thw19850316 2008-03-03
  • 打赏
  • 举报
回复
中文不行啊
用户 昵称 2008-03-03
  • 打赏
  • 举报
回复
那就是vb6的。
thw19850316 2008-03-03
  • 打赏
  • 举报
回复
上面是vb.net,用VB6如何写啊?
用户 昵称 2008-03-03
  • 打赏
  • 举报
回复
Option Explicit
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ExtTextOut Lib "gdi32" Alias "ExtTextOutA" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal wOptions As Long, ByVal lpRect As Any, ByVal lpString As String, ByVal nCount As Long, lpDx As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" (ByVal hdc As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As POINTAPI) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long

Private Sub Form_Load()
Me.Font = "宋体"
Text1.Font = "宋体"

Me.FontSize = 12
Text1.FontSize = 12

Text1.Text = ""

End Sub

Private Sub Text1_Change()
Dim l As Long
Dim w As Long
Dim textsize As POINTAPI
l = LenB(StrConv(Text1.Text, vbFromUnicode))
w = GetTextExtentPoint32(Me.hdc, Text1.Text, l, textsize)
If l * textsize.X > Text1.Width Then
Text1.Text = Mid(Text1.Text, 1, Len(Text1.Text) - 1)
Text1.SelStart = Len(Text1.Text)
End If
End Sub
加载更多回复(4)

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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