2014-01-12 37 views
0

好的,在這裏給你一個更大的圖片;我有一個顯示來自數據庫的記錄的列表視圖,我想要改變列表框和窗體窗口的大小,以及列表視圖包含的行數。簡單。調整窗口大小使窗體消失

這裏是我的代碼,接縫,使表單消失 - 注意該程序仍然運行一個圖標,但沒有可見的形式。

frmMain的形式窗口包含列表視圖 - 它的高度是228

lvRec是列表視圖 - 它的高度是85

此代碼存儲在一個文件,並在需要時通過被稱爲形式,

Public Sub Resizer() 



'Count records/rows with data 
     Dim i As Integer 
       i = frmMain.lvRec.Items.Count 

'Set window Size 
    If i = 3 Then 
     If frmMain.Height < 247 Then 
      Do Until frmMain.Height = 247 
       frmMain.Height = +1 
       System.Threading.Thread.Sleep(100) 
      Loop 
      Do Until frmMain.lvRec.Height = 104 
       frmMain.Height = +1 
       System.Threading.Thread.Sleep(100) 
      Loop 
     ElseIf frmMain.Height > 247 Then 
      Do Until frmMain.Height = 247 
       frmMain.Height = -1 
       System.Threading.Thread.Sleep(100) 
      Loop 
      Do Until frmMain.lvRec.Height = 104 
       frmMain.lvRec.Height = -1 
       System.Threading.Thread.Sleep(100) 
      Loop 
     End If 

他們目前在ListView 3行,但該代碼使得形式消失,即使我已經指出它expand247

+0

使用京錄製視頻,並從此處鏈接 - 這是免費的。不太清楚你的意思。 – Neolisk

+0

這是您的調試器的用途。在'If I = 3 Then'上設置一個斷點,並逐行執行你的代碼。檢查'frmMain.Height'和'frmMain.lvRec.Height'屬性。 –

回答

0

考慮你當前的代碼:

Do Until frmMain.Height = 247 
       frmMain.Height = +1 
       System.Threading.Thread.Sleep(100) 
      Loop 

frmMain.Height成爲有史以來大於或等於247,如果你總是將其設置爲+1

+0

嗨,我的印象是'+1'應該每次循環時將當前表單大小增加1 ... – user2980316

+1

您的印象不正確。 '+ 1'與'1'相同。也許你的意思是'frmMain.Height = frmMain.Height + 1',或'frmMain.Height + = 1' –

+0

是的,剛剛發現它是'+ = 1',我更新了我的代碼,似乎也有類似的問題到之前,我會更新這個,當我工作出現在什麼時候:) – user2980316

0

我懷疑你的代碼陷入了無限循環。

通過你的榜樣繼...

假設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.Height103開始的場景......它會永遠減少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