vb.net - 将行添加到数据源后,当垂直滚动条出现在数据绑定数据网格视图中时程序挂起
问题描述
由于我没有正确提出问题,因此重新发布。
用 VB.NET 4.6 编写的是我的 POS Scan 项目。
它旨在通过 com 端口处理来自扫描仪的扫描。
当扫描某些东西时,事件处理程序(subSerialDataHandler)会调用 subHandleScan,它会根据大小写决定如何处理扫描。
扫描将显示在 dgBarcodes 中。
您还可以通过 btnManuallyAdd 手动将项目添加到 dgBarcodes 的数据源 (dtBarcodes),这将调用相同的 subHandleScan。
现在这是我正在努力解决的问题;
当扫描足够多的项目以使滚动条需要出现在 datagridview 中时,程序会挂起。
通过按钮手动添加项目时,即使需要出现滚动条,程序也不会挂起。
扫描足够少的项目以使滚动条不需要出现,程序不会挂起。
什么可能导致这种情况,我需要做些什么来防止程序挂起?
这是代码
Imports System.IO.Ports
Imports System.Configuration
Imports System.Threading
Imports System.Threading.Tasks
Imports System.Text.RegularExpressions
Imports Microsoft.Web.WebView2.Core
Imports POS_Scan.clsUtilities
Public Class frmPractice
Public Shared frmRefund As New frmRefund 'create an instance of the form
Public Shared frmExchangeIn As New frmExchangeIn 'create an instance of the form
Public Shared frmExchangeOut As New frmExchangeOut 'create an instance of the form
Public Delegate Sub dlgRefund(strBarcode As String) 'Delegates for other form access
Public Delegate Sub dlgExchangeIn(strBarcode As String) 'Delegates for other form access
Public Delegate Sub dlgExchangeOut(strBarcode As String) 'Delegates for other form access
Dim mySerialPort() As SerialPort
Dim dtBarcodes As New DataTable
Public Shared strAction As String = "Sale" 'Semaphore to dictate how to process a barcode scan event.
Private Sub frmPractice_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dtBarcodes.Columns.Add("dScan", Type.GetType("System.String")) 'create a column with a string input
dtBarcodes.Columns.Add("strBarcode", Type.GetType("System.String")) 'create a column with a string input
dtBarcodes.Columns.Add("strMethod", Type.GetType("System.String")) 'create a column with a string input
dtBarcodes.Columns.Add("strAmount", Type.GetType("System.String")) 'create a column with a string input
subSerialListener()
dgBarcodes.DataSource = dtBarcodes.DefaultView 'bind it
End Sub
Private Sub subSerialListener()
Dim strPorts() As String
strPorts = fnGetSerialPorts() 'get all serial ports of this computer
Dim intNumberOfPorts As Integer = strPorts.Count - 1
ReDim mySerialPort(intNumberOfPorts) 'create a 0 based array of serial ports with the right size
For i = 0 To intNumberOfPorts
mySerialPort(i) = New SerialPort(strPorts(i))
mySerialPort(i).BaudRate = 9600
mySerialPort(i).Parity = Parity.None
mySerialPort(i).StopBits = StopBits.One
mySerialPort(i).DataBits = 8
mySerialPort(i).Handshake = Handshake.None
mySerialPort(i).RtsEnable = True
AddHandler mySerialPort(i).DataReceived, AddressOf subSerialDataHandler
mySerialPort(i).Open()
'mySerialPort(i).Close()
Next i
End Sub
Private Function fnGetSerialPorts() As String()
Dim strPorts As String() = SerialPort.GetPortNames() 'get an array of port names of this computer
Return strPorts
End Function
Private Sub subSerialDataHandler(sender As Object, e As SerialDataReceivedEventArgs)
Dim sp As SerialPort = CType(sender, SerialPort) 'convert the sender into a serialport
Dim strInData As String = sp.ReadExisting() 'take the data in the serialport and put it in a string
subHandleScan(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.ff"), strInData, "Scan") 'pass it to the scan
End Sub
Private Sub subHandleScan(strDScan As String, strBarcode As String, strMethod As String)
Dim strNow As String = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
Select Case strAction
Case "Sale" 'if the scan was a sale
subWriteDebugLog(strNow, strMethod, strBarcode) 'write debuglog for a Sale scan
Dim row1 As DataRow = dtBarcodes.NewRow() 'make a new row
row1("dScan") = strDScan 'set the date
row1("strBarcode") = strBarcode 'set the barcode
row1("strMethod") = strMethod 'set the method
dtBarcodes.Rows.Add(row1) 'add the row
Case "Refund" 'if the scan was a refund
subWriteDebugLog(strNow, "RefundScan", strBarcode) 'write debuglog for a Refund scan
If frmRefund.txtReturnedBarcode.InvokeRequired Then 'Calling from another thread
Dim dlgRefund As New dlgRefund(AddressOf frmRefund.subScan) 'sets frmRefund.txtReturnedBarcode.Text =
strBarcode
Me.Invoke(dlgRefund, strBarcode) 'Execute delegate in the UI thread, pass args as an array
End If
Case "ExchangeIn"
subWriteDebugLog(strNow, "ExchangeInScan", strBarcode) 'write debuglog for a ExchangeIn scan
If frmExchangeIn.txtReturnedBarcode.InvokeRequired Then 'Calling from another thread
Dim dlgExchangeIn As New dlgExchangeIn(AddressOf frmExchangeIn.subScan) 'sets
frmExchangeIn.txtReturnedBarcode.Text = strBarcode
Me.Invoke(dlgExchangeIn, strBarcode) 'Execute delegate in the UI thread, pass args as an array
End If
Case "ExchangeOut"
subWriteDebugLog(strNow, "ExchangeOutScan", strBarcode) 'write debuglog for a ExchangeOut scan
If frmExchangeOut.txtExchangedBarcode.InvokeRequired Then 'Calling from
another thread
Dim dlgExhangeOut As New dlgExchangeOut(AddressOf frmExchangeOut.subScan) 'sets
frmExchangeOut.txtExchangedBarcode.Text = strBarcode
Me.Invoke(dlgExhangeOut, strBarcode) 'Execute delegate in the UI thread, pass args as an array
End If
End Select
End Sub
Private Sub btnManuallyAdd_Click(sender As Object, e As EventArgs) Handles btnManuallyAdd.Click
Dim strBarcode As String = Trim(txtManualBarcode.Text)
If strBarcode <> "" Then 'if the clean string pulled from the textbox isn't empty
Dim dateTrans As DateTime = Now
subHandleScan(dateTrans.ToString("yyyy/MM/dd HH:mm:ss.ff"), strBarcode, "Manual") 'handle the scan
subWriteDebugLog(dateTrans.ToString("yyyy/MM/dd HH:mm:ss"), "btnManuallyAdd", strBarcode) 'write to the debug log
Me.txtManualBarcode.Text = "" 'clear the textbox
End If
End Sub
Private Sub btnExchange_Click(sender As Object, e As EventArgs) Handles btnExchange.Click
frmExchangeIn.frmExchangeOut = frmExchangeOut 'pass the instance to the child
strAction = "ExchangeIn" 'set the mode
subWriteDebugLog(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), "btnExchange", "") 'write to the debug log
frmExchangeIn.txtReturnedBarcode.Text = "" 'clear the control
frmExchangeIn.ShowDialog(Me) 'show it
If frmExchangeIn.DialogResult = Windows.Forms.DialogResult.OK Then 'if the child form saved something
Dim strReturnedBarcode As String = frmExchangeIn.strReturnedBarcode 'get the barcode
Dim strExchangedBarcode As String = frmExchangeIn.strExchangedBarcode 'get the barcode
Dim row1 As DataRow = dtBarcodes.NewRow() 'make a new row
Dim row2 As DataRow = dtBarcodes.NewRow() 'make a new row
Dim dateNow As DateTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.ff") 'grab the time so its the exact same for both rows
'new item
row1("dScan") = dateNow 'set the date
row1("strBarcode") = strExchangedBarcode 'set the barcode
row1("strMethod") = "-Exchange" 'set the method
'Returned item
row2("dScan") = dateNow 'set the date
row2("strBarcode") = strReturnedBarcode 'set the barcode
row2("strMethod") = "+Exchange" 'set the method
dtBarcodes.Rows.Add(row1) 'add the row
dtBarcodes.Rows.Add(row2) 'add the row
End If
strAction = "Sale" 'reset the mode
End Sub
Private Sub btnRefund_Click(sender As Object, e As EventArgs) Handles btnRefund.Click
strAction = "Refund" 'set the mode
subWriteDebugLog(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), "btnRefund", "") 'write to the debug log
frmRefund.txtDate.Text = "" 'clear the control
frmRefund.txtTime.Text = "" 'clear the control
frmRefund.txtTotalValue.Text = "" 'clear the control
frmRefund.txtReturnedBarcode.Text = "" 'clear the control
frmRefund.txtReturnedValue.Text = "" 'clear the control
frmRefund.ShowDialog(Me) 'show it
If frmRefund.DialogResult = Windows.Forms.DialogResult.OK Then 'if the child form saved something
Dim strReturnedBarcode As String = frmRefund.strReturnedBarcode 'get the barcode
Dim decValue As Decimal = frmRefund.decValue 'get the value
Dim row As DataRow = dtBarcodes.NewRow() 'make a new row
row("dScan") = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.ff") 'set the date
row("strBarcode") = strReturnedBarcode 'set the barcode
row("strMethod") = "Refund" 'set the method
row("strAmount") = "-" & CStr(decValue) 'set the amount
dtBarcodes.Rows.Add(row) 'add the row
End If
strAction = "Sale" 'reset the mode
End Sub
解决方案
推荐阅读
- javascript - 输入文本在输入字段中占用额外空间
- azure - 无法从私有 github 存储库 URI 下载部署内容
- c# - 我需要安装 microsoft sql server 还是默认安装在 Visual Studio Community 2017 中?
- mysql - SQL中两个表的交集
- python - 在 Python3 中,如何将“help()”函数的输出输入到变量中?
- javascript - 如何解决 req.flash 不是节点 js 中的函数
- c# - 在 C# 中调用非静态方法有更短的方法吗?
- arrays - 如何通过 Unity SDK 将数组存储到 Firebase
- jenkins - 具有自定义功能的 Jenkins 仪表板报告
- php - 在centos 7上安装phpmyadmin的冲突错误