如何以编程方式向 VB.NET 中的窗体添加控件 [英] How to programmatically add controls to a form in VB.NET

查看:30
本文介绍了如何以编程方式向 VB.NET 中的窗体添加控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理 Visual Basic 2010 Express Edition 中的清单.我不知道库存所需的字段数.我希望我可以在程序中使用 for 循环添加文本框/复选框/按钮.有没有办法在不使用工具箱的情况下向表单添加控件?

I am working on an inventory in Visual Basic 2010 Express Edition. I don't know the number of fields that will be necessary for the inventory. My hope was that I could add textboxes/checkboxes/buttons using for loops in the program. Is there a way to add controls to a form without using the toolbox?

我可以通过在程序中实例化控件来添加控件吗?

Can I add controls by instantiating them in the program?

推荐答案

是.

Private Sub MyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim MyTextbox as New Textbox
    With MyTextbox
       .Size = New Size(100,20)
       .Location = New Point(20,20)
    End With
    AddHandler MyTextbox.TextChanged, AddressOf MyTextbox_Changed
    Me.Controls.Add(MyTextbox)

'Without a help environment for an intelli sense substitution
'the address name and the methods name
'cannot be wrote in exchange for each other.
'Until an equality operation is prior for an exchange i have to work
'on an as is base substituted.

End Sub

Friend Sub MyTextbox_Changed(sender as Object, e as EventArgs)
   'Write code here.
End Sub

这篇关于如何以编程方式向 VB.NET 中的窗体添加控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