2015-11-06 53 views
0

将图片框叠加到图片上,如下所示。 Click for Form LayoutVisual Basic和SQL图片框控件

当表单加载时,如果学生出席班级,那么图片框会变成绿色的勾号。如果学生不参加课堂,图片框就会变成空白。

使用SQL我可以查询数据库以返回特定学生所参加的所有类。 这些类存储在一个数组中StudentClass(n)

时间表上有50个类存储在数组AttendsClass(n)中。所有的默认值都是False。 如果学生出席某个班级,则该数组中的特定出席班成为True。

加载表单后,用户可以单击一个图片框来选择该类,并且数组中的相应attends类变为true。 最后,用户保存表格,将类插回到数据库中。

我的问题是在Sub Form4_Load最后,我现在不得不重复相同的代码块50次。这只是检查学生是否参加了课堂,然后将图片框=设置为GUI(_new.jpg)上的图片。

enter code here 
If AttendsClass(1) = True Then 
     PictureBox1.Image = My.Resources._new 
    End If 
    If AttendsClass(2) = True Then 
     PictureBox2.Image = My.Resources._new 
    End If 
    If AttendsClass(3) = True Then 
     PictureBox3.Image = My.Resources._new 
    End If 
    If AttendsClass(4) = True Then 
     PictureBox4.Image = My.Resources._new 
    End If 

是否有可能把它放到一个循环中以缩短我的代码。代码确实有效,但是它的速度很慢且很长,所以任何改进都会有所帮助。 非常感谢亚历克斯·柯里 一个级计算学生 全码:

enter code here 
Imports System.Data.OleDb 
Public Class Form4 
Public con As New OleDbConnection 
Public ds As New DataSet 
Public da As OleDbDataAdapter 
Public cb As OleDbCommandBuilder 
Public constring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" & Application.StartupPath & "\wma.accdb" 
Public AttendsClass(50) As Boolean 
Public StdID As Integer = Form2.DataGridView1.SelectedRows(0).Cells("StudentID").Value 
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    Dim Forename As String = Form2.DataGridView1.SelectedRows(0).Cells("Forename").Value 
    Dim Surname As String = Form2.DataGridView1.SelectedRows(0).Cells("Surname").Value 
    Student.Text = "Student: " & Forename & " " & Surname 
    If Not con.State = ConnectionState.Open Then 
     con.ConnectionString = constring 
     con.Open() 
    End If 
    da = New OleDbDataAdapter("SELECT * FROM StudentClass WHERE StudentId = " & StdID, con) 
    da.Fill(ds, "Class") 
    Dim Maxrow As Integer = ds.Tables("Class").Rows.Count 
    Dim StudentClass(Maxrow) As Integer 
    For n = 1 To 50 
     AttendsClass(n) = False 
    Next 

    For n = 1 To Maxrow 
     StudentClass(n) = ds.Tables("Class").Rows(n - 1).Item(1) 
     For a = 0 To 50 
      If StudentClass(n) = a Then 
       AttendsClass(a) = True 
      End If 
     Next 
    Next 

    If AttendsClass(1) = True Then 
     PictureBox1.Image = My.Resources._new 
    End If 
    If AttendsClass(2) = True Then 
     PictureBox2.Image = My.Resources._new 
    End If 
    If AttendsClass(3) = True Then 
     PictureBox3.Image = My.Resources._new 
    End If 
    If AttendsClass(4) = True Then 
     PictureBox4.Image = My.Resources._new 
    End If 

End Sub 

Private Sub PictureBox1_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox1.Click 
    If PictureBox1.Image Is Nothing Then 
     PictureBox1.Image = My.Resources._new 
     AttendsClass(1) = True 
    Else 
     PictureBox1.Image = Nothing 
     AttendsClass(1) = False 
    End If 
End Sub 

Private Sub PictureBox2_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox2.Click 
    If PictureBox2.Image Is Nothing Then 
     PictureBox2.Image = My.Resources._new 
     AttendsClass(2) = True 
    Else 
     PictureBox2.Image = Nothing 
     AttendsClass(2) = False 
    End If 
End Sub 

Private Sub PictureBox4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox4.Click 
    If PictureBox4.Image Is Nothing Then 
     PictureBox4.Image = My.Resources._new 
     AttendsClass(4) = True 
    Else 
     PictureBox4.Image = Nothing 
     AttendsClass(4) = False 
    End If 
End Sub 

Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click 
    If PictureBox3.Image Is Nothing Then 
     PictureBox3.Image = My.Resources._new 
     AttendsClass(3) = True 
    Else 
     PictureBox3.Image = Nothing 
     AttendsClass(3) = False 
    End If 
End Sub 

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click 
    If Not con.State = ConnectionState.Open Then 
     con.ConnectionString = constring 
     con.Open() 
    End If 
    Dim cmd As New OleDb.OleDbCommand 
    cmd.Connection = con 

    'Deletes Existing Records To Be rewritten' 
    cmd.CommandText = "DELETE * FROM StudentClass WHERE StudentID=" & StdID 
    cmd.ExecuteNonQuery() 

    For n = 1 To 50 
     If AttendsClass(n) = True Then 
      cmd.CommandText = "INSERT INTO StudentClass (StudentID, ClassID) VALUES (" & StdID & "," & n & ")" 
      cmd.ExecuteNonQuery() 
     End If 
    Next 
    Me.Dispose() 
    MsgBox("Updated Successfully", MsgBoxStyle.Information, "WMA") 
    con.Close() 
End Sub 
End Class 

回答

0

工作例如:

Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ 
Handles PictureBox1.Click, ...., PictureBox50.Click 
    Dim pic As PictureBox = CType(Controls(CType(sender, PictureBox).Name), PictureBox) 
    'now you have clicked picturebox 
    'you can change image by pic.Image = ... 
    'if you want picturebox index, use line below: 
    Dim picidx As Integer = CInt(CType(sender, PictureBox).Name.Replace("PictureBox", "")) 

End Sub 

VB2010Ex。