如何添加新控件?

rongxh 2005-11-07 10:49:27
请问在VB中,如何用程序动态地增加控件?
...全文
146 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
rongxh 2005-11-24
  • 打赏
  • 举报
回复
我是希望将控制动态地加到msflexgird行的相应位置,是以增加的形式,如何解决?
bbhere 2005-11-07
  • 打赏
  • 举报
回复
就是这样了
faysky2 2005-11-07
  • 打赏
  • 举报
回复
如果直接加到窗体上可以用楼上几位的方法,如果是加到控件上,可以使用SetParent函数和Move函数来控制:

'动态添加Text控件到SSTab各个页面上
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Dim myText() As VB.TextBox

Private Sub Command1_Click()
ReDim myText(SSTab1.Tabs - 1)
Dim i%
For i = 0 To SSTab1.Tabs - 1
SSTab1.Tab = i
Set myText(i) = Form1.Controls.Add("vb.TextBox", "txt" & i & "")
myText(i).Visible = True
myText(i).Text = "txt" & i
SetParent myText(i).hWnd, SSTab1.hWnd
myText(i).Move 350, 500, 1000, 330
Next
SSTab1.Tab = 0
End Sub

smzh8 2005-11-07
  • 打赏
  • 举报
回复
Private WithEvents NewButton As CommandButton
Private Sub Form_Load()
Set NewButton = Controls.Add("VB.CommandButton", "cmd", Me)
NewButton.Move 0, 0, 1000, 1000
NewButton.Caption = "button"
NewButton.Visible = True
End Sub
Sub newButton_Click()
MsgBox "OK"
End Sub
int_zsw 2005-11-07
  • 打赏
  • 举报
回复
在声明中加入:
Private WithEvents NewButton As CommandButton
...
在事件中加入:
Set NewButton = Controls.Add("VB.CommandButton", "cmdNew", Me)
NewButton.Move Command1.Left + Command1.Width + 240, Command1.Top
NewButton.Caption = "new button"
NewButton.Visible = True
即可

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