Masking TextBox and DataGridView cell

DRGAGI 146 Reputation points
2020-12-13T20:15:40.553+00:00

Hello, i have textbox which contains currency, i am trying to add mask to show currency when some value is entered. Image show sample of what i47644-snap3.jpg need, and also need to be aligned to the right side of the textbox. Same is need it with DataGridView cells in entire column. Thank you.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,644 questions
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2020-12-13T21:40:12.677+00:00

    Hi
    See if this stand alone example helps.

    ' Blank FORM1 
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim lab As New Label
        Dim dgv As New DataGridView
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            With dgv
                .Location = New Point(130, 40)
                .Width = 100
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
                .Columns.Add("Column1", "Column1")
    
                With .Columns("Column1")
                    .DefaultCellStyle.Format = "0.00 Euro"
                    .Width = 65
                End With
                .RowHeadersVisible = False
                .Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top
                For i As Integer = 11 To 15
                    .Rows.Add(i / 3)
                Next
            End With
            ' ==========================
            ' this code block is ONLY for
            ' this example - you have your
            ' controls already on the Form
            Dim y As Integer = 40
            For i As Integer = 1 To 6
                Dim tb As New TextBox
                With tb
                    .Location = New Point(20, y)
                    .Width = 100
                    .TextAlign = HorizontalAlignment.Right
                    Controls.Add(tb)
                    AddHandler tb.TextChanged, AddressOf TextBox_TextChanged
                    AddHandler tb.Validated, AddressOf TextBox_Validated
                    y += tb.Height + 4
                End With
            Next
            With lab
                .Location = New Point(20, 5)
                .AutoSize = True
                .Font = New Font(.Font.FontFamily, 20)
            End With
            Controls.AddRange({lab, dgv})
            ' ==========================
        End Sub
    
        ' you could probably use thse two 
        ' Subs as a possible answer to
        ' your question
        Private Sub TextBox_TextChanged(sender As Object, e As EventArgs)
            Dim total As Double = 0.0
            For Each c As Control In Controls
                If c.GetType = GetType(TextBox) Then
                    total += GetDouble(c.Text)
                End If
            Next
            lab.Text = total.ToString("0.00")
        End Sub
        Private Sub TextBox_Validated(sender As Object, e As EventArgs)
            Dim tb As TextBox = DirectCast(sender, TextBox)
            If Not tb.Text.EndsWith("Euro") Then tb.Text &= " Euro"
            dgv.Rows.Add(GetDouble(tb.Text))
        End Sub
        Function GetDouble(s As String) As Double
            If s.EndsWith(" Euro") Then s = s.Substring(0, s.Length - 5)
            Dim v As Double = 0.0
            If Double.TryParse(s, v) Then Return v
            Return 0.0
        End Function
    End Class
    
    1 person found this answer helpful.

4 additional answers

Sort by: Most helpful
  1. DRGAGI 146 Reputation points
    2020-12-13T22:54:39.71+00:00
    ' This do the comma, but after adding a number and if user want to change it i get error, so i need to clear textbox to be able to change the value 
    
    
    Dim n As Double = TextBox74.Text
    
            TextBox74.Text = n.ToString("##,##,###.00")
    
    0 comments No comments

  2. Karen Payne MVP 35,366 Reputation points
    2020-12-14T01:17:00.4+00:00

    Hello @DRGAGI

    I've edited this post as there were issues. In this revised version the DataGridView columns are created in the designer, DataPropertyName set for each column.

    Screenshot

    47812-b1.png

    • A BindingSource is the data source using a DataTable for both the DataGridView and the TextBox
    • Note how data binding is done for the TextBox using the Binding class which permits formatting in an event.
    • The following Value1Column.DefaultCellStyle.Format = "###.##\ Euro" does the DataGridView formatting
    • EditingControlShowing removed Euro but on exit places Euro back in

      Full source

      Public Class Form1
      Private ReadOnly _bindingSource As New BindingSource
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
          Dim dt As New DataTable  
      
          dt.Columns.Add(New DataColumn() With {.ColumnName = "Value1", .DataType = GetType(Decimal)})  
          dt.Columns.Add(New DataColumn() With {.ColumnName = "Value2", .DataType = GetType(Decimal)})  
      
          dt.Rows.Add(New Object() {12.99D, 12.99D})  
          dt.Rows.Add(New Object() {1112D, 1112D})  
          dt.Rows.Add(New Object() {99.87D, 99.87D})  
      
          _bindingSource.DataSource = dt  
      
          Dim b As New Binding("Text", _bindingSource, "Value1") With {  
              .DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged  
          }  
      
          AddHandler b.Format, AddressOf FormatValue1  
      
          Value1TextBox.DataBindings.Add(b)  
      
          Value1Column.DefaultCellStyle.Format = "###.##\ Euro"  
          DataGridView1.DataSource = _bindingSource  
      
      End Sub  
      
      Private Sub FormatValue1(sender As Object, e As ConvertEventArgs)  
          Dim value As Decimal = 0  
          If Decimal.TryParse(e.Value.ToString(), value) Then  
              e.Value = $"{e.Value} Euro"  
          End If  
      End Sub  
      Private Sub DataGridView1_EditingControlShowing(  
          sender As Object, e As DataGridViewEditingControlShowingEventArgs) _  
          Handles DataGridView1.EditingControlShowing  
      
          If DataGridView1.CurrentCell.ColumnIndex = 0 Then  
              e.CellStyle.Format = "N2"  
              e.Control.Text = DataGridView1.CurrentCell.Value.ToString()  
          End If  
      
      End Sub  
      
      End Class
    0 comments No comments

  3. DRGAGI 146 Reputation points
    2020-12-14T18:38:54.52+00:00

    I found easy way formatting DataGridView cells via DataGridView Tasks in designer window and i got this as result48006-3.jpg48007-1.jpg


  4. DRGAGI 146 Reputation points
    2020-12-14T19:25:59.183+00:00
    Private Sub Button25_Click(sender As Object, e As EventArgs) Handles Button25.Click
    
            TextBox73.Text = Val(TextBox49.Text) + Val(TextBox50.Text) + Val(TextBox51.Text) + Val(TextBox52.Text) + Val(TextBox53.Text) + Val(TextBox54.Text) + Val(TextBox55.Text) + Val(TextBox56.Text) + Val(TextBox57.Text) + Val(TextBox58.Text) + Val(TextBox59.Text) + Val(TextBox60.Text) + Val(TextBox61.Text) + Val(TextBox62.Text) + Val(TextBox63.Text) + Val(TextBox64.Text) + Val(TextBox65.Text) + Val(TextBox66.Text) + Val(TextBox67.Text) + Val(TextBox68.Text) + Val(TextBox69.Text) + Val(TextBox70.Text) + Val(TextBox71.Text) + Val(TextBox72.Text)
    
            Dim n As Double = TextBox73.Text
    
            TextBox73.Text = n.ToString("##,##,###.00")
            TextBox73.TextAlign = HorizontalAlignment.Right
            If Not TextBox73.Text.EndsWith("Euro") Then TextBox73.Text &= " Euro"
    
        End Sub