Calculating Median from a listbox

thirtystoned

New member
Joined
Apr 13, 2010
Messages
1
Programming Experience
Beginner
I'm extremely new to vb.net, but have been able to scrap code together to make most of my stuff work, but I can't seem to get this. I want to get the median from a listbox that is filled with information from a dataset when I call on a particular column via a radio button selection. There are 16 items in the column and for some reason i can't seem to index the 8 and 9 values to generate a median. Any help is welcome.

heres my code so far (my comments are for the most part chicken scratch)


Public Class Form1
Dim Newds As New Data.DataSet



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim connectionstr As String
connectionstr = "Provider=Microsoft.Jet.OLEDB.4.0; "
connectionstr &= " Data Source=" & "asn3.mdb" & ";"
Dim objcn As New OleDb.OleDbConnection(connectionstr)
objcn.Open()
' begin step 3
Dim mysql As String
mysql = "SELECT * FROM StudentScores ORDER BY Id"
Dim objcm As New OleDb.OleDbCommand(mysql, objcn)
' begin step 4
Dim objdr As OleDb.OleDbDataReader
objdr = objcm.ExecuteReader
' begin step 5
While (objdr.Read())

If RadioButton1.Checked Then
ListBox1.Items.Add(objdr("assignment1").ToString)
Else
If RadioButton2.Checked Then
ListBox1.Items.Add(objdr("assignment2").ToString)
End If
End If
If RadioButton8.Checked Then
ListBox1.Sorted = True
Dim min As Integer = ListBox1.Items.Item(0)
TextBox1.Text = min
Else
If RadioButton9.Checked Then
ListBox1.Sorted = True
Dim Max As Long
Dim i As Integer

For i = 0 To ListBox1.Items.Count - 1
If Val(ListBox1.Items(i)) > Max Then Max = Val(ListBox1.Items(i))
Next

TextBox1.Text = Max

Else

If RadioButton10.Checked Then
ListBox1.Sorted = True
Dim med1 As Integer = (ListBox1.Items.Item(8))
Dim med2 As Integer = (ListBox1.Items.Item(9))
TextBox1.Text = med1 = med2 / 2
End If
End If
End If

End While


objdr.Close()
objcn.Close()


'step 1 and 2 only need be done once.
objcn.Open()
' begin step 3

mysql = "SELECT * FROM StudentScores"
objcm.CommandText = mysql
' begin step 4
Dim objdr2 As OleDb.OleDbDataReader
objdr2 = objcm.ExecuteReader

Dim Newds As New Data.DataSet()
Dim tmpCart As Data.DataTable
tmpCart = New Data.DataTable()
tmpCart.TableName = "StudentScores"

Newds.Tables.Add(tmpCart)


tmpCart.Columns.Add(New Data.DataColumn("Id", GetType(Long)))
tmpCart.Columns.Add(New Data.DataColumn("Midterm", GetType(String)))
tmpCart.Columns.Add(New Data.DataColumn("Final", GetType(String)))
tmpCart.Columns.Add(New Data.DataColumn("Assignment1", GetType(Long)))
tmpCart.Columns.Add(New Data.DataColumn("Assignment2", GetType(Long)))
tmpCart.Columns.Add(New Data.DataColumn("Assignment3", GetType(Long)))
tmpCart.Columns.Add(New Data.DataColumn("Assignment4", GetType(Long)))
tmpCart.Columns.Add(New Data.DataColumn("Assignment5", GetType(Long)))

' begin step 5
Dim drNew As DataRow
While (objdr2.Read())
drNew = tmpCart.NewRow()

'first method to change to long
drNew("midterm") = Val(objdr2("midterm").ToString)
drNew("id") = objdr2("id")
drNew("final") = Val(objdr2("final").ToString)
drNew("Assignment1") = Val(objdr2("Assignment1").ToString)
drNew("Assignment2") = Val(objdr2("Assignment2").ToString)
drNew("Assignment3") = Val(objdr2("Assignment3").ToString)
drNew("Assignment4") = Val(objdr2("Assignment4").ToString)
drNew("Assignment5") = Val(objdr2("Assignment5").ToString)

tmpCart.Rows.Add(drNew)

End While
objdr2.Close()
objcn.Close()

DataGridView1.DataSource = Newds
DataGridView1.DataMember = "StudentScores"






End Sub




Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


DataGridView1.DataSource = DBNull.Value

DataGridView1.Refresh()

ListBox1.Items.Clear()

TextBox1.Clear()



End Sub
End Class
 

Latest posts

Back
Top