I have the need to create an event that is executed when changing the properties of 3 objects that I have in a class. In order to present everything, I must show you what I have done: Below is the complete class, whose function is to create ToolStrip
a search filter fecha
where you can choose a date, between two dates or always, and it is as follows:
Public Class filtrofecha
Implements IDisposable
Private labelfiltroporfecha As ToolStripLabel
Public Property hora1 As New DateTime
Public Property hora2 As New DateTime
Public Property tipodeintervalo As New intervalo
Public Enum intervalo
porfecha
porintervalo
todoeltiempo
End Enum
Private horas1lab As Label
Private horas2lab As Label
Public horas1 As DateTimePicker
Public horas2 As DateTimePicker
Public tiempo As ComboBox
Private barraaagregar As ToolStrip
Private barra As ToolStripControlHost
Sub New(ByRef barraaagregar0 As ToolStrip, ByRef horas01 As DateTimePicker, ByRef horas02 As DateTimePicker, ByRef tiempo0 As ComboBox)
barraaagregar = barraaagregar0
horas1 = horas01
horas2 = horas02
horas1lab = New Label
horas2lab = New Label
tiempo = tiempo0
labelfiltroporfecha = New System.Windows.Forms.ToolStripLabel()
labelfiltroporfecha.Name = "labelfiltroporfecha"
labelfiltroporfecha.Size = New System.Drawing.Size(94, 24)
labelfiltroporfecha.Text = "Filtro de tiempo:"
horas1lab.Text = "Desde:"
horas2lab.Text = "Hasta:"
horas1.Format = DateTimePickerFormat.Short
horas2.Format = DateTimePickerFormat.Short
horas1.Width = 85
horas2.Width = 85
horas1.CustomFormat = "dd-MM-yyyy"
horas2.CustomFormat = "dd-MM-yyyy"
tiempo.Items.Add("Fecha")
tiempo.Items.Add("Intervalo")
tiempo.Items.Add("Todo el tiempo")
tiempo.DropDownStyle = ComboBoxStyle.DropDownList
barraaagregar.Items.Add(labelfiltroporfecha)
barra = New ToolStripControlHost(tiempo)
barraaagregar.Items.Add(barra)
barra = New ToolStripControlHost(horas1lab)
barraaagregar.Items.Add(barra)
barra = New ToolStripControlHost(horas1)
barraaagregar.Items.Add(barra)
barra = New ToolStripControlHost(horas2lab)
barraaagregar.Items.Add(barra)
barra = New ToolStripControlHost(horas2)
barraaagregar.Items.Add(barra)
AddHandler tiempo.SelectedIndexChanged, AddressOf TiempoSelectedIndexChanged
AddHandler horas1.ValueChanged, AddressOf horas1_ValueChanged
AddHandler horas2.ValueChanged, AddressOf horas2_ValueChanged
tiempo.SelectedIndex = 2
End Sub
Private Sub compararfechas()
Me.hora1 = horas1.Value
Me.hora2 = horas2.Value
If tiempo.SelectedIndex = 2 Then
If Not (DateTime.Compare(horas1.Value, horas2.Value) > 0) Then
mensaje("Ingreso de fechas incorrecto!", True)
End If
End If
End Sub
Private Sub horas1_ValueChanged(sender As Object, e As EventArgs)
compararfechas()
End Sub
Private Sub horas2_ValueChanged(sender As Object, e As EventArgs)
compararfechas()
End Sub
Sub TiempoSelectedIndexChanged(sender As Object, e As EventArgs)
Select Case tiempo.SelectedIndex
Case 1
tipodeintervalo = intervalo.porintervalo
horas2.Visible = True
horas2lab.Visible = True
horas1lab.Visible = True
horas1.Visible = True
Case 0
tipodeintervalo = intervalo.porfecha
horas2.Visible = False
horas2lab.Visible = False
horas1lab.Visible = True
horas1.Visible = True
Case 2
tipodeintervalo = intervalo.todoeltiempo
horas2.Visible = False
horas2lab.Visible = False
horas1.Visible = False
horas1lab.Visible = False
End Select
End Sub
#Region "IDisposable Support"
Private disposedValue As Boolean ' Para detectar llamadas redundantes
' IDisposable
Protected Overridable Sub Dispose(disposing As Boolean)
If Not disposedValue Then
If disposing Then
' TODO: elimine el estado administrado (objetos administrados).
End If
' TODO: libere los recursos no administrados (objetos no administrados) y reemplace Finalize() a continuación.
' TODO: configure los campos grandes en nulos.
End If
disposedValue = True
End Sub
' TODO: reemplace Finalize() solo si el anterior Dispose(disposing As Boolean) tiene código para liberar recursos no administrados.
'Protected Overrides Sub Finalize()
' ' No cambie este código. Coloque el código de limpieza en el anterior Dispose(disposing As Boolean).
' Dispose(False)
' MyBase.Finalize()
'End Sub
' Visual Basic agrega este código para implementar correctamente el patrón descartable.
Public Sub Dispose() Implements IDisposable.Dispose
' No cambie este código. Coloque el código de limpieza en el anterior Dispose(disposing As Boolean).
Dispose(True)
' TODO: quite la marca de comentario de la siguiente línea si Finalize() se ha reemplazado antes.
' GC.SuppressFinalize(Me)
End Sub
#End Region
End Class
Here the properties that I want to import would be Hora1
and Hora2
that they are Datetime
, and tipodeintervalo
that it is a custom object Enumerable
. The event to create is when the values of these properties are changed
Here is the code of how I create an object of this class:
Private filtroitemsanulados As filtrofecha
Dim tiempofiltro As New ComboBox
AddHandler tiempofiltro.SelectedIndexChanged, AddressOf fitroanulaciones
Dim horas1filtro As New DateTimePicker
AddHandler horas1filtro.ValueChanged, AddressOf fitroanulaciones
Dim horas2filtro As New DateTimePicker
AddHandler horas2filtro.ValueChanged, AddressOf fitroanulaciones
filtroitemsanulados = New filtrofecha(Barradeitemsanulados, horas1filtro, horas2filtro, tiempofiltro)
Here the default events of Valuechanged
and SelectedIndexChanged
of the parameters of horas1filtro
, horas2filtro
and tiempofiltro
they are something similar to what I am looking for, the problem with this is that the previously mentioned properties are not modified in these events, but a few milliseconds later. That is to say that they do not work for me, for example when changing the index of the combo box tiempofiltro
the index changes but what happens in the class in the same event is the following, I quote code:
Sub TiempoSelectedIndexChanged(sender As Object, e As EventArgs)
Select Case tiempo.SelectedIndex
Case 1
tipodeintervalo = intervalo.porintervalo
horas2.Visible = True
horas2lab.Visible = True
horas1lab.Visible = True
horas1.Visible = True
Case 0
tipodeintervalo = intervalo.porfecha
horas2.Visible = False
horas2lab.Visible = False
horas1lab.Visible = True
horas1.Visible = True
Case 2
tipodeintervalo = intervalo.todoeltiempo
horas2.Visible = False
horas2lab.Visible = False
horas1.Visible = False
horas1lab.Visible = False
End Select
End Sub
As you can see, the tipodeintervalo
, changes value after passing through this sub
. What I want is to create an event like the following:
Private filtroitemsanulados As filtrofecha
Dim tiempofiltro As New ComboBox
Dim horas1filtro As New DateTimePicker
Dim horas2filtro As New DateTimePicker
filtroitemsanulados = New filtrofecha(Barradeitemsanulados, horas1filtro, horas2filtro, tiempofiltro)
AddHandler filtroitemanulados.cambianpropiedades, AddresOf Rellenado
So when I go to said sub
it would have something like this:
Sub rellenado ()
'alguno de los siguientes cambian
msgbox(filtroitemsanulados.hora1.tostring) <--- con hora cambiada
msgbox(filtroitemsanulados.hora2.tostring) <--- con hora cambiada
msgbox(filtroitemsanulados.tipodeintervalo.tostring) <--- con variable ambiada
end sub
In the latter, only the variables will be displayed on the screen, the interesting thing would be that the changes die as soon as they are made by the user. I hope I explained myself well! THANK YOU
I was able to find a way to fix it, the first thing is to create an event with this simple line
Then, to the class already assigned this event, by creating an instance of this class you can invoke the event to a procedure
In the same class to invoke the event is with the following line
I always place this line after assigning a value to the property and that's it, I hope that whoever finds this question will find it helpful!