Visual Basic 2008 控制数组
我正在研究 VB 2008 (express) 中按钮数组的控制属性。我一直在查看 MS 白皮书中的示例代码,与 VB6 中所做的相比,这有点让我头晕! (是的,我是一个业余爱好者,所以请原谅糟糕的编码,对于这里有经验的人来说这可能是一个简单的问题......)
如果我正确地阅读它,那么现在正确的方法是创建一个单独的类,为该类创建构造函数等,然后在项目中实例化它?
我正在尝试使用一个相对简单的数组,如下所示:
'Create buttons
'Dim btn(30) As Button
'For i As Integer = 0 To 29
' btn(i) = New Button()
' btn(i).Width = 100
' btn(i).Height = 30
' btn(i).Text = i + 1
' btn(0).Left = 120
' btn(0).Top = 100
' If i >= 1 And i <= 14 Then
' btn(i).Left = 120
' btn(i).Top = btn(0).Top + (i * 30)
' End If
' If i = 15 Then
' btn(15).Left = 235
' btn(15).Top = 100
' End If
' If i >= 16 And i <= 29 Then
' btn(i).Left = 235
' btn(i).Top = btn(0).Top + ((i Mod 15) * 30)
' End If
' Me.Controls.Add(btn(i))
'Next
如果我将其放入加载方法中,那么我无法从另一个按钮访问它,因为我认为当此代码片段位于加载子例程中时它位于私有子例程中?有没有一种简单的方法可以只使用此代码,以便可以从表单中的其他函数访问 btn(i) 数组?
I'm looking at controlling properties of an array of buttons in VB 2008 (express). I have been looking at the sample code from a whitepaper at MS, and it kind of makes my head spin compared to what was done in VB6! (and yes I'm an amateur, so please forgive the poor coding and what is probably a simple question for the experienced here...)
If I'm reading it correctly, the correct way to do it now is to create a separate class, create constructors, etc. for that class, then instantiate it in the project?
I'm trying to use a relatively simple array like this:
'Create buttons
'Dim btn(30) As Button
'For i As Integer = 0 To 29
' btn(i) = New Button()
' btn(i).Width = 100
' btn(i).Height = 30
' btn(i).Text = i + 1
' btn(0).Left = 120
' btn(0).Top = 100
' If i >= 1 And i <= 14 Then
' btn(i).Left = 120
' btn(i).Top = btn(0).Top + (i * 30)
' End If
' If i = 15 Then
' btn(15).Left = 235
' btn(15).Top = 100
' End If
' If i >= 16 And i <= 29 Then
' btn(i).Left = 235
' btn(i).Top = btn(0).Top + ((i Mod 15) * 30)
' End If
' Me.Controls.Add(btn(i))
'Next
If I put it into the load method, then I can't access it from another button, because I think it's in a private subroutine when this snippet is in the load subroutine? Is there a simple way to just have this code so that the btn(i) array is accessible from other functions in the form?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使按钮数组成为类中的字段或属性,而不是加载方法中的字段或属性。
最简单的方法是将
Dim btn(30) As Button
声明从 load 方法移到类中(将其粘贴在 load 方法上方的行上),然后就完成了。人们可能会争论编码标准和其他东西 - 但我认为这会让你工作(-:
好的,除了上面和你的评论之外,还有足够的代码来说明我试图解释的内容。这段代码将编译并运行 -按钮数组可通过表单类中的两个方法访问:
You need to make the button array a field or a property in the class rather than in the load method.
Easiest way is to just move the
Dim btn(30) As Button
declaration out of the load method into the class (stick it on the line above the load method) and you're done.One might argue about coding standards and stuff - but I think that'll get you working (-:
Ok, further to the above and your comment, just enough code to illustrate what I was trying to explain. This code will compile and run - button array accessible from two methods within the form class: