none
Module Function Wrong Reply RRS feed

  • Question

  • Module Blocker
        Dim rep As String = 0
        Public Function Alph_Spec_locked(ByVal calling As String)
            If ((Microsoft.VisualBasic.Asc(calling) > 64) And _
               (Microsoft.VisualBasic.Asc(calling) < 91) Or _
               (Microsoft.VisualBasic.Asc(calling) > 96) And _
               (Microsoft.VisualBasic.Asc(calling) < 123) Or _ 
               (Microsoft.VisualBasic.Asc(calling) > 32) And _
               (Microsoft.VisualBasic.Asc(calling) < 45) Or _
               (Microsoft.VisualBasic.Asc(calling) = 46) Or _
               (Microsoft.VisualBasic.Asc(calling) = 47) Or _
               (Microsoft.VisualBasic.Asc(calling) > 57) And _
               (Microsoft.VisualBasic.Asc(calling) < 65) Or _
               (Microsoft.VisualBasic.Asc(calling) > 90) And _
               (Microsoft.VisualBasic.Asc(calling) < 97) Or _
               (Microsoft.VisualBasic.Asc(calling) > 122) And _
               (Microsoft.VisualBasic.Asc(calling) < 127) Or _
               (Microsoft.VisualBasic.Asc(calling) > 127) And _
               (Microsoft.VisualBasic.Asc(calling) <= 255)) Then rep = 1
            Return rep
        End Function
    End Module

    I have this Module which I want to use for constraints on text boxes as in this form.

    Public Class Checking
        Dim reply As String
        Dim calling As String
        Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
            reply = Blocker.Alph_Spec_locked(e.KeyChar)
            If reply = 1 Then
                e.Handled = True
                Label9.Text = reply 'For checking the value
                reply = 0
            End If
        End Sub
    End Class

    Problem : The problem that I'm facing is:

    • It works if a key from the keys which aren't locked is pressed.
    • It blocks all keys if a key from the keys which are locked is pressed.
    • I put a label to check if its the fault by the value stored in reply, but its not.

    Any idea would be appreciated, else using these functions locally.

    Thanks.



    Habib Ur Rehman

    Wednesday, January 16, 2019 1:21 PM

Answers


  • I am blocking Alphabetic & Special keys not to be entered in the textbox.

    The textbox will only except Numeric keys.


    Habib Ur Rehman

    How about a numeric TextBox. Also disallows pasting from the Windows Clipboard.

    Public Class NumericTextbox
        Inherits TextBox
    
        Const WM_PASTE As Integer = &H302
        Public Enum DecimalDigits
            ZeroDigits
            OneDigit
            TwoDigits
            ThreeDigits
        End Enum
        Public Property DecimalPlaces As DecimalDigits
        <ComponentModel.Browsable(False)>
        Public ReadOnly Property DoubleValue As Double
            Get
                If String.IsNullOrWhiteSpace(Text) Then
                    Return 0
                Else
                    Return CDbl(Text)
                End If
            End Get
        End Property
        <ComponentModel.Browsable(False)>
        Public ReadOnly Property DecimalValue As Decimal
            Get
                If String.IsNullOrWhiteSpace(Text) Then
                    Return 0
                Else
                    Return CDec(Text)
                End If
            End Get
        End Property
        <ComponentModel.Browsable(False)>
        Public ReadOnly Property IntegerValue As Integer
            Get
                If String.IsNullOrWhiteSpace(Text) Then
                    Return 0
                Else
                    Return CInt(Text)
                End If
            End Get
        End Property
        <ComponentModel.Browsable(False)>
        Public ReadOnly Property FormattedValue As String
            Get
                Select Case DecimalPlaces.ToString
                    Case "ZeroDigits"
                        Return DoubleValue.ToString("F0")
                    Case "OneDigit"
                        Return DoubleValue.ToString("F1")
                    Case "TwoDigits"
                        Return DoubleValue.ToString("F2")
                    Case "ThreeDigits"
                        Return DoubleValue.ToString("F3")
                    Case Else
                        Return ""
                End Select
            End Get
        End Property
        Protected Overrides Sub OnKeyPress(e As KeyPressEventArgs)
            Dim value As String = Text
    
            value = value.Remove(SelectionStart, Me.SelectionLength)
            value = value.Insert(SelectionStart, e.KeyChar)
    
            e.Handled = value.LastIndexOf("-", StringComparison.Ordinal) > 0 Or
                Not (Char.IsControl(e.KeyChar) OrElse Char.IsDigit(e.KeyChar) OrElse
                     (e.KeyChar = "."c And
                      Not Text.Contains(".") Or e.KeyChar = "."c And
                      SelectedText.Contains(".")) OrElse (e.KeyChar = "-"c And
                                                             Me.SelectionStart = 0))
    
            MyBase.OnKeyPress(e)
    
        End Sub
        Protected Overrides Sub WndProc(ByRef m As Message)
            If m.Msg = WM_PASTE Then
                Dim value As String = Text
                value = value.Remove(SelectionStart, SelectionLength)
                value = value.Insert(SelectionStart, Clipboard.GetText)
                Dim result As Decimal = 0
                If Not Decimal.TryParse(value, result) Then
                    Return
                End If
            End If
            MyBase.WndProc(m)
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 16, 2019 2:47 PM
    Moderator

