Suscribir controles a eventos vb.net AddHandler
Hola amigos , aqui os dejo una buena paractica usando addhandler… que es … la suscripcion de eventos de los propios controles a otras funciones.
Esto se comporta como si fuera un trigger en sql. Haciendo este tipo de funciones nos ahorramos mucho tiempo y codigo en nuestros formulario.
Tambien puedes hacerlo evento a evento pero si el formulario tiene muchos controles creo que te vas a aburrir de ir copiando y pegando codigo. XD
Ya sabes que primero esta el video donde explico el codigo que tienes en la parte inferior y te recomiendo que lo ves.
Si te gusta el video o el codigo , unete a mi canal en youtube y o suscribete en la web.
Aqui el video
Aqui el codigo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim dt As New DataTable dt.Columns.Add("CodPostal") dt.TableName = "tabla" Dim dr As DataRow dr = dt.NewRow dr("Codpostal") = "00000" dt.Rows.Add(dr) Me.DataGridView1.DataSource = dt Me.ComboBox1.DisplayMember = "CodPostal" Me.ComboBox1.DataSource = dt DesahibilarBotones() SuscribiraEventos() End Sub Private Function Guardar() Me.Button1.Enabled = True Me.Button2.Enabled = True End Function Private Function DesahibilarBotones() Me.Button1.Enabled = False Me.Button2.Enabled = False End Function Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click QuitarSuscribiraEventos() 'Hacer las operacion que haga falta ' ' DesahibilarBotones() SuscribiraEventos() End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click QuitarSuscribiraEventos() 'Hacer las operacion que haga falta ' ' DesahibilarBotones() SuscribiraEventos() End Sub Private Function SuscribiraEventos() For Each CN As Control In Me.Controls If CN.GetType.ToString = GetType(System.Windows.Forms.TextBox).ToString Then AddHandler CN.TextChanged, AddressOf Guardar End If If CN.GetType.ToString = GetType(System.Windows.Forms.ComboBox).ToString Then AddHandler CType(CN, System.Windows.Forms.ComboBox).SelectedIndexChanged, AddressOf Guardar End If If CN.GetType.ToString = GetType(System.Windows.Forms.DataGridView).ToString Then AddHandler CType(CN, System.Windows.Forms.DataGridView).RowsAdded, AddressOf Guardar AddHandler CType(CN, System.Windows.Forms.DataGridView).RowsRemoved, AddressOf Guardar AddHandler CType(CN, System.Windows.Forms.DataGridView).CellValueChanged, AddressOf Guardar End If Next End Function Private Function QuitarSuscribiraEventos() For Each CN As Control In Me.Controls If CN.GetType.ToString = GetType(System.Windows.Forms.TextBox).ToString Then RemoveHandler CN.TextChanged, AddressOf Guardar End If If CN.GetType.ToString = GetType(System.Windows.Forms.ComboBox).ToString Then RemoveHandler CType(CN, System.Windows.Forms.ComboBox).SelectedIndexChanged, AddressOf Guardar End If If CN.GetType.ToString = GetType(System.Windows.Forms.DataGridView).ToString Then RemoveHandler CType(CN, System.Windows.Forms.DataGridView).RowsAdded, AddressOf Guardar RemoveHandler CType(CN, System.Windows.Forms.DataGridView).RowsRemoved, AddressOf Guardar RemoveHandler CType(CN, System.Windows.Forms.DataGridView).CellValueChanged, AddressOf Guardar End If Next End Function |
Espero que te sirtva, un Saludo