0

我正在处理一份工作申请。我们使用 excel 文件来创建报告。我对编码很生疏,真的没有遇到过这么大的要求。该应用程序的工作方式是有标准工具栏。当用户打开应用程序时,他会看到一个空白表单。他们必须首先创建一个新文件并将其保存到他们选择的文件夹中。我使用了流读取器/写入器,但问题是用户必须填写大量数据,所以我的代码很糟糕而且像怪物一样,我想知道有没有更简单的方法?

     ToolStripLabel1.Text = FileDataStorage.OpenFileTextBox1.Text
     If ToolStripLabel1.Text = ("C:\Temp\New QCA.qca") Then
        MsgBox("Must Save New file first ACCESS DENIED!")
        End If
     Dim FILE_NAME As String = FileDataStorage.OpenFileTextBox1.Text
    Try
        If System.IO.File.Exists(FILE_NAME) = True Then

            Dim objWriter As New System.IO.StreamWriter(FILE_NAME)

            objWriter.WriteLine(General_Data.GTextBox1.Text)
            objWriter.WriteLine(General_Data.GTextBox2.Text)
            objWriter.WriteLine(General_Data.GTextBox3.Text)
            objWriter.WriteLine(General_Data.GTextBox4.Text)
            objWriter.WriteLine(General_Data.GTextBox5.Text)
            objWriter.WriteLine(General_Data.GTextBox6.Text)
            objWriter.WriteLine(General_Data.GTextBox7.Text)
            objWriter.WriteLine(General_Data.GTextBox8.Text)
            objWriter.WriteLine(General_Data.GTextBox9.Text)
            objWriter.WriteLine(General_Data.GTextBox10.Text)
            objWriter.WriteLine(General_Data.GTextBox11.Text)
            objWriter.WriteLine(Inventory.ComboBox1.Text)
            objWriter.WriteLine(Inventory.ComboBox2.Text)
            objWriter.WriteLine(Inventory.ComboBox3.Text)
            objWriter.WriteLine(Inventory.ComboBox4.Text)
            objWriter.WriteLine(Inventory.ComboBox5.Text)
            objWriter.WriteLine(Inventory.ComboBox6.Text)
            objWriter.WriteLine(Inventory.ComboBox7.Text)
            objWriter.WriteLine(Inventory.ComboBox8.Text)
            objWriter.WriteLine(Inventory.ComboBox9.Text)
            objWriter.WriteLine(Inventory.ComboBox10.Text)
            objWriter.WriteLine(Inventory.ComboBox11.Text)
            objWriter.WriteLine(Inventory.ComboBox12.Text)
            objWriter.WriteLine(Inventory.ComboBox13.Text)
            objWriter.WriteLine(Inventory.ComboBox14.Text)
            objWriter.WriteLine(Inventory.ComboBox15.Text)
            objWriter.WriteLine(Inventory.ComboBox16.Text)
            objWriter.WriteLine(Inventory.ComboBox17.Text)
            objWriter.WriteLine(Inventory.ComboBox18.Text)
            objWriter.WriteLine(Inventory.ComboBox19.Text)
            objWriter.WriteLine(Inventory.ComboBox20.Text)
            objWriter.WriteLine(Inventory.TextBox1.Text)
            objWriter.WriteLine(Inventory.TextBox2.Text)
            objWriter.WriteLine(Inventory.TextBox3.Text)
            objWriter.WriteLine(Inventory.TextBox4.Text)
            objWriter.WriteLine(Inventory.TextBox5.Text)
            objWriter.WriteLine(Inventory.TextBox6.Text)
            objWriter.WriteLine(Inventory.TextBox7.Text)
            objWriter.WriteLine(Inventory.TextBox8.Text)
            objWriter.WriteLine(Inventory.TextBox9.Text)
            objWriter.WriteLine(Inventory.TextBox10.Text)
            objWriter.WriteLine(Inventory.TextBox11.Text)
            objWriter.WriteLine(Inventory.TextBox12.Text)
            objWriter.WriteLine(Inventory.TextBox13.Text)
            objWriter.WriteLine(Inventory.TextBox14.Text)
            objWriter.WriteLine(Inventory.TextBox15.Text)
            objWriter.WriteLine(Inventory.TextBox16.Text)
            objWriter.WriteLine(Inventory.TextBox17.Text)
            objWriter.WriteLine(Inventory.TextBox18.Text)
            objWriter.WriteLine(Inventory.TextBox19.Text)
            objWriter.WriteLine(Inventory.TextBox20.Text)
            objWriter.WriteLine(Inventory.TextBox21.Text)
            objWriter.WriteLine(Inventory.TextBox22.Text)
            objWriter.WriteLine(Inventory.TextBox23.Text)
            objWriter.WriteLine(Inventory.TextBox24.Text)
            objWriter.WriteLine(Inventory.TextBox25.Text)
            objWriter.WriteLine(Inventory.TextBox26.Text)
            objWriter.WriteLine(Inventory.TextBox27.Text)
            objWriter.WriteLine(Inventory.TextBox28.Text)
            objWriter.WriteLine(Inventory.TextBox29.Text)
            objWriter.WriteLine(Inventory.TextBox30.Text)
            objWriter.WriteLine(Inventory.TextBox31.Text)
            objWriter.WriteLine(Inventory.TextBox32.Text)
            objWriter.WriteLine(Inventory.TextBox33.Text)
            objWriter.WriteLine(Inventory.TextBox34.Text)
            objWriter.WriteLine(Inventory.TextBox35.Text)
            objWriter.WriteLine(Inventory.TextBox36.Text)
            objWriter.WriteLine(Inventory.TextBox37.Text)
            objWriter.WriteLine(Inventory.TextBox38.Text)
            objWriter.WriteLine(Inventory.TextBox39.Text)
            objWriter.WriteLine(Inventory.TextBox40.Text)
            objWriter.WriteLine(Inventory.TextBox41.Text)
            objWriter.WriteLine(Inventory.TextBox42.Text)
            objWriter.WriteLine(Inventory.TextBox43.Text)
            objWriter.WriteLine(Inventory.TextBox44.Text)
            objWriter.WriteLine(Inventory.TextBox45.Text)
            objWriter.WriteLine(Inventory.TextBox46.Text)
            objWriter.WriteLine(Inventory.TextBox47.Text)
            objWriter.WriteLine(Inventory.TextBox48.Text)
            objWriter.WriteLine(Inventory.TextBox49.Text)
            objWriter.WriteLine(Inventory.TextBox50.Text)
            objWriter.WriteLine(Inventory.TextBox51.Text)
            objWriter.WriteLine(Inventory.TextBox52.Text)
            objWriter.WriteLine(Inventory.TextBox53.Text)
            objWriter.WriteLine(Inventory.TextBox54.Text)
            objWriter.WriteLine(Inventory.TextBox55.Text)
            objWriter.WriteLine(Inventory.TextBox56.Text)
            objWriter.WriteLine(Inventory.TextBox57.Text)
            objWriter.WriteLine(Inventory.TextBox58.Text)
            objWriter.WriteLine(Inventory.TextBox59.Text)
            objWriter.WriteLine(Inventory.TextBox60.Text)
            objWriter.WriteLine(Inventory.TextBox61.Text)
            objWriter.WriteLine(Inventory.TextBox62.Text)
            objWriter.WriteLine(Inventory.TextBox63.Text)
            objWriter.WriteLine(Inventory.TextBox64.Text)
            objWriter.WriteLine(Inventory.TextBox65.Text)
            objWriter.WriteLine(Inventory.TextBox66.Text)
            objWriter.WriteLine(Inventory.TextBox67.Text)
            objWriter.WriteLine(Inventory.TextBox68.Text)
            objWriter.WriteLine(Inventory.TextBox69.Text)
            objWriter.WriteLine(Inventory.TextBox70.Text)
            objWriter.WriteLine(Inventory.TextBox71.Text)
            objWriter.WriteLine(Inventory.TextBox72.Text)
            objWriter.WriteLine(Inventory.TextBox73.Text)
            objWriter.WriteLine(Inventory.TextBox74.Text)
            objWriter.WriteLine(Inventory.TextBox75.Text)
            objWriter.WriteLine(Inventory.TextBox76.Text)
            objWriter.WriteLine(Inventory.TextBox77.Text)
            objWriter.WriteLine(Inventory.TextBox78.Text)
            objWriter.WriteLine(Inventory.TextBox79.Text)
            objWriter.WriteLine(Inventory.TextBox80.Text)
            objWriter.WriteLine(Water_QC.ComboBox1.Text)
            objWriter.WriteLine(Water_QC.TextBox1.Text)
            objWriter.WriteLine(Water_QC.TextBox2.Text)
            objWriter.WriteLine(Water_QC.TextBox3.Text)
            objWriter.WriteLine(Water_QC.TextBox4.Text)
            objWriter.WriteLine(Water_QC.TextBox5.Text)
            objWriter.WriteLine(Water_QC.ComboBox2.Text)
            objWriter.WriteLine(Water_QC.TextBox6.Text)
            objWriter.WriteLine(Water_QC.TextBox7.Text)
            objWriter.WriteLine(Water_QC.TextBox8.Text)
            objWriter.WriteLine(Water_QC.TextBox9.Text)
            objWriter.WriteLine(Water_QC.TextBox10.Text)
            objWriter.WriteLine(Water_QC.TextBox11.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox1.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox2.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox3.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox4.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox5.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox6.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox7.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox8.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox9.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox10.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox11.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox12.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox13.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox14.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox15.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox16.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox17.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox18.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox19.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox20.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox21.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox22.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox23.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox24.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox25.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox26.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox27.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox28.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox29.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox30.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox31.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox32.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox33.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox34.Text)
            objWriter.WriteLine(Base_Gel_QC.TextBox35.Text)
            objWriter.WriteLine(Acid_QC.TextBox1.Text)
            objWriter.WriteLine(Acid_QC.TextBox2.Text)
            objWriter.WriteLine(Acid_QC.TextBox3.Text)
            objWriter.WriteLine(Acid_QC.TextBox4.Text)
            objWriter.WriteLine(Acid_QC.TextBox5.Text)
            objWriter.WriteLine(Acid_QC.TextBox6.Text)
            objWriter.WriteLine(Acid_QC.TextBox7.Text)
            objWriter.WriteLine(Acid_QC.TextBox8.Text)
            objWriter.WriteLine(Acid_QC.TextBox9.Text)
            objWriter.WriteLine(Acid_QC.TextBox10.Text)
            objWriter.WriteLine(Acid_QC.TextBox11.Text)
            objWriter.WriteLine(Acid_QC.TextBox12.Text)
            objWriter.WriteLine(Acid_QC.TextBox13.Text)
            objWriter.WriteLine(Acid_QC.TextBox14.Text)
            objWriter.WriteLine(Acid_QC.TextBox15.Text)
            objWriter.WriteLine(Acid_QC.TextBox16.Text)
            objWriter.WriteLine(Acid_QC.TextBox17.Text)
            objWriter.WriteLine(Acid_QC.TextBox18.Text)
            objWriter.WriteLine(Acid_QC.TextBox19.Text)
            objWriter.WriteLine(Acid_QC.TextBox20.Text)
            objWriter.WriteLine(Acid_QC.TextBox21.Text)
            objWriter.WriteLine(Acid_QC.TextBox22.Text)
            objWriter.WriteLine(Acid_QC.TextBox23.Text)
            objWriter.WriteLine(Acid_QC.TextBox24.Text)
            objWriter.WriteLine(Acid_QC.TextBox25.Text)
            objWriter.WriteLine(Acid_QC.TextBox26.Text)
            objWriter.WriteLine(Acid_QC.TextBox27.Text)
            objWriter.WriteLine(Acid_QC.TextBox28.Text)
            objWriter.WriteLine(Acid_QC.TextBox29.Text)
            objWriter.WriteLine(Acid_QC.TextBox30.Text)
            objWriter.WriteLine(Acid_QC.TextBox31.Text)
            objWriter.WriteLine(Acid_QC.TextBox32.Text)
            objWriter.WriteLine(Acid_QC.TextBox33.Text)
            objWriter.WriteLine(Acid_QC.TextBox34.Text)
            objWriter.WriteLine(Acid_QC.TextBox35.Text)
            objWriter.WriteLine(Acid_QC.TextBox36.Text)
            objWriter.WriteLine(Acid_QC.TextBox37.Text)
            objWriter.WriteLine(Acid_QC.TextBox38.Text)
            objWriter.WriteLine(Acid_QC.TextBox39.Text)
            objWriter.WriteLine(Acid_QC.TextBox40.Text)
            objWriter.WriteLine(Acid_QC.TextBox41.Text)
            objWriter.WriteLine(Acid_QC.TextBox42.Text)
            objWriter.WriteLine(Acid_QC.TextBox43.Text)
            objWriter.WriteLine(Acid_QC.TextBox44.Text)
            objWriter.WriteLine(Acid_QC.TextBox45.Text)
            objWriter.WriteLine(Acid_QC.TextBox46.Text)
            objWriter.WriteLine(Acid_QC.TextBox47.Text)
            objWriter.WriteLine(Acid_QC.TextBox48.Text)
            objWriter.WriteLine(Acid_QC.TextBox49.Text)
            objWriter.WriteLine(Acid_QC.TextBox50.Text)
            objWriter.WriteLine(Acid_QC.TextBox51.Text)
            objWriter.WriteLine(Acid_QC.TextBox52.Text)
            objWriter.WriteLine(Acid_QC.TextBox53.Text)
            objWriter.WriteLine(Acid_QC.TextBox54.Text)
            objWriter.WriteLine(Acid_QC.TextBox55.Text)
            objWriter.WriteLine(Acid_QC.TextBox56.Text)
            objWriter.WriteLine(Acid_QC.TextBox57.Text)
            objWriter.WriteLine(Acid_QC.TextBox58.Text)
            objWriter.WriteLine(Acid_QC.TextBox59.Text)
            objWriter.WriteLine(Acid_QC.TextBox60.Text)
            objWriter.WriteLine(Acid_QC.TextBox61.Text)
            objWriter.WriteLine(Acid_QC.TextBox62.Text)
            objWriter.WriteLine(Acid_QC.TextBox63.Text)
            objWriter.WriteLine(Acid_QC.TextBox64.Text)
            objWriter.WriteLine(Acid_QC.TextBox65.Text)



            objWriter.Close()
            MsgBox("Data written to file")

        Else

            'Do Nothing

        End If
    Catch ex As Exception
    End Try
