none
Make a Private Sub

    Question

  • Hello,

    Is it possible to make a Private Sub Allow of this code ?

    Know that i have to replace Bbas.text with something like textbox but i can't figure it out.

    Private Sub Allow()

     Dim charactersAllowed As String = "1234567890" Dim theText As String = Bbas.Text Dim Letter As String Dim SelectionIndex As Integer = Bbas.SelectionStart Dim Change As Integer For x As Integer = 0 To Bbas.Text.Length - 1 Letter = Bbas.Text.Substring(x, 1) If charactersAllowed.Contains(Letter) = False Then theText = theText.Replace(Letter, String.Empty) Change = 1 End If Next Bbas.Text = theText Bbas.Select(SelectionIndex - Change, 0) End Sub


    Thursday, December 6, 2018 2:33 PM

Answers

  • Probably you need this:

    Private Sub Allow( Bbas as TextBox )

       . . .

    End Sub

     

    Now you can call it for different textboxes, for example:

       Allow( MyTextBox1 )

       Allow( MyTextBox2 )


    • Marked as answer by KeesBlunder Thursday, December 6, 2018 7:21 PM
    Thursday, December 6, 2018 6:56 PM

All replies

  • Hi

    The closest I can guess from your question is that you seem to want a way to limit the User input into a TextBox - maybe I am way off. Anyway, if your needs do coincide with my guess, then here is some code to illustrate one way to do it. It limits the input to only the characters in Allowed and only allows one occurance of the '.+' and only allows the '-' at the start of the input.

    ' Limit user input
    ' Numeric TextBox Input
    
    ' Form1 with TextBox1
    Option Strict On
    Option Explicit On
    Public Class Form1
    
      Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    
        Dim t As TextBox = DirectCast(sender, TextBox)
    
        t.Text = t.Text.Replace("."c, ","c)
    
        If t.Text.StartsWith(".") Or t.Text.StartsWith(",") Then t.Text = "0" & t.Text
        t.SelectionStart = t.MaxLength
        t.SelectionLength = 0
    
        Dim tb() As Char = t.Text.ToCharArray
    
        Dim allowed As String = "0123456789.+-" & Chr(8)
    
        If allowed.IndexOf(e.KeyChar) = -1 Or (e.KeyChar = "." And tb.Count(Function(c) c = "." AndAlso tb.Count > 0) > 0) Or (e.KeyChar = "," And tb.Count(Function(c) c = "," AndAlso tb.Count > 0) > 0) Or (e.KeyChar = "-" And tb.Count > 0) Or (e.KeyChar = "+" And tb.Count > 0) Or (e.KeyChar = "." And t.Text.Contains(",")) Then
          e.Handled = True
        End If
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, December 6, 2018 3:06 PM
    Thursday, December 6, 2018 3:03 PM
  • Hello Les ,

    Maybe is ask the question wrong.

    The code works fine when i use it in Keypress , but i want to use it in about 12 textboxes or so .

    When i make a Private Sub is save text so not to use al of the text 12 times.

    Thursday, December 6, 2018 6:34 PM
  • Probably you need this:

    Private Sub Allow( Bbas as TextBox )

       . . .

    End Sub

     

    Now you can call it for different textboxes, for example:

       Allow( MyTextBox1 )

       Allow( MyTextBox2 )


    • Marked as answer by KeesBlunder Thursday, December 6, 2018 7:21 PM
    Thursday, December 6, 2018 6:56 PM
  • Thanks Viorel_ ,

    Works great.

    Thursday, December 6, 2018 7:21 PM