我有一个Main(MDIParent)和Ticket(MDIChild)。在“故障单”表单中,有一个列表视图,显示上一个故障单故障,未决和未解决的故障单。当我添加新的故障单故障时,有一个按钮BtnCreat_T
,它将显示TicketAdd表单。票证表单不会关闭,我使用showdialog()
来显示TicketAdd。当我在TicketAdd表单中添加新的故障单问题时,Ticket表单中的列表视图将不会刷新,更新或重新加载。
此外,我注意到在TicketAdd表单中,我无法运行在Ticket表单中创建的Public Sub ViewRecords
。
这是我的代码,以工单形式Public Sub ViewRecords
形式存在。我称它为票证形式加载:
Public Sub ViewRecords()
Dim i As Integer = 0
LvTicket.Items.Clear()
con.Open()
SQLString = String.Empty
SQLString = "SELECT * FROM TblTickets ORDER BY Ticket_ID DESC"
SQLDA = New SqlDataAdapter(SQLString, con)
Using SQLDT As New DataTable
SQLDA.Fill(SQLDT)
Do Until i = SQLDT.Rows.Count
With LvTicket
.Items.Add(SQLDT.Rows(i)("Ticket_ID"))
With .Items(.Items.Count - 1).SubItems
.Add(SQLDT.Rows(i)("Dept"))
.Add(SQLDT.Rows(i)("Aff_Per"))
.Add(SQLDT.Rows(i)("N_Prob"))
.Add(SQLDT.Rows(i)("Prob_Des"))
.Add(SQLDT.Rows(i)("DateTime"))
.Add(SQLDT.Rows(i)("Findings"))
.Add(SQLDT.Rows(i)("Status"))
End With
i += 1
End With
Loop
End Using
con.Close()
End Sub
这是我的代码,用于在TicketAdd表单BtnCreat_T按钮中添加新的故障单:
Private Sub BtnCreateT_Click(sender As Object, e As EventArgs) Handles BtnCreateT.Click
If CmbDept.Text = "" Or TxtAffectedP.Text = "" Or CmbNOProblem.Text = "" Or TxtProbD.Text = "" Or TxtFindings.Text = "" Or CmbStatus.Text = "" Then
MsgBox("Their is an empty field, please check!", MsgBoxStyle.Exclamation, "Ticket Details")
Else
If MsgBox("Are all fields are correct? Please double check!", MsgBoxStyle.Information + MsgBoxStyle.YesNo, "Ticket Details") = MsgBoxResult.Yes Then
GetTicket()
con.Open()
SQLString = String.Empty
SQLString = "INSERT INTO TblTickets (Ticket_ID, Dept, Aff_Per, N_Prob, Prob_Des, DateTime, Findings, Status)" &
"VALUES (@TID, @D, @AP, @NP, @PD, @DT, @F, @S)"
SQLCmd = New SqlCommand(SQLString, con)
SQLCmd.Parameters.AddWithValue("@TID", LblTicketID.Text)
SQLCmd.Parameters.AddWithValue("@D", CmbDept.Text)
SQLCmd.Parameters.AddWithValue("@AP", TxtAffectedP.Text)
SQLCmd.Parameters.AddWithValue("@NP", CmbNOProblem.Text)
SQLCmd.Parameters.AddWithValue("@PD", TxtProbD.Text)
SQLCmd.Parameters.AddWithValue("@DT", DTPicker.Value)
SQLCmd.Parameters.AddWithValue("@F", TxtFindings.Text)
SQLCmd.Parameters.AddWithValue("@S", CmbStatus.Text)
SQLCmd.ExecuteNonQuery()
MsgBox("Data has been saved! Your Ticket Number is: " & LblTicketID.Text, MsgBoxStyle.OkOnly, "Ticket Details")
con.Close()
EmptyField()
CmbDept.Select()
Ticket.ViewRecords()
Else
CmbDept.Select()
End If
End If
End Sub
单击BtnCreat_T
按钮后,TicketAdd表单不会关闭,并且必须更新或刷新Ticket表单中的列表视图。
运行此代码时没有错误,但我无法在故障单窗体中重新加载或更新列表视图。
答案 0 :(得分:0)
如前所述,您显示了TicketAdd Form模式(.ShowDialog()) 因此,我建议只要显示该TicketAdd来自,更新票证表单就没什么关系了,因为您的应用程序焦点集中在ticketAdd-Dialog上。 关闭TicketAdd表单后,您可以在显示TicketAdd表单的子区域中调用ViewRecords。在那里,您可以直接访问您的票证的真实实例(由@JayV提及)
但是,如果您真的需要在保存每条记录时对其进行更新,则可以通过传递给TicketAdd表单的操作来处理它
在显示TicketAdd的Ticketform中,设置“操作”(参考Ticketform的子签名)
Dim lTicketAdd As New TicketAdd
lTicketAdd.SetUpdateData(AddressOf ViewRecords)
lTicketAdd.ShowDialog()
用Sub扩展TicketAdd表单以指定操作引用,然后在保存DataRecord的位置将此操作称为
'And Action to hold the Reference to your ViewRecords Code from the parent Form
Private _RefreshData As Action
'A sub to set this Action Reference
Public Sub SetUpdateData(ByVal vDelegation As Action)
_RefreshData = vDelegation
End Sub
Private Sub BtnCreateT_Click(sender As Object, e As EventArgs) Handles BtnCreateT.Click
.....
'Your code from above
'Instead of calling ViewRecord, call the Action
If _RefreshData IsNot Nothing Then
_RefreshData.Invoke()
End If
End Sub