count lines of text

  • Thread starter Thread starter Paul Mars
  • Start date Start date
Herfried K. Wagner said:
Notice that this won't prevent the user from pasting longer text.

One could create a User Control that inherits from Textbox and Override the
WndProc Sub

Private Const WM_COPY As Integer = &H301
Private Const WM_PASTE As Integer = &H302
Private Const WM_CUT As Integer = &H300

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

'Stop the control from excepting paste commands
If m.Msg = WM_PASTE Then
Dim txt As String = _
DirectCast(Clipboard.GetDataObject().GetData(GetType(String)), _
String)
Return
End If

MyBase.WndProc(m)

End Sub
 
Notice that this won't prevent the user from pasting longer text.
One could create a User Control that inherits from Textbox and Override the
WndProc Sub

Private Const WM_COPY As Integer = &H301
Private Const WM_PASTE As Integer = &H302
Private Const WM_CUT As Integer = &H300

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

'Stop the control from excepting paste commands
If m.Msg = WM_PASTE Then
Dim txt As String = _
DirectCast(Clipboard.GetDataObject().GetData(GetType(String)), _
String)
Return
End If

MyBase.WndProc(m)

End Sub

Here's an implementation, create a user control and replace all code behind
with the following:

Add user control to a form wherever you would use a textbox.

Imports System.Windows.Forms

Public Class HTIControlTextBox
Inherits TextBox

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
'
'HTIControlTextBox
'
Me.Name = "HTIControlTextBox"
Me.Size = New System.Drawing.Size(168, 16)

End Sub

#End Region

Private Const WM_COPY As Integer = &H301
Private Const WM_PASTE As Integer = &H302
Private Const WM_CUT As Integer = &H300
Public Event UIMessage(ByVal msg As String)

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
'Stop the control from excepting paste commands
If m.Msg = WM_PASTE Then
Dim txt As String = _
DirectCast(Clipboard.GetDataObject().GetData(GetType(String)), _
String)
Dim s() As String = txt.Split(Convert.ToChar(vbLf))
Dim lines As Integer = s.Length - 1 + Me.Lines.Length
If lines > MaxLines Then
RaiseEvent UIMessage("Pasted text exceeds " & MaxLines)
Return
End If
End If
MyBase.WndProc(m)
End Sub

Dim m_MaxLines As Integer = 0
Public Property MaxLines() As Integer
Get
Return m_MaxLines
End Get
Set(ByVal Value As Integer)
m_MaxLines = Value
End Set
End Property

Private Sub HTIControlTextBox_KeyPress(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles
MyBase.KeyPress
If MaxLines = 0 Then Exit Sub
If e.KeyChar = Convert.ToChar(13) And Me.Lines.Length >= MaxLines
Then
e.Handled = True
RaiseEvent UIMessage("Text entered exceeds " & MaxLines)
End If
End Sub
End Class
 
yes, thanks Cindy
p
Cindy M -WordMVP- said:
Hi Paul,

In similar situations, I've known programmers to use a Boolean variable to
suppress the trigger event (i.e. it's set when the "Enter" code starts
executing, and if it has a certain value the event doesn't start this loop).

Couldn't you also use something like KeyPress to pick up when the Enter key
is used, and increment a variable? When it gets to 3, move the focus to the
next field (or whatever).

-- Cindy
 
Back
Top