End Sub
4

2 回答 2

1

如果你的意思是让代码更优雅?您可以执行以下操作。

    ToolStripLabel1.Text = FileDataStorage.OpenFileTextBox1.Text

    If ToolStripLabel1.Text = ("C:\Temp\New QCA.qca") Then
        MsgBox("Must Save New file first ACCESS DENIED!")
    End If

    Dim FILE_NAME As String = FileDataStorage.OpenFileTextBox1.Text

    Try
        If System.IO.File.Exists(FILE_NAME) = True Then

            Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
            For Each c As Control In Form.Controls
                If TypeOf c Is TextBox Or TypeOf c Is ComboBox Then
                    objWriter.WriteLine(c.Text)
                End If
            Next

            objWriter.Close()
            MsgBox("Data written to file")

         Else
            'Do Nothing
         End If
    Catch ex As Exception
         Throw
    End Try
于 2013-02-07T20:57:07.420 回答
0

与您已有的保持一致,类似的外部功能可能对您有用。它遍历传递给它的表单,然后找到某种类型的对象,然后将它们打印给您在那里创建的编写器。你必须让它与你已经拥有的东西一起工作,但这样的事情会节省很多行。

它所做的是获取表单上控件的数量,然后遍历每个控件以获取其类型。如果类型正确,那么它只是将其内容写入文件。

Private sub writeToFile(formFrom as Form)
    Dim controls As Control
    For Each controls In formFrom.Controls
        If TypeOf (controls) Is TextBox Then
            objWriter.WriteLine(formFrom.controls.text)
        elseif TypeOf (controls) is ComboBox then
            objWriter.WriteLine(formFrom.controls.text)
        End If
    Next
end sub
于 2013-02-07T20:50:45.903 回答