Dear Sir,
I'm Trying to format a textbox & label to be used as a payment and change limit it with numbers with regex in VB.NET
So I want to input numbers as usual without entering commas from the string format and also limit to only numbers and also the labelchange will also be reduced according to the value I input in TextBoxPayment. When I do the input, the number format remains unchanged
Please Guide Me.
Link database ms access
Link Result code 04-07-2024-02
Desired Result first option
For invono : 1000
Total Value 1,121,000.00
Total Payment 1,121,000.00
Change 0
Desired Result second option
For invono : 1000
Total Value 1,121,000
Total Payment 1,121,000
Change 0
Code in form1
Imports System.ComponentModel
Imports System.Data.OleDb
Imports Dapper
Public Class Form1
Private bindingSource As BindingSource = Nothing
Private salesservices As New SalesService()
Private Sub CalculateTotalValue()
Dim tot As Double = 0
For Each item As DataGridViewRow In DataGridView1.Rows
Dim val As Double
Dim val2 As Double
Dim val3 As Double
Double.TryParse(CType(item.Cells("Qty").Value, String), val)
Double.TryParse(CType(item.Cells("Price").Value, String), val2)
Double.TryParse(CType(item.Cells("Discount").Value, String), val3)
tot += (val * val2) - (val3)
Next item
lblValue.Text = tot.ToString("N2")
'lblChange.Text = (cash - tot).ToString("N2")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Txtinvono.Text = "1000"
DataGridView1.AllowUserToAddRows = False
bindingSource = New BindingSource With {.DataSource = New BindingList(Of Salesdetail)(CType(salesservices.GetSalesdetail(Txtinvono.Text), IList(Of Salesdetail)))}
DataGridView1.DataSource = bindingSource
DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
CalculateTotalValue()
End Sub
Private Sub BtnPayment_Click(sender As Object, e As EventArgs) Handles BtnPayment.Click
Using frm = New Payment()
frm.LblInvono.Text = Me.Txtinvono.Text
If frm.ShowDialog() = DialogResult.OK Then
End If
End Using
End Sub
End Class
Public Class Sales
Public Property Invno() As String
Public Property DateInvno() As Date
Public Property Totalinv() As Integer
Public Property Paymentofterm() As String
Public Property CashPayment() As Integer
Public Property NCashPayment() As Integer
Public Property Sales() As New List(Of Salesdetail)()
End Class
Public Class Salesdetail
Public Property No() As Integer
Public Property Invno() As String
Public Property CodeProduct() As String
Public Property Qty() As Integer
Public Property Price() As Integer
Public Property Discount() As Integer
Public Property Total() As Integer
End Class
Public Class SalesService
Public Function GetOledbConnectionString() As String
Return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Sales.accdb;Persist Security Info=False;"
End Function
Private ReadOnly _conn As OleDbConnection
Private _connectionString As String = GetOledbConnectionString()
Public Sub New()
_conn = New OleDbConnection(_connectionString)
End Sub
Public Function GetSalesdetail(ByVal Invno As String) As IEnumerable(Of Salesdetail)
Dim sql = $"SELECT * FROM Salesdetail WHERE Invno = '{Invno}'"
Using _conn = New OleDbConnection(GetOledbConnectionString())
Return _conn.Query(Of Salesdetail)(sql).ToList()
End Using
End Function
Public Function GetSales(ByVal Invno As String) As Sales
Dim sql = $"SELECT * FROM Sales WHERE Invno = '{Invno}'"
Using _conn = New OleDbConnection(GetOledbConnectionString())
Return _conn.Query(Of Sales)(sql).FirstOrDefault()
End Using
End Function
End Class
Code in Payment
Imports System.Text.RegularExpressions
Public Class Payment
Private salesservices As New SalesService()
Private Sub Payment_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim sales = salesservices.GetSales(LblInvono.Text)
LabelTotalvalue.Text = sales.Totalinv.ToString("N")
txtPayment.Text = (sales.CashPayment + sales.NCashPayment).ToString("N")
LabelChange.Text = ((sales.CashPayment + sales.NCashPayment) - sales.Totalinv).ToString("N")
End Sub
Private Function KeyEnteredIsValid(ByVal key As String) As Boolean
Dim regex As Regex
regex = New Regex("[^0-9]") 'regex that matches disallowed text
Return regex.IsMatch(key)
End Function
Private Sub txtPayment_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtPayment.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
If Not IsNumeric(txtPayment.Text) Then
txtPayment.Text = "0"
ElseIf Val(txtPayment.Text) <= 0 Then
LabelChange.Text = "0"
Else
LabelChange.Text = CType(Val(txtPayment.Text) - Val(LabelTotalvalue.Text), String)
End If
End If
End Sub
Private Sub txtPayment_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPayment.TextChanged
If IsNumeric(txtPayment.Text) Then
LabelChange.Text = CType(Val(txtPayment.Text) - Val(LabelTotalvalue.Text), String)
ElseIf Val(txtPayment.Text) <= 0 Then
LabelChange.Text = CType(Val(txtPayment.Text) - Val(LabelTotalvalue.Text), String)
End If
End Sub
End Class