哦控制阵列你在哪里? [英] Oh Control Arrays where art thou?

查看:56
本文介绍了哦控制阵列你在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,但这是另一个关于VB.Net缺乏

控制阵列的抱怨。我是VB.Net的新手,我正在构建一个

应用程序,它使用在运行时创建的可变数量的Label控件

。标签

控件的数量将在50到200之间变化。我创建了一个

标签数组并将它们放在表单上。它的工作原理很好,但是我无法为这些控件提出任何事件

。我做了一个带有9个标签的小演示

而没有使用控制数组,并且能够为控件编写一个

单个事件处理程序。我包含了我编写的代码代码片段,用于生成数组

和事件处理程序的代码。关于

如何在运行时创建可变数量的控件以及事件

的任何建议都将非常受欢迎。


Bernie


Sub BuildBoard(ByVal N As Integer)

''创建一系列标签,Dot(i)

''连续N个点数和行数

Dim i As Integer

Dim X0 As Integer,Y0 As Integer

X0 = 30

Y0 = 60

Dim Dot((N + 1)^ 2 - 1)作为标签

对于i = 0 To(N + 1)^ 2 - 1

Dot(i)=新标签

Controls.Add(Dot(i))

Dot(i ).Location = New Point(X0 + 35 *(i Mod _

(N + 1)),Y0 + 35 *(i \(N + 1)))

Dot(i).Size =新尺寸(5,5)

Dot(i).ImageList = imgDot

Dot(i).ImageIndex = 0

下一页

End Sub


Sub ProcessClick(ByVal发送者为对象,_

ByVal e As System.EventArgs)_

处理Dot2.Click,Dot3.Click,_

Dot4.Click, Dot5.Click

''事件处理程序交换标签Dot2,Dot3等中的图像。

如果DirectCast(发件人,标签).ImageIndex = 1则

DirectCast(发件人,标签).ImageIndex = 0

否则

DirectCast(发件人,标签).ImageIndex = 1

结束如果

End Sub

解决方案

嗨Bernie


好​​的尝试


您可以尝试使用AddHandler,它不是具有控制数组的缺点,只有限制是您可以在设计时创建控制数组。但是,您可以通过代码创建控件数组。任何方式我都不会谈论它。


通过删除控件数组并不意味着在VB.NET中丢失使用控件数组的优点。这些事情也可以在VB.NET中完成


即使你不需要使用With Event来定义对象。相反,您可以像通常那样为所有其他类声明对象。并使用addhandler将事件分配给对象。


例如


