Form4
包含PictureBox
刷新代码,该代码以 2 秒的间隔逐个刷新所选图片。这是通过在变量增加 10 时将循环提供给 1000 值来实现的。当Form4
关闭时,这个循环需要中断。ListBox1
项包含通过 openfile对话框选择的图像的路径。 等待并刷新映像工作正常。
我已经尝试了所有可能的方法来检测表单是否已关闭或应用程序是否已关闭。那些失败得非常严重。但我确实相信有一种方法可以在用户返回到 [主菜单] 时中断循环form1
。
尝试Application.openforms.count
方法,无法将其添加到 for 每个循环中,因为它给出 0 值。
也可以在这里没有消息,而是将文本仅显示到富文本框中,但是当Windows表单应用程序关闭时,应用程序仍将运行到g = 1000
并在任务管理器中处于活动状态。
因此,do while 循环需要休息。
Do Until g = 1000
For Each Item In ListBox1.Items
PictureBox1.Image = Image.FromFile(Item)
PictureBox1.Refresh()
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
' g = g + 10
' MsgBox(Item)
CreateObject("WScript.Shell").Popup(Item, 1, "Title")
wait(2)
Next
' need to break here if the form is closed.
Loop
Private Sub wait(ByVal seconds As Integer)
For i As Integer = 0 To seconds * 100
System.Threading.Thread.Sleep(10)
Application.DoEvents()
Next
End Sub
循环中断是需要的条件,当该特定表单关闭时,则中断循环。
你试过计时器组件吗?将计时器组件添加到窗体。将"间隔"属性设置为"(毫秒)2000"。当您的表单关闭时,它将停止滴答作响;无需担心循环。
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Static Index As Integer 'Static preserves the value between calls to the method
If Index > ListBox1.Items.Count - 1 Then
Index = 0 'will start the display over again
'If you want to stop at the final image just Timer1.Enabled = False
End If
Dim Item = ListBox1.Items(Index).ToString
PictureBox1.Image = Image.FromFile(Item.ToString)
PictureBox1.Refresh()
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
'Option Strict does not like the following line
CreateObject("WScript.Shell").Popup(Item, 1, "Title")
Index += 1
End Sub
在Form4
类中定义私有布尔变量Dispose
然后按以下方式定义类的方法:
Public Class Form4
Private _isFormClosed = False
'rest of your code here
Public Overloads Sub Dispose(disposing As Boolean)
MyBase.Dispose(disposing)
_isFormClosed = True
End Sub
End Class
然后只需在循环中使用 bool 变量来打破它。
另一种方法是在那里使用布尔变量FormClosing
事件和设置值。
If Me.IsDisposed Then
Exit Do
End If
这在
Do Until g = 1000
For Each Item In ListBox1.Items
' If tf.Contains("WindowsApplication6.Form4, Text: Form4") Then
PictureBox1.Image = Image.FromFile(Item)
PictureBox1.Refresh()
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
g = g + 10
'MsgBox(Item)
RichTextBox1.Text = System.IO.Path.GetFileName(Item)
'RichTextBox1.SelectionAlignment = HorizontalAlignment.Center
' RichTextBox1.BackColor = Color.AliceBlue
' RichTextBox1.Font = New Font(RichTextBox1.Font, FontStyle.Bold)
'CreateObject("WScript.Shell").Popup(Item, 1, "Title")
wait(2)
Next
' here
If Me.IsDisposed Then
Exit Do
End If
Loop
神奇的是,这只需要在代码中正确中断。 感谢所有通过宝贵意见支持我解决这个问题的人。