Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Apply formatting on multiple Text boxes within a Group box.

Apply formatting on multiple Text boxes within a Group box.

Scheduled Pinned Locked Moved .NET (Core and Framework)
helptutorial
7 Posts 5 Posters 11 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Interplain
    wrote on last edited by
    #1

    Hello, I have a request for help with how to format a text box with numbers with 3 digits with two decimal places. but will not advance until a decimal point is placed, this works with one box. but i would like to implement this on 14 text boxes in a group box. so i don't have to duplicate the code 14 times.

    Private Sub RollTemp_BOTTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RollTemp_BOTTextBox.KeyPress
    Dim keyChar = e.KeyChar
    If Char.IsControl(keyChar) Then
    'Allow all control characters.
    ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
    Dim text = Me.RollTemp_BOTTextBox.Text
    Dim selectionStart = Me.RollTemp_BOTTextBox.SelectionStart
    Dim selectionLength = Me.RollTemp_BOTTextBox.SelectionLength
    text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)
    If Integer.TryParse(text, New Integer) AndAlso text.Length > 3 Then
    'Reject an integer that is longer than 16 digits.
    e.Handled = True
    ElseIf Double.TryParse(text, New Double) AndAlso text.IndexOf("."c) < text.Length - 4 Then
    'Reject a real number with two many decimal places.
    e.Handled = True
    End If
    Else
    'Reject all other characters.
    e.Handled = True
    End If
    End Sub

    The Text Box will have like 123.67 in it. But if you enter 123 it doesn't let you put another number in and advance until you put the (.) in and then allows you to put another number in. Thank you

    P Richard DeemingR 2 Replies Last reply
    0
    • I Interplain

      Hello, I have a request for help with how to format a text box with numbers with 3 digits with two decimal places. but will not advance until a decimal point is placed, this works with one box. but i would like to implement this on 14 text boxes in a group box. so i don't have to duplicate the code 14 times.

      Private Sub RollTemp_BOTTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RollTemp_BOTTextBox.KeyPress
      Dim keyChar = e.KeyChar
      If Char.IsControl(keyChar) Then
      'Allow all control characters.
      ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
      Dim text = Me.RollTemp_BOTTextBox.Text
      Dim selectionStart = Me.RollTemp_BOTTextBox.SelectionStart
      Dim selectionLength = Me.RollTemp_BOTTextBox.SelectionLength
      text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)
      If Integer.TryParse(text, New Integer) AndAlso text.Length > 3 Then
      'Reject an integer that is longer than 16 digits.
      e.Handled = True
      ElseIf Double.TryParse(text, New Double) AndAlso text.IndexOf("."c) < text.Length - 4 Then
      'Reject a real number with two many decimal places.
      e.Handled = True
      End If
      Else
      'Reject all other characters.
      e.Handled = True
      End If
      End Sub

      The Text Box will have like 123.67 in it. But if you enter 123 it doesn't let you put another number in and advance until you put the (.) in and then allows you to put another number in. Thank you

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      You can always reuse the same event handler for the different textboxes. What I would do is rename this event handler so it doesn't refer to the control by name and then point each of the relevant controls KeyPress events at this event handler. As the sender refers to the textbox that raises the event, you can refactor the method to avoid using Me.RollTemp_BOTTextBox by casting sender to a TextBox type.

      Advanced TypeScript Programming Projects

      I 1 Reply Last reply
      0
      • P Pete OHanlon

        You can always reuse the same event handler for the different textboxes. What I would do is rename this event handler so it doesn't refer to the control by name and then point each of the relevant controls KeyPress events at this event handler. As the sender refers to the textbox that raises the event, you can refactor the method to avoid using Me.RollTemp_BOTTextBox by casting sender to a TextBox type.

        Advanced TypeScript Programming Projects

        I Offline
        I Offline
        Interplain
        wrote on last edited by
        #3

        Would you give an example please, i am not following your explanation. Would be grateful.

        L 1 Reply Last reply
        0
        • I Interplain

          Would you give an example please, i am not following your explanation. Would be grateful.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Start by creating a collection / list that contains your "14 text boxes". How you create your list "depends" on what you're doing now. That's enough of a task for the time being.

          It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

          1 Reply Last reply
          0
          • I Interplain

            Hello, I have a request for help with how to format a text box with numbers with 3 digits with two decimal places. but will not advance until a decimal point is placed, this works with one box. but i would like to implement this on 14 text boxes in a group box. so i don't have to duplicate the code 14 times.

            Private Sub RollTemp_BOTTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RollTemp_BOTTextBox.KeyPress
            Dim keyChar = e.KeyChar
            If Char.IsControl(keyChar) Then
            'Allow all control characters.
            ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
            Dim text = Me.RollTemp_BOTTextBox.Text
            Dim selectionStart = Me.RollTemp_BOTTextBox.SelectionStart
            Dim selectionLength = Me.RollTemp_BOTTextBox.SelectionLength
            text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)
            If Integer.TryParse(text, New Integer) AndAlso text.Length > 3 Then
            'Reject an integer that is longer than 16 digits.
            e.Handled = True
            ElseIf Double.TryParse(text, New Double) AndAlso text.IndexOf("."c) < text.Length - 4 Then
            'Reject a real number with two many decimal places.
            e.Handled = True
            End If
            Else
            'Reject all other characters.
            e.Handled = True
            End If
            End Sub

            The Text Box will have like 123.67 in it. But if you enter 123 it doesn't let you put another number in and advance until you put the (.) in and then allows you to put another number in. Thank you

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            As Pete suggested:

            Private Sub NumericTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RollTemp_BOTTextBox.KeyPress, OtherTextBox.KeyPress, YetAnotherTextBox.KeyPress
            Dim keyChar = e.KeyChar
            If Char.IsControl(keyChar) Then
            'Allow all control characters.
            ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
            Dim box As TextBox = DirectCast(sender, TextBox)
            Dim text As String = box.Text
            Dim selectionStart As Integer = box.SelectionStart
            Dim selectionLength As Integer = box.SelectionLength
            text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)

                Dim valueInt32 As Integer
                Dim valueDouble As Double
                
                If Integer.TryParse(text, valueInt32) AndAlso valueInt32 > 999 Then
                    ' Reject an integer that is longer than 3 digits.
                    e.Handled = True
                ElseIf Double.TryParse(text, valueDouble) AndAlso text.IndexOf("."c) < text.Length - 4 Then
                    ' Reject a real number with two many decimal places.
                    e.Handled = True
                End If
            Else
                ' Reject all other characters.
                e.Handled = True
            End If
            

            End Sub

            How to: Connect Multiple Events to a Single Event Handler - Windows Forms .NET Framework | Microsoft Docs[^]


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            I 1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              As Pete suggested:

              Private Sub NumericTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RollTemp_BOTTextBox.KeyPress, OtherTextBox.KeyPress, YetAnotherTextBox.KeyPress
              Dim keyChar = e.KeyChar
              If Char.IsControl(keyChar) Then
              'Allow all control characters.
              ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
              Dim box As TextBox = DirectCast(sender, TextBox)
              Dim text As String = box.Text
              Dim selectionStart As Integer = box.SelectionStart
              Dim selectionLength As Integer = box.SelectionLength
              text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)

                  Dim valueInt32 As Integer
                  Dim valueDouble As Double
                  
                  If Integer.TryParse(text, valueInt32) AndAlso valueInt32 > 999 Then
                      ' Reject an integer that is longer than 3 digits.
                      e.Handled = True
                  ElseIf Double.TryParse(text, valueDouble) AndAlso text.IndexOf("."c) < text.Length - 4 Then
                      ' Reject a real number with two many decimal places.
                      e.Handled = True
                  End If
              Else
                  ' Reject all other characters.
                  e.Handled = True
              End If
              

              End Sub

              How to: Connect Multiple Events to a Single Event Handler - Windows Forms .NET Framework | Microsoft Docs[^]


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              I Offline
              I Offline
              Interplain
              wrote on last edited by
              #6

              Thank you Pete, i see now what you mean. So easy when you have the expertise. :)

              R 1 Reply Last reply
              0
              • I Interplain

                Thank you Pete, i see now what you mean. So easy when you have the expertise. :)

                R Offline
                R Offline
                Ralf Meier
                wrote on last edited by
                #7

                this could also be your Solution - integrate the functionality into your own customized Textbox :

                Public Class NumericTextbox
                Inherits TextBox

                Private Sub NumericTextBox\_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
                    Dim keyChar = e.KeyChar
                    If Char.IsControl(keyChar) Then
                        'Allow all control characters.
                    ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
                        Dim box As TextBox = DirectCast(sender, TextBox)
                        Dim text As String = box.Text
                        Dim selectionStart As Integer = box.SelectionStart
                        Dim selectionLength As Integer = box.SelectionLength
                        text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)
                
                        Dim valueInt32 As Integer
                        Dim valueDouble As Double
                
                        If Integer.TryParse(text, valueInt32) AndAlso valueInt32 > 999 Then
                            ' Reject an integer that is longer than 3 digits.
                            e.Handled = True
                        ElseIf Double.TryParse(text, valueDouble) AndAlso text.IndexOf("."c) < text.Length - 4 Then
                            ' Reject a real number with two many decimal places.
                            e.Handled = True
                        End If
                    Else
                        ' Reject all other characters.
                        e.Handled = True
                    End If
                End Sub
                

                End Class

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups