在后台工作者中更新TextBox

时间:2017-06-04 11:42:48

标签: c# vb.net visual-studio

无法在后台工作程序中更新文本框值, 要更新的值是在后台worker中创建的数据表的列(“TaxableValue”)的总和。 未提示错误消息

开启按钮点击以下事件

capture.check(('root', 'INFO', StringComparison(r'(.*\n)*.*test exception.*'))

我创建了一个计算TAX的函数。

Private Sub btnCalcTax_Click(sender As Object, e As EventArgs) Handles btnCalcTax.Click        
        BackgroundWorker1.RunWorkerAsync()
    End Sub

我无法将列的总和(“TaxableValue”)更新为Textbox1.text

BackgroundWorker_DoWork的代码是

Public Sub TAXCALC(itemname As String, rackrate As Double, discountedrate As Double, billdate As Date)
    Dim finalfdtaxcalcdt As New DataTable
    finalfdtaxcalcdt.Columns.Clear()
    finalfdtaxcalcdt.Rows.Clear()
    Dim outlettyp As String = "POS"
    Dim taxcount As New DataTable
    Dim tax As New DataTable
    If finalfdtaxcalcdt.Columns.Count = 0 Then
        finalfdtaxcalcdt.Columns.Add("TaxID")
        finalfdtaxcalcdt.Columns.Add("TaxName")
        finalfdtaxcalcdt.Columns.Add("TaxableValue")
        finalfdtaxcalcdt.Columns.Add("TaxRate")
        finalfdtaxcalcdt.Columns.Add("SalesAmount")
        finalfdtaxcalcdt.Columns.Add("TaxAmount")
        finalfdtaxcalcdt.Columns.Add("SalesID")
        finalfdtaxcalcdt.Columns.Add("PurchaseID")
        finalfdtaxcalcdt.Columns.Add("TaxClassID")
    End If

    For i = 0 To BodyGridView.RowCount - 1
        If IsDBNull(BodyGridView.GetRowCellValue(i, "Particulars")) Then
            Exit Sub
        End If
        If BodyGridView.GetRowCellValue(i, "Particulars") <> "# End of List" Then
            Dim tempdt As New DataTable

            If isinterstate Then
                GetDataSetFromSqlToDataTable("Select * from MstTax where TaxClassId = (Select Distinct(TaxClassid) from mstitemTaxClass where ApplyFrom = (SELECT MAX(ApplyFrom) FROM dbo.MstItemTaxClass WHERE ApplyFrom <= '" + billdate.ToString("yyyy-MM-dd") + "' and ItemId = '" + GetDataFromSql("Select ITEM_ID from MStItem where Item = '" + BodyGridView.GetRowCellValue(i, "Particulars") + "'", con) + "')) and IsInterState = 'TRUE'", tempdt)
                'GetDataSetFromSqlToDataTable("Select * from MstTax where TaxClassId = (Select TaxClass_Id from MstItem where Item = '" + BodyGridView.GetRowCellValue(i, "Particulars").ToString + "') and IsInterState = 'True'", tempdt)
            Else
                GetDataSetFromSqlToDataTable("Select * from MstTax where TaxClassId = (Select Distinct(TaxClassid) from mstitemTaxClass where ApplyFrom = (SELECT MAX(ApplyFrom) FROM dbo.MstItemTaxClass WHERE ApplyFrom <= '" + billdate.ToString("yyyy-MM-dd") + "' and ItemId = '" + GetDataFromSql("Select ITEM_ID from MStItem where Item = '" + BodyGridView.GetRowCellValue(i, "Particulars") + "'", con) + "')) and IsInterState = 'False'", tempdt)
                'GetDataSetFromSqlToDataTable("Select * from MstTax where TaxClassId = (Select TaxClass_Id from MstItem where Item = '" + BodyGridView.GetRowCellValue(i, "Particulars").ToString + "') and IsInterState = 'False'", tempdt)
            End If

            Dim sum As Double = tempdt.Compute("SUM(TaxRate)", String.Empty)

            For j = 0 To tempdt.Rows.Count - 1
                If finalfdtaxcalcdt.Select("TaxID = '" + tempdt.Rows(j).Item(0).ToString + "'").Count = 0 Then
                    finalfdtaxcalcdt.Rows.Add()
                    finalfdtaxcalcdt.Rows(finalfdtaxcalcdt.Rows.Count - 1).Item("TaxID") = tempdt.Rows(j).Item("Tax_ID").ToString
                    finalfdtaxcalcdt.Rows(finalfdtaxcalcdt.Rows.Count - 1).Item("TaxName") = tempdt.Rows(j).Item("TaxName").ToString
                    finalfdtaxcalcdt.Rows(finalfdtaxcalcdt.Rows.Count - 1).Item("TaxRate") = tempdt.Rows(j).Item("TaxRate").ToString
                    finalfdtaxcalcdt.Rows(finalfdtaxcalcdt.Rows.Count - 1).Item("SalesAmount") = Double.Parse(BodyGridView.GetRowCellValue(i, "Amount"))
                    finalfdtaxcalcdt.Rows(finalfdtaxcalcdt.Rows.Count - 1).Item("TaxAmount") = Math.Round((Double.Parse(finalfdtaxcalcdt.Rows(finalfdtaxcalcdt.Rows.Count - 1).Item("SalesAmount")) / (1 + (Double.Parse(sum) * 0.01))) * (Double.Parse(finalfdtaxcalcdt.Rows(finalfdtaxcalcdt.Rows.Count - 1).Item("TaxRate")) * 0.01), 2)
                    finalfdtaxcalcdt.Rows(finalfdtaxcalcdt.Rows.Count - 1).Item("TaxableValue") = Math.Round(Double.Parse(finalfdtaxcalcdt.Rows(finalfdtaxcalcdt.Rows.Count - 1).Item("SalesAmount")) / (1 + (sum * 0.01)), 2)
                    finalfdtaxcalcdt.Rows(finalfdtaxcalcdt.Rows.Count - 1).Item("SalesID") = tempdt.Rows(j).Item("SalesAc_ID").ToString
                    finalfdtaxcalcdt.Rows(finalfdtaxcalcdt.Rows.Count - 1).Item("TaxClassID") = tempdt.Rows(j).Item("TaxClassId").ToString
                    'finalfdtaxcalcdt.Rows(finalfdtaxcalcdt.Rows.Count - 1).Item("PurchaseID") = tempdt.Rows(j).Item("TaxRate").ToString
                Else
                    Dim index As Integer = finalfdtaxcalcdt.Rows.IndexOf(finalfdtaxcalcdt.Select("TaxId = '" + tempdt.Rows(j).Item(0).ToString + "'").FirstOrDefault())
                    finalfdtaxcalcdt.Rows(index).Item("SalesAmount") = Double.Parse(finalfdtaxcalcdt.Rows(index).Item("SalesAmount")) + Double.Parse(BodyGridView.GetRowCellValue(i, "Amount"))
                    finalfdtaxcalcdt.Rows(index).Item("TaxAmount") = Math.Round((Double.Parse(finalfdtaxcalcdt.Rows(index).Item("SalesAmount")) / (1 + (Double.Parse(sum) * 0.01))) * (Double.Parse(finalfdtaxcalcdt.Rows(index).Item("TaxRate")) * 0.01), 2)
                    finalfdtaxcalcdt.Rows(index).Item("TaxableValue") = Math.Round(Double.Parse(finalfdtaxcalcdt.Rows(index).Item("SalesAmount")) / (1 + (sum * 0.01)), 2)
                End If
            Next
        End If
    Next
    '        MsgBox(finalfdtaxcalcdt.Compute("Sum(TaxAmount)", String.Empty).ToString + "1")
    '        TaxGridControl.DataSource = finalfdtaxcalcdt
    ''       TaxGridView.Columns("TaxID").Visible = False
    '     TaxGridView.Columns("SalesAmount").Visible = False
    '    TaxGridView.Columns("SalesID").Visible = False
    '   TaxGridView.Columns("PurchaseID").Visible = False
    '  TaxGridView.Columns("TaxClassID").Visible = False
    Dim k As String = finalfdtaxcalcdt.Compute("Sum(TaxAmount)", String.Empty)
    TextBox1.Invoke(DirectCast(Sub() TextBox1.Text = k.ToString, MethodInvoker))
End Sub

4 个答案:

答案 0 :(得分:1)

您需要使用委托。

MSDN Control.Invoke Method (Delegate)

示例:

Public Delegate Sub _TAXCALC(itemname As String, rackrate As Double, discountedrate As Double, billdate As Date)
Public Sub TAXCALC(itemname As String, rackrate As Double, discountedrate As Double, billdate As Date)
    If BodyGridView.InvokeRequired Then
        BodyGridView.Invoke(New _TAXCALC(AddressOf TAXCALC), itemname, rackrate, discountedrate, billdate)
    Else
        'Your code here'
    End If
End Sub

希望有所帮助。

答案 1 :(得分:0)

您无法在后台工作线程的上下文中访问表单对象,因为两个对象都在不同线程的上下文中运行(即使它们可能在同一进程的上下文中运行)。

来自后台工作者的数据需要在您的课程中缓冲。获取此数据并将其传递给控件的例程必须在创建表单的过程的上下文中运行。

快速而简单的方法是创建

  • 包含来自后台工作人员的结果数据的缓冲变量
  • 一个布尔变量,指示数据已准备好被提取(可在RunWorkerCompleted事件上设置)
  • 表单上的更新例程(例如计时器),用于检查指示符变量并根据需要更新控件
  • 获取缓冲数据,重置指标和清除缓冲区的获取例程

注意:更新例程应尽可能简短,因为它将同步运行,从而阻止表单。对于更复杂的控件更新,请考虑研究BeginUpdate,SuspendLayout,ResumeLayout,EndUpdate和常见的windows.forms.control更新行为。

答案 2 :(得分:0)

除非我遗漏了某些内容,否则你应该从taxCalc方法返回值(显然首先转换为函数),在do work方法中设置e.result并在RunWorkerCompleted中获取结果。

如有疑问请阅读msdn

  

如果您的操作产生结果,则可以将结果分配给DoWorkEventArgs.Result属性。这将在RunWorkerCompletedEventArgs.Result属性中的RunWorkerCompleted事件处理程序中可用。

答案 3 :(得分:0)

在致电(更新)

之前添加此代码
 Control.CheckForIllegalCrossThreadCalls = False

希望能帮到你。