创建具有相同事件和功能的重复控件 [英] Create duplicate controls with same events and functions

查看:63
本文介绍了创建具有相同事件和功能的重复控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以在运行时移动的按钮。现在我需要使用相同的事件和函数创建此控件的副本或副本。我该怎么做?



我在运行时使用此代码创建新按钮。

I have a button that can move during runtime. Now i need to create duplicates or copies of this control with the same events and functions. How do i do this?

I use this code to create new buttons during runtime.

Dim btn1 As New System.Windows.Forms.Button
Me.Controls.Add(btn1)



要创建的重复数量无法预测。


The number of duplicates to be created can not be predicted.

推荐答案

OK ...

这是引用(例如)Click-Event的方法。您可以根据需要进行修改:

OK ...
This is the Method which refers to (for example) the Click-Event. You could modify it like you need it :
Private Sub WochenElement_Click(sender As Object, e As System.EventArgs)
    Dim mySender As Button = Sender

    ' do what is necessary to be done here
    ' you could also add a 'Handles' directly to the method
End Sub





现在在循环中创建一些按钮的方法:



Now the way to create some Buttons in a Loop :

For i as integer = 1 To 6
    Dim myButton As New Button
    myButton.Backcolor = Color.Gray
    myButton.Name = "Week_" + i.ToString
    ' and other settings
    myButton.Parent = Me
    AddHandler myButton.Click, AddressOf WochenElement_Click
Next



此方法部分需要放置在您想要创建控件的位置。



我希望这能解决你的问题...



附加信息:

如果你处理你的父控件(以这种方式托管你的你必须通过RemoveHandler删除所有这些手动创建的处理程序!


this method-part need to be placed where you want to create the controls.

I hope this solves your question ...

Additional Information :
If you dispose your parent-Control (which hosts your in this way created elements) you have to remove all of this "Manual" created Handlers by RemoveHandler !


这篇关于创建具有相同事件和功能的重复控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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