Vb.net net基础知识,如何创建用于处理动态创建的按钮的公共函数
我是vb.net的新手。老实说,我才用了一个星期左右。在为动态创建的按钮创建公共函数时,我遇到了一个难题 我有一个表单,其中有以下按钮创建代码:Vb.net net基础知识,如何创建用于处理动态创建的按钮的公共函数,vb.net,Vb.net,我是vb.net的新手。老实说,我才用了一个星期左右。在为动态创建的按钮创建公共函数时,我遇到了一个难题 我有一个表单,其中有以下按钮创建代码: 'Function for creating buttons Public Sub CreateNewButton(font As String, x As Integer, y As Integer, width As Integer, height As Integer,
'Function for creating buttons
Public Sub CreateNewButton(font As String, x As Integer, y As Integer,
width As Integer, height As Integer,
name As String, text As String,
menu_type As Integer,
Optional hidden As Boolean = True,
Optional centered As Boolean = False)
'Create a button as an entity
Dim btn As Button = New Button
'Assign the location
If centered = True Then
btn.Location = New Point(x - width / 2, y - height / 2)
Else
btn.Location = New Point(x, y)
End If
'Assign other variables from function parameters
btn.Name = name
btn.Text = text
btn.Height = height
btn.Width = width
'Should the button be hidden when created?
If hidden = True Then
btn.Hide()
End If
'Change the font
If font = "Normal" Then
btn.Font = setFont(btn.Font, "Consolas", 18)
ElseIf font = "Small" Then
btn.Font = setFont(btn.Font, "Consolas", 12)
End If
'Assign the Handlers and non-function variables
btn.BackColor = Color.Gray
btn.Anchor = AnchorStyles.None
Me.Controls.Add(btn)
AddHandler btn.Click, AddressOf ButtonOnMouseClick
AddHandler btn.MouseHover, AddressOf ButtonOnMouseHover
AddHandler btn.MouseLeave, AddressOf ButtonOnMouseLeave
End Sub
所以基本上,它在这种形式中使用时就像一个符咒。但是当我尝试以不同的形式使用它时,我意识到我必须再次创建相同的函数。如果我必须对每一种形式都这样做,那么这个过程就会变得单调乏味。
这就是为什么我决定创建一个名为PublicFunctions的新模块,在这个模块中,每个模块共享公共函数。问题是,一旦创建,它就无法执行,因为它返回的错误“Controls.add”以前没有声明过
我相信这是一件非常简单的事情。我试图寻找可能丢失的库,并尝试导入System.Windows.Forms.Controls,但仍然出现相同的错误
如何创建一个可以在不同表单之间共享的公共函数
提前感谢,Alex根据要求提供示例代码: 方法1将控件集合传递给助手函数
Public Sub CreateNewButton(parentControls As ControlCollection,
font As String, x As Integer, y As Integer,
width As Integer, height As Integer,
name As String, text As String,
menu_type As Integer,
Optional hidden As Boolean = True,
Optional centered As Boolean = False)
' Abbreviated implementation to illustrate the point
Dim newButton As New Button
newButton.Top = y
newButton.Left = x
newButton.Name = name
newButton.Text = text
parentControls.Add(newButton)
End Sub
Public Sub CreateNewButton(parent As Control,
font As String, x As Integer, y As Integer,
width As Integer, height As Integer,
name As String, text As String,
menu_type As Integer,
Optional hidden As Boolean = True,
Optional centered As Boolean = False)
' Abbreviated implementation to illustrate the point
Dim newButton As New Button
newButton.Top = y
newButton.Left = x
newButton.Name = name
newButton.Text = text
parent.Controls.Add(newButton)
End Sub
方法2将父容器传递给助手函数
Public Sub CreateNewButton(parentControls As ControlCollection,
font As String, x As Integer, y As Integer,
width As Integer, height As Integer,
name As String, text As String,
menu_type As Integer,
Optional hidden As Boolean = True,
Optional centered As Boolean = False)
' Abbreviated implementation to illustrate the point
Dim newButton As New Button
newButton.Top = y
newButton.Left = x
newButton.Name = name
newButton.Text = text
parentControls.Add(newButton)
End Sub
Public Sub CreateNewButton(parent As Control,
font As String, x As Integer, y As Integer,
width As Integer, height As Integer,
name As String, text As String,
menu_type As Integer,
Optional hidden As Boolean = True,
Optional centered As Boolean = False)
' Abbreviated implementation to illustrate the point
Dim newButton As New Button
newButton.Top = y
newButton.Left = x
newButton.Name = name
newButton.Text = text
parent.Controls.Add(newButton)
End Sub
它看起来像什么的屏幕截图。
*新1来自第一个函数
*新的2来自第二个函数
这两个函数可以这样调用:
CreateNewButton(Me.Controls, "", 10, 10, 50, 20, "btnNew1", "New 1", 1)
CreateNewButton(Me, "", 100, 100, 50, 20, "btnNew2", "New 2", 1)
创建一个类库.DLL,比如包含函数的MyFunctionsLib.DLL,并将其放在新项目文件夹的同一文件夹或任何子文件夹中。 创建另一个新项目时,您可以添加对“DLL”文件的引用,并可以访问这些函数。 在visual Studio 2017中,文件->新建项目->visual Basic->.NET标准->类库 并给出一个合适的名称,如MyFunctionsLib,单击Ok 例如
构建您的项目并将“DLL”用于任何其他项目。您可以传入父容器。控件属性,然后这将成为非问题。不再是我。您需要添加一个Control类型的额外参数。@JayV您能给我一个伪代码示例吗?@HansPassant我已经尝试将myControl添加为Control作为额外参数,但错误仍然存在,因为“add”不是Control的成员,而是myControl.Controls的成员