VB.Net如何以编程方式添加控件和事件处理程序

时间:2019-05-24 01:22:12

标签: vb.net events controls programmatically

我正在使用PictureBox控件创建游戏,并添加MouseDown事件以处理减少PictureBox的“健康”值的情况。

我想以编程方式在初始控件失去所有健康状态时添加一个新控件,以便为新PictureBox的显示方式添加差异,但是我不知道如何向以编程方式创建的控件添加事件处理程序

更新:感谢您的帮助! 如果您想这样做,这就是我完成的代码的工作方式。

Public Class Form1
    Private Sub NewBug()
        Dim pbBug As New PictureBox With {
        .Name = "pb",
        .Width = 100,
        .Height = 100,
        .Top = 75,
        .Left = 75,
        .SizeMode = PictureBoxSizeMode.Zoom,
        .Image = My.Resources.bug}
        Me.Controls.Add(pbBug)

        AddHandler pbBug.MouseDown, Sub(sender As Object, e As EventArgs)
                                    MessageBox.Show("Hello", "It Worked")
                                End Sub
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        NewBug()
    End Sub

谢谢, sunnyCr

2 个答案:

答案 0 :(得分:0)

某些事情加起来没有。似乎Dim MyButton As New Button()应该在类范围内声明,而不必在局部变量上声明WithEvents。此外,如果MyButton在本地以加载例程,则MyButton_Click子程序将不会编译。如果在类作用域中声明了它,那么您可以Dim MyButton As New Button来代替WithEvents MyButton As New Button,如果您想将其保留在本地,则可以执行以下操作

Dim MyButton As New Button With {
     .Name = "MyButton",
     .Top = 100,
     .Left = 100,
     .Image = My.Resources.SomeImage}

Controls.Add(MyButton)

AddHandler MyButton.Click, Sub(s As Object, ev As EventArgs)
                               'Do stuff
                           End Sub

通常这就是我的工作方式,除非我打算删除处理程序,否则我将创建一个Sub并使用AddressOf,例如您尝试使用它的方式。

答案 1 :(得分:0)

必须通过代码导入windows.system.Forms创建一个类来生成图片框;例如,您可以执行此操作,更改图片框的标签:

这是课程:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    class Etiqueta
    {
        private Label etiquetaHija;

        public Label EtiquetaHija { get => etiquetaHija; set => etiquetaHija = value; }

        public Etiqueta(Point posicion, Size dimensiones, string texto)
        {
            etiquetaHija = new Label();
            etiquetaHija.Size = dimensiones;
            etiquetaHija.Location = posicion;
            etiquetaHija.Text = texto;
            etiquetaHija.Click += EtiquetaHija_Click;
                   }

        private void EtiquetaHija_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Etiqueta Hija");
        }

  
    }
    

这是表单背后的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Etiqueta etiqueta = new Etiqueta(new Point(20, 20), new Size(100, 100), "Ejemplo de creacion de un label por codigo con manejador de eventos");
            this.Controls.Add(etiqueta.EtiquetaHija );
        }
    }
}