All replies

  • Hello,

    Can you explain in plain English what you are blocking e.g. specific characters and if so what are they as there may be a better direction to take.

    Also it appears you don't have option strict on as when I put your code into a form there were errors which without much thought this is what I came up with and it appears you only want numbers?

    Public Class Form1
        Dim reply As String
        Dim calling As String
        Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
            reply = CType(Blocker.Alph_Spec_locked(e.KeyChar), String)
            If CInt(reply) = 1 Then
                e.Handled = True
                Label9.Text = reply 'For checking the value
                reply = CType(0, String)
            End If
        End Sub
    End Class
    Module Blocker
        Dim rep As Integer = 0
        Public Function Alph_Spec_locked(calling As String) As Integer
            Dim rep As Integer = 0
    
            If ((Asc(calling) > 64) And
                (Asc(calling) < 91) Or
                (Asc(calling) > 96) And
                (Asc(calling) < 123) Or
                (Asc(calling) > 32) And
                (Asc(calling) < 45) Or
                (Asc(calling) = 46) Or
                (Asc(calling) = 47) Or
                (Asc(calling) > 57) And
                (Asc(calling) < 65) Or
                (Asc(calling) > 90) And
                (Asc(calling) < 97) Or
                (Asc(calling) > 122) And
                (Asc(calling) < 127) Or
                (Asc(calling) > 127) And
                (Asc(calling) <= 255)) Then
                rep = 1
            End If
    
            Return rep
        End Function
    End Module
    

    So another possibility is

    Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
        '97 - 122 = Ascii codes for simple letters
        '65 - 90  = Ascii codes for capital letters
        '48 - 57  = Ascii codes for numbers
    
        If Asc(e.KeyChar) <> 8 Then
            If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
                e.Handled = True
            End If
        End If
    End Sub


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Wednesday, January 16, 2019 2:14 PM
    Moderator
  • Hi

    Is this of any help. Any TextBox included in the Handles clause will be treated as Numeric only.

    ' try to make any number of TextBoxes ' numerical input only. Add any ' TextBox names needed into the ' Handling clause. Private Sub TextBox_KeyUp(sender As Object, e As KeyEventArgs)

    Handles TextBox1.KeyUp, TextBox2.KeyUp

    Dim tb As TextBox = DirectCast(sender, TextBox) If Not Integer.TryParse(tb.Text, New Integer()) Then If Not Double.TryParse(tb.Text & "0", New Double()) Then tb.Text = Nothing e.Handled = True End If End If End Sub



    Regards Les, Livingston, Scotland


    • Edited by leshay Wednesday, January 16, 2019 2:17 PM
    Wednesday, January 16, 2019 2:17 PM
  • Hello,

    Can you explain in plain English what you are blocking e.g. specific characters and if so what are they as there may be a better direction to take......

    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    I am blocking Alphabetic & Special keys not to be entered in the textbox.

    The textbox will only except Numeric keys.


    Habib Ur Rehman

    Wednesday, January 16, 2019 2:18 PM
  • Hi

    Is this of any help. Any TextBox included in the Handles clause will be treated as Numeric only.

    ' try to make any number of TextBoxes

    Regards Les, Livingston, Scotland

    Thanks Les, but blocking Numeric keys isn't the Issue.

    According to my module when e.keychar is passed to the it, it has to return either 0 or 1 as defined in the module.

    What it does is that if wrong key is pressed then it constantly returns 1 which doesn't allow any key to be written in the textbox.


    Habib Ur Rehman

    Wednesday, January 16, 2019 2:34 PM

  • I am blocking Alphabetic & Special keys not to be entered in the textbox.

    The textbox will only except Numeric keys.


    Habib Ur Rehman

    How about a numeric TextBox. Also disallows pasting from the Windows Clipboard.

    Public Class NumericTextbox
        Inherits TextBox
    
        Const WM_PASTE As Integer = &H302
        Public Enum DecimalDigits
            ZeroDigits
            OneDigit
            TwoDigits
            ThreeDigits
        End Enum
        Public Property DecimalPlaces As DecimalDigits
        <ComponentModel.Browsable(False)>
        Public ReadOnly Property DoubleValue As Double
            Get
                If String.IsNullOrWhiteSpace(Text) Then
                    Return 0
                Else
                    Return CDbl(Text)
                End If
            End Get
        End Property
        <ComponentModel.Browsable(False)>
        Public ReadOnly Property DecimalValue As Decimal
            Get
                If String.IsNullOrWhiteSpace(Text) Then
                    Return 0
                Else
                    Return CDec(Text)
                End If
            End Get
        End Property
        <ComponentModel.Browsable(False)>
        Public ReadOnly Property IntegerValue As Integer
            Get
                If String.IsNullOrWhiteSpace(Text) Then
                    Return 0
                Else
                    Return CInt(Text)
                End If
            End Get
        End Property
        <ComponentModel.Browsable(False)>
        Public ReadOnly Property FormattedValue As String
            Get
                Select Case DecimalPlaces.ToString
                    Case "ZeroDigits"
                        Return DoubleValue.ToString("F0")
                    Case "OneDigit"
                        Return DoubleValue.ToString("F1")
                    Case "TwoDigits"
                        Return DoubleValue.ToString("F2")
                    Case "ThreeDigits"
                        Return DoubleValue.ToString("F3")
                    Case Else
                        Return ""
                End Select
            End Get
        End Property
        Protected Overrides Sub OnKeyPress(e As KeyPressEventArgs)
            Dim value As String = Text
    
            value = value.Remove(SelectionStart, Me.SelectionLength)
            value = value.Insert(SelectionStart, e.KeyChar)
    
            e.Handled = value.LastIndexOf("-", StringComparison.Ordinal) > 0 Or
                Not (Char.IsControl(e.KeyChar) OrElse Char.IsDigit(e.KeyChar) OrElse
                     (e.KeyChar = "."c And
                      Not Text.Contains(".") Or e.KeyChar = "."c And
                      SelectedText.Contains(".")) OrElse (e.KeyChar = "-"c And
                                                             Me.SelectionStart = 0))
    
            MyBase.OnKeyPress(e)
    
        End Sub
        Protected Overrides Sub WndProc(ByRef m As Message)
            If m.Msg = WM_PASTE Then
                Dim value As String = Text
                value = value.Remove(SelectionStart, SelectionLength)
                value = value.Insert(SelectionStart, Clipboard.GetText)
                Dim result As Decimal = 0
                If Not Decimal.TryParse(value, result) Then
                    Return
                End If
            End If
            MyBase.WndProc(m)
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 16, 2019 2:47 PM
    Moderator
  • Loading into 'Informations' , new module on list by presentation. 

    Having to know where grap res from ps6 with versioning on it. Looking at creations. Finding something solutions where as keep loading pages/ searching exellence on 'original' connecting hyberpage. 

    Wednesday, January 16, 2019 2:57 PM
  • Loading into 'Informations' , new module on list by presentation. 

    Having to know where grap res from ps6 with versioning on it. Looking at creations. Finding something solutions where as keep loading pages/ searching exellence on 'TN'. 


    Tos Passionate Made Different

    Hi

    OK, I fold, too difficult for me, only you left Karen, good luck.


    Regards Les, Livingston, Scotland

    Wednesday, January 16, 2019 2:59 PM
  • Why don't input by cmdlet on 'RUN' to list line-up for it?

    Easy way display components, 'Keyboard/Pointing Device', folded into accessories. Help knows like these formation:

    -----------------------------------------

    Description Standard PS/2 Keyboard

    Name 
    Layout 
    PNP Device ID 
    Number of Function Keys 12
    I/O Port 0x00000060-0x00000060
    I/O Port 0x00000064-0x00000064
    IRQ Channel IRQ 1
    Driver 

    -----------------------------------------


    Wednesday, January 16, 2019 3:18 PM
  • Hi

    Maybe try THIS LINK FOR TRANSLATION

    Type your question in your own language and post the translation.


    Regards Les, Livingston, Scotland


    • Edited by leshay Wednesday, January 16, 2019 3:31 PM
    Wednesday, January 16, 2019 3:31 PM
  • I am blocking Alphabetic & Special keys not to be entered in the textbox.

    Public Class NumericTextbox

    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thanks Karen this will definitely work. Custom Tool is better than calling functions from every where manually.

    And I didn't thought about pasting, You pointed it out and also gave the solution.

    Once again a bundle of thanks you saved my day.


    Habib Ur Rehman

    Wednesday, January 16, 2019 4:19 PM
  • Hi

    Maybe try THIS LINK FOR TRANSLATION

    Type your question in your own language and post the translation.


    Regards Les, Livingston, Scotland

    Les did I said something inconvenience to you?

    If not then why are you upset. If you didn't understood my language that's o.k but what about the code. You could simply run it to understand my language.

    Still sorry that I hurt you.


    Habib Ur Rehman

    Wednesday, January 16, 2019 4:29 PM
  • Hi

    Maybe try THIS LINK FOR TRANSLATION

    Type your question in your own language and post the translation.


    Regards Les, Livingston, Scotland

    Les did I said something inconvenience to you?

    If not then why are you upset. If you didn't understood my language that's o.k but what about the code. You could simply run it to understand my language.

    Still sorry that I hurt you.


    Habib Ur Rehman

    Hi

    You do not need to apologise. I am not upset. I just couldn't understand your posts in any way. I only made a suggestion that I thought you might need if you were having trouble in posting in English.

    Why are you now able to post understandable English when you seemed completely mixed up in previous posts?


    Regards Les, Livingston, Scotland

    Wednesday, January 16, 2019 4:59 PM
  • It cannot think about if plays attachment unit interface like authoring software in reinnovate area.Going up to, question there. What reload , display on analogue?

    Have unseen after mouswheel-click on <ln>https://translate.google.com</ln> Open in Google Translatedetected something to us. As topic descirbe. Had meet with you guys 'TEXT' fewer lines on diagalet box front of whats faced. Everything is improving up. 


    Wednesday, January 16, 2019 5:43 PM
  • It cannot think about if plays attachment unit interface like authoring software in reinnovate area.Going up to, question there. What reload , display on analogue?

    Have unseen after mouswheel-click on <ln>https://translate.google.com</ln> Open in Google Translatedetected something to us. As topic descirbe. Had meet with you guys 'TEXT' fewer lines on diagalet box front of whats faced. Everything is improving up. 


    Hi

    This is an example where I can not make any sense at all from those random words you have posted.

    Please try my suggested Translation link.


    Regards Les, Livingston, Scotland

    Wednesday, January 16, 2019 5:48 PM
  • Understand. Raffirmative. :) What reload , display on analogue should what should read firstly.
    Thursday, January 17, 2019 2:13 AM
  • Karen, how would i do the same for Alphabetic keys.

    Habib Ur Rehman

    Thursday, January 17, 2019 6:14 AM