我懷疑你的代碼陷入了無限循環。
通過你的榜樣繼...
假設i = 3
...
If i = 3 Then
If frmMain.Height < 247 Then
' Most likely > 247 as that's a very small height. Let's skip on for now...
ElseIf frmMain.Height > 247 Then
'Else would suffice - not need for ElseIf as it ignores the case frmMain.Height = 247
Do Until frmMain.Height = 247
'Shrink form height (very slowly - we're talking pixels here)
frmMain.Height = -1 'Not correct as discussed in the other answer and below
System.Threading.Thread.Sleep(100)
Loop
Do Until frmMain.lvRec.Height = 104
' Who says frmMain.lvRec.Height is > 104? It may well be less in which case this loop will never end. See note below.
frmMain.lvRec.Height = -1
System.Threading.Thread.Sleep(100)
Loop
End If
一般情況下,嘗試使用邏輯的條件,只有當你一定你會擊中精確值他們每次。
想象一下frmMain.lvRec.Height
從103
開始的場景......它會永遠減少1
,這意味着主UI線程將被鎖定,並且表單將永遠不會重繪。
一個更好的(但仍然不完美)的例子是...
While frmMain.lvRec.Height <> 104
If frmMain.lvRec.Height > 104
frmMain.lvRec.Height -= 1 'Same as frmMain.lvRec.Height = frmMain.lvRec.Height -1
Else
frmMain.lvRec.Height += 1 'vice-versa
End If
System.Threading.Thread.Sleep(100)
End While
理想的情況下,要在規模過渡到採取一個固定的時間長度。比方說,它應該最後2秒(2000毫秒)在100ms的步驟= 20個步驟...
Dim Current = frmMain.lvRec.Height
Dim Desired = 104
For i = 1 to 20 '1 to 20 since we're aiming for exactly 20 steps. Normally a for would start at 0 but in this case that would cause 21 steps
Dim Pos = Desired * (i/20) + Current * ((20-i)/20)
frmMain.lvRec.Height = Pos
System.Threading.Thread.Sleep(100)
End While
我們在這裏所做的是建立一個非常簡單的線性「寬鬆」。有效地取入20個步驟的舊值的百分比和新的百分比隨時間變化(如i
增加)
實施例...當前值= 200
,希望的值= 100
(中間值示於[ ]使其更容易理解)...
SizeThisLoop = Desired * (i/20) + Current * ((20-i)/20)
100 * (01/20) [=05] + 200 * (19/20) [=190] = 195
100 * (02/20) [=10] + 200 * (18/20) [=180] = 190
...
100 * (05/20) [=25] + 200 * (15/20) [=150] = 175
...
100 * (20/20) [=100] + 200 * (0/20) [=000] = 100
這給你一個很好的線性過渡。
值得一提的是,windows窗體(又名WinForms)在這類事情上特別糟糕。如果主(UI)線程正在使用,用戶界面通常不會更新。有些方法使用DoEvents
,但它們最好是黑客 - 所以除非你實現某種形式的後臺線程,否則用戶界面很可能會鎖定,直到完成循環(這就是你的表單消失的原因 - 這是永遠調整大小,沒有機會重繪)。
有幾種選擇。 WPF的設計具有這樣的好效果,但其行爲方式完全不同,不能用這種方式書寫。
如果失敗了,使用後臺線程可以提供幫助 - 後臺線程會隨着時間的推移而調整大小,並告訴前臺線程在窗口可以調整時調整窗口大小。
喜歡的東西...
Public Sub Resizer()
'Set up a background thread to do the resizing
Dim T As New Threading.Thread(Sub() ResizeWorker())
T.Start()
End Sub
Private Sub ResizeWorker()
'The background thread. Takes approx 2 seconds to complete
Dim Current = frmMain.lvRec.Height
Dim Desired = 104
For i = 1 to 20 '1 to 20 since we're aiming for exactly 20 steps. Normally a for would start at 0 but in this case that would cause 21 steps
Dim Pos = Desired * (i/20) + Current * ((20-i)/20)
UIResize(Cint(Pos)) 'Do a resize
System.Threading.Thread.Sleep(100)
End While
End Sub
Private Sub UIResize(NewSize As Integer)
'Only the UI thread is allowed to update controls. Check if we're allowed to do so? (Can we make a change without invoking another thread?)
If Me.InvokeRequired
'This code isn't being executed on the UI thread. Ask the UI thread to run this method with the same parameters when it when it can
Me.Invoke(Sub() UIResize(NewSize))
Else
'We're now running on the UI thread... Resize at will!
Me.lvRec.Height = NewSize
End IF
End Sub
使用京錄製視頻,並從此處鏈接 - 這是免費的。不太清楚你的意思。 – Neolisk
這是您的調試器的用途。在'If I = 3 Then'上設置一個斷點,並逐行執行你的代碼。檢查'frmMain.Height'和'frmMain.lvRec.Height'屬性。 –