伊莉討論區

標題: VB6 InputBox改成ComboBox輸入 [打印本頁]

作者: Waroger    時間: 2018-6-29 02:55 PM     標題: VB6 InputBox改成ComboBox輸入

本帖最後由 Waroger 於 2018-6-29 02:59 PM 編輯

將VB6 的InputBox元件改成下拉式選單,除了讓使用者方便輸入並能限制使用者輸入固定的數值。
  1. '底下在模組

  2. Private Type POINTAPI
  3.         x As Long
  4.         Y As Long
  5. End Type

  6. Private Type RECT
  7.         Left As Long
  8.         Top As Long
  9.         Right As Long
  10.         Bottom As Long
  11. End Type
  12. Private Type WINDOWPLACEMENT
  13.         Length As Long
  14.         flags As Long
  15.         showCmd As Long
  16.         ptMinPosition As POINTAPI
  17.         ptMaxPosition As POINTAPI
  18.         rcNormalPosition As RECT
  19. End Type

  20. Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  21. Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
  22. Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
  23. Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
  24. Declare Function SetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
  25. Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
  26. Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
  27. Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
  28. Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
  29. Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
  30. Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
  31. Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

  32. Public ms As String, cm As ComboBox

  33. Public Sub chg2ComboBox(ByVal h As Long, ByVal m As Long, ByVal i As Long, ByVal t As Long)
  34.     Dim x As Long, k As String
  35.     Static f As Long, tmp As String, b As Boolean
  36.    
  37.     x = FindWindow("#32770", ms)
  38.     If x Then
  39.        If b Then
  40.           k = cm
  41.           If tmp <> k Then SetWindowText f, k: tmp = k
  42.        Else
  43.           f = FindWindowEx(x, ByVal 0&, "Edit", vbNullString)
  44.           If f Then
  45.              Dim w As WINDOWPLACEMENT
  46.             
  47.              w.Length = Len(w)
  48.              GetWindowPlacement f, w
  49.              SetParent cm.hwnd, x
  50.              SetWindowPlacement cm.hwnd, w
  51.              w.showCmd = 0
  52.              SetWindowPlacement f, w
  53.              b = True
  54.           End If
  55.          
  56.        End If
  57.     Else
  58.        b = False: f = 0: tmp = ""
  59.        KillTimer h, i
  60.     End If
  61. End Sub

  62. '------------------------------------
  63. '底下在表單,置放一個CommandButton

  64. Const GW_CHILD = 5
  65. Const GWL_STYLE = (-16)
  66. Const NV_INPUTBOX As Long = &H5000&

  67. Private Sub Command1_Click()
  68.     Dim i, s
  69.    
  70.     Set cm = Controls.Add("VB.ComboBox", "Combo1")
  71.     With cm
  72.          .FontName = "微軟正黑體"
  73.          .FontItalic = True
  74.          .ForeColor = vbRed
  75.          .FontSize = 9
  76.          .Visible = True
  77.          s = Array("台北市", "新北市", "台北市", "台中市", "台南市", "高雄市")
  78.          For Each i In s
  79.              cm.AddItem i
  80.          Next
  81.          DestroyWindow GetWindow(.hwnd, GW_CHILD)
  82.          SetWindowLong .hwnd, GWL_STYLE, GetWindowLong(.hwnd, GWL_STYLE) + 1
  83.     End With
  84.     ms = "選擇城市"
  85.     SetTimer hwnd, NV_INPUTBOX, 10, AddressOf chg2ComboBox
  86.     s = InputBox("請選擇所在城市", ms)
  87.     Controls.Remove cm
  88.     If StrPtr(s) Then MsgBox s
  89. End Sub
複製代碼





歡迎光臨 伊莉討論區 (http://a401.file-static.com/) Powered by Discuz!