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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
   | Public Class TextBoxFilter 
    inherits ToolStripTextBox 
 
    '-------------------------------------------------------------------------'
    '                                  ATTRIBUTS                              '
    '-------------------------------------------------------------------------'
    Private _columns As List( Of String )
    Private _critere As String
    Private _clearZone As Rectangle
    Private _label As Label
 
 
    '-------------------------------------------------------------------------'
    '                                  EVENEMENTS                             '
    '-------------------------------------------------------------------------'
    Public Event CritereChanged( ByVal critere As String )
 
 
    '-------------------------------------------------------------------------'
    '                                 CONSTRUCTEUR                            '
    '-------------------------------------------------------------------------'
    Public Sub New()
        'initialiations
        _columns = New List( Of String )
        _critere = String.Empty
        _clearZone = New Rectangle( Me.Size.Width - 17, Me.Size.Height - 10, my.Resources.ClearFilter.Width , my.Resources.ClearFilter.Height  ) 
        _label = New Label
 
        'création du label        
        _label.Text = ""
        _label.Width = My.Resources.ClearFilter.Width
        _label.Height = My.Resources.ClearFilter.Height
        _label.Image = My.Resources.ClearFilter
        _label.Location = New Point( Me.Width - ( My.Resources.ClearFilter.Width + 5 ), 3 )
        _label.Visible = False
        TextBox.Controls.Add( _label )
    End Sub
 
 
    '-------------------------------------------------------------------------'
    '                                  PROPRIETES                             '
    '-------------------------------------------------------------------------'
    Public ReadOnly Property Critere As String
        Get
            Critere = _critere
        End Get 
    End Property 
 
    Public Property Columns As List( Of String )
        Get
            Return _columns
        End Get
        Set ( ByVal list As List( Of String ) )
            _columns = list
        End Set
    End Property
 
 
    '-------------------------------------------------------------------------'
    '                                  EVENEMENTS                             '
    '-------------------------------------------------------------------------'
    Private Sub TextBoxFilter_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.TextChanged
        _critere = String.Empty 
 
        'création du critère       
        For Each col As String In _columns
            If ( _critere.Length > 0 ) then _critere &= " OR "
            _critere &= " " & col & " LIKE '*" & Me.Text  & "*'"
        next
 
        'si la case n'est pas vide, un filtre est appliqué, on colore la textbox
        If ( Me.Text.Length > 0 ) Then 
            Me.BackColor = system.Drawing.SystemColors.ControlLight 
            _label.Visible= True
        Else 
            Me.BackColor = system.Drawing.SystemColors.Window 
            _label.Visible = False
        End If
 
        'on lève l'événement
        RaiseEvent CritereChanged ( _critere )
    End Sub
 
    Private Sub TextBoxFilter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
        'TODO : Si on click sur l'image ClearFilter -> vider la TextBox
 
    End Sub
 
    Private Sub TextBoxFilter_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        If (     e.X >= _label.Location.X And e.X <= _label.Location.X + _label.Width _
             And e.Y >= _label.Location.Y And e.Y <= _label.Location.Y + _label.Height ) Then
            Me.Clear
        End If
    End Sub
 
    Private Sub TextBoxFilter_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If (     e.X >= _label.Location.X And e.X <= _label.Location.X + _label.Width _
             And e.Y >= _label.Location.Y And e.Y <= _label.Location.Y + _label.Height ) Then
            'TODO : Changer la forme du curseur
        End If
    End Sub
End Class | 
Partager