Jump to content
Xtreme .Net Talk

Recommended Posts

  • *Experts*
Posted

Yeap. You can control the length and number of lines of text entered into a textbox through it's KeyPress sub. Here's a sample of and address textbox that limits the lines to 3 using an array & the split method to break apart the entered returns. I also measured the string to include text wrapping in the 3 line limit for the last line.

 

   Private Sub txtAddress_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtAddress.KeyPress

       If AscW(e.KeyChar) = 8 Then 'backspace
           e.Handled = False
           Exit Sub
       End If

       If txtAddress.Text.IndexOf(System.Environment.NewLine) > 0 Then
           Dim address() As String = Split(txtAddress.Text, System.Environment.NewLine)

           If address.GetUpperBound(0) > 1 Then
               If address.GetUpperBound(0) >= 2 _
               And AscW(e.KeyChar) = 13 Then 'enter key
                   e.Handled = True
               Else
                   Dim g As Graphics = Me.CreateGraphics
                   If g.MeasureString(address(2), txtAddress.Font).Width >= txtAddress.Width Then
                       e.Handled = True
                   Else : e.Handled = False
                   End If
                   g.Dispose()
               End If
           End If
       End If
   End Sub

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...