Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我在运行时创建了一组动态元素。如何事先为它们定义和声明函数?
即,我将在执行期间创建一组 25 个按钮,并为每个按钮分配各种属性值。
比如说,我如何为这些元素(即按钮)添加 onclick 事件?
在“编译”它们之前声明和定义它们会导致错误吗?不会吗?
试试这个 :
yourButton.Click += (o, args) => { // Code goes here... };
或为点击事件创建一个方法:
yourButton.Click += yourButton_Click; private void yourButton_Click(object sender, EventArgs eventArgs) { // Code goes here... }
你可以为任何甚至不仅仅是Click一个。
Click