从选定的组合框项目中获取数组信息。

时间:2016-04-26 03:50:36

标签: arrays vb.net combobox

我有一个简单的问题。我需要做的就是从选定的组合框项中获取数组信息。我知道关于combobox.selectedindex但这不是我的问题。我需要检索数组信息,所以一旦我选择学生名称,它将显示他们的原始成绩和弯曲的字母等级。我已经完成了数学计算,除了这个,我已经完成了所有工作。这是我的代码。

      Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click

    Dim Line_String As String

    Dim ArraySize_Integer As Integer = 0
    Dim Grades_StreamReader As IO.StreamReader
    Dim i As Integer = 0
    Dim GradesFile_String As String = "Grades.txt" 'To hold the filename for opening
    Dim DialogResponse As DialogResult
    Dim x_Integer As Integer
    Dim Mean_Decimal As Double = 0
    Dim Total_Integer As Double = 0
    Dim sum As Double
    Dim stDev As Double
    Dim x As Integer
    Dim y As Integer
    Dim orgScore As Integer


    'Prompt to open the file

    'Set the initial folder to display
    OpenFileDialog1.InitialDirectory = IO.Directory.GetCurrentDirectory


    'Open the file
    With OpenFileDialog1
        'Begin in the current folder
        .InitialDirectory = Directory.GetCurrentDirectory()
        .FileName = "Grades.txt"
        .Title = "Select File or Directory for File"
        'Filter to show only .txt files
        OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
        'Display the open file dialog box
        DialogResponse = .ShowDialog()
    End With
    'Get the filename
    GradesFile_String = OpenFileDialog1.FileName

    'Connect to the file
    Grades_StreamReader = IO.File.OpenText(GradesFile_String)
    'Read the file into a structure array until it runs out of data
    Do Until Grades_StreamReader.Peek = -1
        'Raed the data
        Line_String = Grades_StreamReader.ReadLine
        'Split the line into the fields
        StringArray_String = Line_String.Split(","c)
        'Dynamically size the array of structures
        ReDim Preserve StudentData_Person(ArraySize_Integer)
        'Assign the fields to the structure, trimming the space
        StudentData_Person(ArraySize_Integer).Name_String = StringArray_String(0).Trim
        StudentData_Person(ArraySize_Integer).Grade_String = StringArray_String(1).Trim
        'Increment for the next array element
        ArraySize_Integer += 1
        'Populate the combo box
        Students_ComboBox.Items.Add(StringArray_String(0))

    Loop
    'Close the file
    Grades_StreamReader.Close()

    'Count the scores
    'Convert from String to Integer
    Scores_TextBox.Text = ArraySize_Integer.ToString



    'Calculate the mean

    If StudentData_Person.Length > 0 Then
        For x_Integer = 0 To StudentData_Person.Length - 1
            Total_Integer += CInt(StudentData_Person(x_Integer).Grade_String)
        Next
        Mean_Decimal = Total_Integer / StudentData_Person.Length
    End If
    Mean_TextBox.Text = Mean_Decimal.ToString

    'Calculate the Standard Deviation

    If StudentData_Person.Length > 0 Then
        For x = 0 To StudentData_Person.Length - 1
            sum += ((CInt(StudentData_Person(x).Grade_String) - Mean_Decimal) ^ 2)
        Next
        stDev = Math.Round(Math.Sqrt(sum / (x - 1)), 2)

    End If
    stDev_TextBox.Text = stDev.ToString

    'Get information from ComboBox Selected Entry
    Students_ComboBox.SelectedIndex

    'Determine letter grade
    For y = 0 To StudentData_Person.Length
        If CInt(StudentData_Person(x).Grade_String) >= Mean_Decimal + (1.5 * stDev) Then
            CurvedGrade_TextBox.Text = "A"
        End If
        If (Mean_Decimal + (0.5 * stDev)) <= CInt(StudentData_Person(x).Grade_String) And CInt(StudentData_Person(x).Grade_String) < (Mean_Decimal + (1.5 * stDev)) Then
            CurvedGrade_TextBox.Text = "B"
        End If
        If (Mean_Decimal - (0.5 * stDev)) <= CInt(StudentData_Person(x).Grade_String) And CInt(StudentData_Person(x).Grade_String) < (Mean_Decimal + (1.5 * stDev)) Then
            CurvedGrade_TextBox.Text = "C"
        End If
        If (Mean_Decimal - (1.5 * stDev)) <= CInt(StudentData_Person(x).Grade_String) And CInt(StudentData_Person(x).Grade_String) < (Mean_Decimal + (1.5 * stDev)) Then
            CurvedGrade_TextBox.Text = "D"
        End If
        If CInt(StudentData_Person(x).Grade_String) < (Mean_Decimal - (1.5 * stDev)) Then
            CurvedGrade_TextBox.Text = "F"
        End If
    Next
End Sub
End Class

然后我还需要从中检索信息并计算出给定的分数,然后将其推回到我可以执行的文本框中。

我会做什么

    originalscore_textbox.text = StudentData_Person(SelectedIndex)
    curvedgrade_textbox.text = 'something like what's above this line

或类似的东西我知道这绝对是错的我只是在表达我所有的思维概念。有任何问题,请问我会尽力解释。但这确实是我所需要的,而且我已完成了我的程序哈哈。

1 个答案:

答案 0 :(得分:0)

如果您发布了StudentData_Person的定义方式,将会很有帮助。

我建议在StudentData_Person中添加一个新的Curved_String字段。并在计算弯曲坡度时填充它。然后你就做了。

originalscore_textbox.text = StudentData_Person(SelectedIndex).Grade_String
curvedgrade_textbox.text = StudentData_Person(SelectedIndex).Curved_String