Associare evento ad un crontollo creato a runtime

venerdì 29 aprile 2022 - 15.46
Tag Elenco Tags  VB.NET

superciuck Profilo | Newbie

Anzitutto ciao a todos.
Spero di aver scritto nell'area corretta....
Linguaggio Visual Basic
Ora non sto qui a spiegare il perchè ma.......
Devo creare in un form una decina di pulsanti indicizzati. Esempio Puls(0), Puls(1), Puls(2)........
Poi devo associare un azione al click di ognuno di questi.
Trovo un sacco di problemi a riuscire a farlo.
Se invece i pulsanti non fossero indicizzati nessun problema.
Allego sotto il codice di un semplice form dove con un pulsante ne creo altri 2 NON indicizzati
e ad ognuno dei due associo un azione al click.
Poi un secondo pulsante con il quale creo altri 4 pulsanti questa volta indicizzati ai quali non riesco ad associare
azioni.
Ho cercato di leggere in lungo e largo..... ora non massacratemi ma come al solito, per quel che mi riguarda
il sistema migliore per capire è un piccolo esempio passo passo.
Grazie 1000 a todos.....

Sotto il codice:

Public Class Form1
' Pulsanti NON indicizzati
Friend WithEvents Nuovo_Puls_01 As System.Windows.Forms.Button
Friend WithEvents Nuovo_Puls_02 As System.Windows.Forms.Button
'Pulsanti indicizzati
Dim Pulsante_Index(4) As Button
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub Button_Crea_2_Pls_Click(sender As Object, e As EventArgs) Handles Button_Crea_2_Pls.Click
'Creo il primo pulsante NON indicizzato
Nuovo_Puls_01 = New Button
Nuovo_Puls_01.Top = 100
Nuovo_Puls_01.Left = 12
Nuovo_Puls_01.Parent = Me
Nuovo_Puls_01.BringToFront()
'Creo il secondo pulsante NON indicizzato
Nuovo_Puls_02 = New Button
Nuovo_Puls_02.Top = 130
Nuovo_Puls_02.Left = 12
Nuovo_Puls_02.Parent = Me
Nuovo_Puls_02.BringToFront()
Label3.Visible = True
End Sub
Private Sub Nuovo_Puls_01_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Nuovo_Puls_01.Click
Beep()
Label1.Text = "Evento primo pulsante"
End Sub
Private Sub Nuovo_Puls_02_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Nuovo_Puls_02.Click
Beep()
Label2.Text = "Evento secondo pulsante"
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Btn_Pul_Lbl.Click
Label1.Text = ""
Label2.Text = ""
End Sub


Private Sub Button_Crea_4_Pls_Index_Click(sender As Object, e As EventArgs) Handles Button_Crea_4_Pls_Index.Click
Dim i As Integer
' un ciclo for per creare i 4 pulsanti indicizzati
For i = 1 To 4
Pulsante_Index(i) = New Button
Next
'poi uno per uno li piazzo, scrivo il testo etc etc......
Pulsante_Index(1).Top = 100
Pulsante_Index(1).Left = 360
Pulsante_Index(1).Width = 180
Pulsante_Index(1).Text = "Pulsante_Index(1)"
Pulsante_Index(1).Parent = Me
Pulsante_Index(1).BringToFront()

Pulsante_Index(2).Top = 130
Pulsante_Index(2).Left = 360
Pulsante_Index(2).Width = 180
Pulsante_Index(2).Text = "Pulsante_Index(2)"
Pulsante_Index(2).Parent = Me
Pulsante_Index(2).BringToFront()

Pulsante_Index(3).Top = 160
Pulsante_Index(3).Left = 360
Pulsante_Index(3).Width = 180
Pulsante_Index(3).Text = "Pulsante_Index(3)"
Pulsante_Index(3).Parent = Me

Pulsante_Index(3).BringToFront()
Pulsante_Index(4).Top = 190
Pulsante_Index(4).Left = 360
Pulsante_Index(4).Width = 180
Pulsante_Index(4).Text = "Pulsante_Index(4)"
Pulsante_Index(4).Parent = Me
Pulsante_Index(4).BringToFront()
End Sub
End Class
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5