将实验室视为新标签(

Addhandler lab.EventName,Addressof<具有相同签名的程序


-------------------- -


在你的情况下,你有ProcessClick程序,它将处理点阵元素的点击事件


你能做什么创建新数组元素后需要编写以下代码:


AddHandler Dot(i).Click,Addressof ProcessClic


单击点(i)然后将触发processclick


如果发件人将包含dot(i)的实例


我希望这可以在一定程度上帮助你


Sadha Sivam

Malleable Minds Software Pvt Ltd(印度)


如果我了解你在做什么,你可以创建自己的标签类

(继承自原始标签);在这个类中添加一个事件,它将执行你想要的行为,或者像你正在做的那样调用另一个例行程序。

如下。然后创建此类的实例而不是原始标签。 IF

我理解你的正确。


" Bernie" <一个******* @ discussions.microsoft.com>在消息中写道

news:27 **************************** @ phx.gbl ... < blockquote class =post_quotes>对不起,但这是另一个关于VB.Net缺乏控制阵列的抱怨。我是VB.Net的新手,我正在构建一个
应用程序,它使用在运行时创建的可变数量的Label控件。标签
控件的数量将在50到200之间变化。我已经创建了一个
标签数组并将它们放在表单上。它的工作原理很好,但是我无法为这些控件引发任何事件。我在没有使用控制阵列的情况下制作了一个包含9个标签的小型演示,并且能够为控件编写一个
单一事件处理程序。我包括我编写的代码代码片段,用于生成数组
和事件处理程序的代码。关于如何在运行时创建带有事件的可变数量的控件的任何建议将非常受欢迎。

Bernie

Sub BuildBoard(ByVal N As Integer)
''创建一个标签数组,Dot(i)
''连续N个点和行数
Dim i As Integer
Dim X0 As Integer,Y0 As Integer
X0 = 30
Y0 = 60
Dim Dot((N + 1)^ 2 - 1)作为标签
对于i = 0 To (N + 1)^ 2 - 1
Dot(i)=新标签
Controls.Add(Dot(i))
Dot(i).Location = New Point(X0 + 35 *(i Mod _
(N + 1)),Y0 + 35 *(i \(N + 1)))
Dot(i).Size =新尺寸(5,5)
Dot(i).ImageList = imgDot
Dot(i).ImageIndex = 0
下一页





Sub ProcessClick(ByVal sender As Object, _
ByVal e As System.EventArgs)_
句柄s Dot2.Click,Dot3.Click,_
Dot4.Click,Dot5.Click
''事件处理程序交换标签Dot2,Dot3等中的图像。
如果DirectCast(发件人,标签) .ImageIndex = 1然后
DirectCast(发件人,标签).ImageIndex = 0
Else
DirectCast(发件人,标签).ImageIndex = 1
结束如果
结束子



*" Bernie" <一个******* @ discussions.microsoft.com> scripsit:

抱歉,这是另一个关于VB.Net缺乏控制阵列的抱怨。




创建Visual Basic .NET和Visual C#.NET中的控件数组:

< URL:http://msdn.microsoft.com/library/en-us/dv_vstechart/html/vbtchCreatingControlArraysInVisualBasicNETVisualCN ET.asp>


-

Herfried K. Wagner [MVP]

< URL:http://dotnet.mvps.org />


Sorry, but this ia another whine about VB.Net''s lack of
Control Arrays. I am new to VB.Net and I''m building an
application that uses variable number of Label controls
that are created at run time. The number of label
controls will vary between 50 and 200. I have created an
array of labels and placed them on the form. It works
great, BUT I have not been able to raise any events for
these controls. I made a small demo with 9 labels
without using a control array and was able to write an
single event handler for the controls. I am including
code snippets of the code I wrote to generate the array
and the code for the event handler. Any suggestions on
how to create a variable number of controls with events
at run time would be most appreciated.

Bernie

Sub BuildBoard(ByVal N As Integer)
''Create an array of labels, Dot(i)
''N number of dots in a row and the number of rows
Dim i As Integer
Dim X0 As Integer, Y0 As Integer
X0 = 30
Y0 = 60
Dim Dot((N + 1) ^ 2 - 1) As Label
For i = 0 To (N + 1) ^ 2 - 1
Dot(i) = New Label
Controls.Add(Dot(i))
Dot(i).Location = New Point(X0+35*(i Mod _
(N+1)),Y0+35*(i\(N + 1)))
Dot(i).Size = New Size(5, 5)
Dot(i).ImageList = imgDot
Dot(i).ImageIndex = 0
Next
End Sub

Sub ProcessClick(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Dot2.Click, Dot3.Click, _
Dot4.Click, Dot5.Click
''Event Handler swaps images in labels Dot2, Dot3, etc.
If DirectCast(sender, Label).ImageIndex = 1 Then
DirectCast(sender, Label).ImageIndex = 0
Else
DirectCast(sender, Label).ImageIndex = 1
End If
End Sub

解决方案

Hi Bernie

Good try

You can try with AddHandler, It is not a disadvantage of having control arrays, Only restriction is you can create control arrays in the design time. However you can create the control arrays through code. Any way i am not going to talk about that.

By removing control array doesn''t mean that the advantages of using the control arrays are lost in VB.NET. Also those things can be done in VB.NET

Even you don''t need to define the object using With Event. Instead you can declare the object as you normally do for all other class. and use addhandler to assign the event to the object.

For example

Dim lab as new Label(

Addhandler lab.EventName, Addressof <procedure with the same signature

---------------------

In your case, you have the ProcessClick procedure which is going to handle your click event of dot array elements

What you can do is

after you creating a new array element you need to write the following code

AddHandler Dot(i).Click, Addressof ProcessClic

When you click the dot(i) then the processclick will be fired

Where the sender will contain an instance of dot(i)

I hope this could help you up to some extent

Sadha Sivam
Malleable Minds Software Pvt Ltd (India


If I understand what you''re doing, you can create your own label class
(inheriting from the original label); add an event to this class that will
either do the behavior you want or call another routine like you''re doing
below. Then create instances of this class instead of the original label. IF
I understand you correctly.

"Bernie" <an*******@discussions.microsoft.com> wrote in message
news:27****************************@phx.gbl...

Sorry, but this ia another whine about VB.Net''s lack of
Control Arrays. I am new to VB.Net and I''m building an
application that uses variable number of Label controls
that are created at run time. The number of label
controls will vary between 50 and 200. I have created an
array of labels and placed them on the form. It works
great, BUT I have not been able to raise any events for
these controls. I made a small demo with 9 labels
without using a control array and was able to write an
single event handler for the controls. I am including
code snippets of the code I wrote to generate the array
and the code for the event handler. Any suggestions on
how to create a variable number of controls with events
at run time would be most appreciated.

Bernie

Sub BuildBoard(ByVal N As Integer)
''Create an array of labels, Dot(i)
''N number of dots in a row and the number of rows
Dim i As Integer
Dim X0 As Integer, Y0 As Integer
X0 = 30
Y0 = 60
Dim Dot((N + 1) ^ 2 - 1) As Label
For i = 0 To (N + 1) ^ 2 - 1
Dot(i) = New Label
Controls.Add(Dot(i))
Dot(i).Location = New Point(X0+35*(i Mod _
(N+1)),Y0+35*(i\(N + 1)))
Dot(i).Size = New Size(5, 5)
Dot(i).ImageList = imgDot
Dot(i).ImageIndex = 0
Next
End Sub

Sub ProcessClick(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Dot2.Click, Dot3.Click, _
Dot4.Click, Dot5.Click
''Event Handler swaps images in labels Dot2, Dot3, etc.
If DirectCast(sender, Label).ImageIndex = 1 Then
DirectCast(sender, Label).ImageIndex = 0
Else
DirectCast(sender, Label).ImageIndex = 1
End If
End Sub



* "Bernie" <an*******@discussions.microsoft.com> scripsit:

Sorry, but this ia another whine about VB.Net''s lack of
Control Arrays.



Creating Control Arrays in Visual Basic .NET and Visual C# .NET:
<URL:http://msdn.microsoft.com/library/en-us/dv_vstechart/html/vbtchCreatingControlArraysInVisualBasicNETVisualCN ET.asp>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


这篇关于哦控制阵列你在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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