0

我的代码有问题;并希望能得到一些帮助,让她做我想做的事,而不会出现烦人的错误。读取数据绑定列表框字符串,找到用户选择的字符串并将所述字符串写入文件。

    Private Sub btnContinue_Click(sender As System.Object, e
    As System.EventArgs) Handles btnContinue.Click
    '  1st file and mySQL Update to testing table
    If txtLocation.Text.ToString <> "" And txtUnitTested.Text.ToString <> "" Then 
        Dim filewriter As New System.IO.StreamWriter(
               "C:\Users\OER\Documents\Visual Studio 2010\Projects\frmTest1_0.txt")
        Dim curItem = lstCustomer.SelectedItem
        Dim now As DateTime = DateTime.Now
        With filewriter
            .WriteLine(vbCrLf + (now.ToString("F"))) ' Write the DateTime to the file
        End With

        For Each objDataRowView As DataRowView In lstCustomer.SelectedItems
            Dim item As String
            For Each item As DataRowView In Me.lstCustomer.Items.Item("customer")
                item = String.Parse(lstCustomer.SelectedValue)
                WriteLine(curItem(item))
            Next item
        Next '  THIS IS THE FOR LOOP I AM HAVING ISSUES WITH!!!!!!!

        With filewriter
            .WriteLine(vbCrLf + txtLocation.Text + vbCrLf + txtUnitTested.Text)
        End With
        filewriter.Close()
        frmTest1.Show()
    Else
        MsgBox("Please Type the Location and Unit Tested!!!", vbCritical)
        txtLocation.Clear()
        txtUnitTested.Clear()
        txtLocation.Focus()
    End If
4

1 回答 1

1

这些循环:

For Each objDataRowView As DataRowView In lstCustomer.SelectedItems
     Dim item As String
     For Each item As DataRowView In Me.lstCustomer.Items.Item("customer")
         item = String.Parse(lstCustomer.SelectedValue)
             WriteLine(curItem(item))
     Next item
Next 

会给你带来一些问题。您已经声明了一个与另一个变量同名的循环变量。然后你给它赋值。

另外,我不太清楚这两个循环的目的是什么。我对您在这部分代码中要完成的工作感到有些困惑。我将从重命名这一行中的变量开始:

Dim item as String

我将以此为例:

Dim result as string

做别的东西,然后你不能在循环中更改 item 的值(因为那是循环变量)。这将导致错误。

将循环内的代码更改为:

result = String.Parse(lstCustomer.SelectedValue)

很可能这仍然无法让您到达您想要去的地方,但这是一个好的开始。除非您的 lstCustomer 中的每个项目都包含多个要迭代的项目,否则我认为您可能只需要第一个外部循环。

如果你的循环是这样开始的:

For Each objDataRowView As DataRowView In lstCustomer.SelectedItems

然后我想你会想要:

objDataRowView.Item("customer")

作为输出。我认为这将是您表中客户字段的值。

您基本上是在说这一行有一个名为 customer.... 的列。我想要那个值。

于 2013-02-27T21:09:05.363 回答