首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

VB 動態添加控件:Label 未顯示需檢查 Width/Visible/Z-Order,調試定位

     編輯:程式設計疑難解答

代碼片段


Load Picture1(Picture1.UBound + 1)
Load Label1(Label1.UBound + 1)
Load sock(sock.UBound + 1)
SockIndex = Label1.UBound
SockIndex = Picture1.UBound
SockIndex = sock.UBound
Picture1(Picture1.UBound).Visible = True
Picture1(Picture1.UBound).Left = i * 3000
Label1(Label1.UBound).Visible = True
Label1(Label1.UBound).Left = i * 3000

現象:`Picture1` 和 `sock` 正常動態創建,`Label1` 不顯示。

排查清單

  1. Width / Caption 為空**:動態創建的 Label 默認 `Width=0`、`Caption=""`,需顯式設置:
    `Label1(Label1.UBound).Width = 2000`
    `Label1(Label1.UBound).Caption = sock(SockIndex).RemoteHostIP`
  2. Z-Order 被遮擋**:`Picture1` 在 `Label1` 之上。調用 `Label1(Label1.UBound).ZOrder vbBringToFront`。
  3. 容器縮放/坐標**:`Left = i * 3000` 可能超出可見區域,用 `ScaleMode` 確認單位(Twips/像素)。
  4. 調試**:斷點查看 `Label1(Label1.UBound).Left/Top/Width/Height/Visible/ZOrder` 屬性值。

修正代碼


Load Label1(Label1.UBound + 1)
With Label1(Label1.UBound)
    .Visible = True
    .Left = i * 3000
    .Top = Picture1(Picture1.UBound).Top + Picture1(Picture1.UBound).Height + 100
    .Width = 2000
    .Caption = sock(SockIndex).RemoteHostIP
    .ZOrder vbBringToFront
End With
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved