roy
roy

Reputation: 729

How to create a LostFocus event in a UserControl TextBox with vb.net

I'm Trying to create a LostFocus event in a UserControl TextBox with vb.net.

I tried lostfocus in the usercontrol textbox Textboxsearch then the messagebox does not stop if there is something wrong implementing my code . Textboxsearch It's in the position below and I attach also a gif file . SelectionStart & SelectionLength For this how can I update the code so that it also supports .

Another one if I move from textbox to textbox usercontrol makes the textbox like there is a moving event but if from the standard textbox it moves to a standard textbox then there is no move event in the standard textbox visualstudio and this can be seen from the gif file that I attach

please guide me .

Thanks

code in textbox usercontrol

Imports System.ComponentModel
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

<DefaultEvent("TextChanged")>
<ToolboxBitmap(GetType(PrintPreviewDialog))>
<Description("SearchBox control.")>
Public Class Textboxsearch
    Private _BorderColor As Color = Color.Gray
    Private _BorderFocusColor As Color = Color.CornflowerBlue
    Private _BackColor As Color = Color.White
    Private _ForeColor As Color = Color.Black
    Private _Font As Font
    Private _IsFocused As Boolean = False
    Private _AutoCompleteCustomSource As AutoCompleteStringCollection
    Private _AutoCompleteSource As AutoCompleteSource
    Private _AutoCompleteMode As AutoCompleteMode

    <DllImport("User32.dll", CharSet:=CharSet.Auto)>
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Int32
    End Function
    'Protected Overrides Sub OnHandleCreated(e As EventArgs)
    '    MyBase.OnHandleCreated(e)
    'End Sub
    'Properties
    Public Property BorderColor As Color
        Get
            Return _BorderColor
        End Get
        Set(value As Color)
            _BorderColor = value
            Invalidate()
        End Set
    End Property
    Public Property BorderFocusColor As Color
        Get
            Return _BorderFocusColor
        End Get
        Set(value As Color)
            _BorderFocusColor = value
            Invalidate()
        End Set
    End Property
    Public Overrides Property BackColor As Color
        Get
            Return _BackColor
        End Get
        Set(value As Color)
            _BackColor = value
            MyBase.BackColor = value
            txtSearch.BackColor = value
        End Set
    End Property
    Public Overrides Property ForeColor As Color
        Get
            Return _ForeColor
        End Get
        Set(value As Color)
            _ForeColor = value
            MyBase.ForeColor = value
            txtSearch.ForeColor = MyBase.ForeColor
        End Set
    End Property
    Public Overrides Property Font As Font
        Get
            Return _Font
        End Get
        Set(value As Font)
            _Font = value
            txtSearch.Font = value
        End Set
    End Property
    Public Property IsFocused As Boolean
        Get
            Return _IsFocused
        End Get
        Set(value As Boolean)
            _IsFocused = value
        End Set
    End Property

    <Browsable(True)>
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
    <Bindable(True)>
    <Category("My Control")>
    Public Overrides Property Text As String
        Get
            Return txtSearch.Text
        End Get
        Set(value As String)
            txtSearch.Text = value
        End Set
    End Property
    Public Property AutoCompleteSource As AutoCompleteSource
        Get
            Return txtSearch.AutoCompleteSource
        End Get
        Set(value As AutoCompleteSource)
            txtSearch.AutoCompleteSource = value
        End Set
    End Property

    Public Property AutoCompleteMode As AutoCompleteMode
        Get
            Return txtSearch.AutoCompleteMode
        End Get
        Set(value As AutoCompleteMode)
            txtSearch.AutoCompleteMode = value
        End Set
    End Property

    <Browsable(True)> <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> <Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", GetType(Drawing.Design.UITypeEditor))> <EditorBrowsable(EditorBrowsableState.Always)> <Localizable(True)> <DescriptionAttribute("TextBoxAutoCompleteCustomSourceDescr")>
    Public Property AutoCompleteCustomSource As AutoCompleteStringCollection
        Get
            Return txtSearch.AutoCompleteCustomSource
        End Get
        Set(value As AutoCompleteStringCollection)
            txtSearch.AutoCompleteCustomSource = value
        End Set
    End Property

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        SetStyle(ControlStyles.UserPaint, True)
        ' Add any initialization after the InitializeComponent() call.

        MinimumSize = New Size(0, 23)
    End Sub

    Protected Overrides Sub CreateHandle()
        MyBase.CreateHandle()
        If (Height > 23) Then Height = 23
        If (Font.Size > 10) Then txtSearch.Font = New Font(Font, 10)
    End Sub

    'Event
    <Browsable(True)>
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
    <Bindable(True)>
    <Category("My Control")>
    Public Shadows Event TextChanged As EventHandler

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        Using bmp As Bitmap = New Bitmap(Width, Height)
            Using g As Graphics = Me.CreateGraphics()
                g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
                Dim clientArea As Rectangle = New Rectangle(0, 0, Me.Width - 1, Height - 1)

                If (_IsFocused) Then
                    'Draw Focus Border
                    g.DrawRectangle(New Pen(_BorderFocusColor, 1), clientArea.X, clientArea.Y, clientArea.Width, clientArea.Height)
                Else
                    'Draw Normal Border
                    g.DrawRectangle(New Pen(_BorderColor, 1), clientArea.X, clientArea.Y, clientArea.Width, clientArea.Height)
                End If
            End Using
            e.Graphics.DrawImage(bmp.Clone, 0, 0)
            bmp.Dispose()
        End Using
        MyBase.OnPaint(e)
    End Sub
    Private Sub txtSearch_TextChanged(sender As Object, e As EventArgs) Handles txtSearch.TextChanged
        RaiseEvent TextChanged(sender, e)
    End Sub
    Private Sub txtSearch_Enter(sender As Object, e As EventArgs) Handles txtSearch.Enter
        _IsFocused = True
        Invalidate()
    End Sub
    Private Sub txtSearch_Leave(sender As Object, e As EventArgs) Handles txtSearch.Leave
        _IsFocused = False
        Invalidate()
    End Sub
    Private Sub txtSearch_Click(sender As Object, e As EventArgs) Handles txtSearch.Click
        MyBase.OnClick(e)
    End Sub
    Private Sub txtSearch_MouseEnter(sender As Object, e As EventArgs) Handles txtSearch.MouseEnter
        MyBase.OnMouseEnter(e)
    End Sub

    Private Sub txtSearch_MouseLeave(sender As Object, e As EventArgs) Handles txtSearch.MouseLeave
        MyBase.OnMouseLeave(e)
    End Sub
    Private Sub txtSearch_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtSearch.KeyPress
        MyBase.OnKeyPress(e)
    End Sub
    Private Sub txtSearch_LostFocus(sender As Object, e As EventArgs) Handles txtSearch.LostFocus
        _IsFocused = False
        Invalidate()
    End Sub
End Class
Public Class Form2
    Dim TestAutoComplete As New AutoCompleteStringCollection
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Bindtextbox()
        BindTextboxsearchUserControl()
    End Sub
    Private Sub Bindtextbox()
        Me.TextBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        Me.TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
        TestAutoComplete.Add("Mahesh Chand")
        TestAutoComplete.Add("Mac Jocky")
        TestAutoComplete.Add("Millan Peter")
        TextBox1.AutoCompleteCustomSource = TestAutoComplete
    End Sub
    Private Sub BindTextboxsearchUserControl()
        Me.Textboxsearch1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        Me.Textboxsearch1.AutoCompleteSource = AutoCompleteSource.CustomSource
        TestAutoComplete.Add("Mahesh Chand")
        TestAutoComplete.Add("Mac Jocky")
        TestAutoComplete.Add("Millan Peter")
        Textboxsearch1.AutoCompleteCustomSource = TestAutoComplete
    End Sub
    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        If TextBox1.Text = "" Then
            ErrorProvider1.SetError(TextBox1, "")
        Else
            If TestAutoComplete.Count > 0 Then
                ErrorProvider1.SetError(TextBox1, "")
            End If
        End If
    End Sub
    Private Sub TextBox1_LostFocus(sender As Object, e As EventArgs) Handles TextBox1.LostFocus
        TestAutoComplete.Add("Mahesh Chand")
        TestAutoComplete.Add("Mac Jocky")
        TestAutoComplete.Add("Millan Peter")
        TextBox1.AutoCompleteCustomSource = TestAutoComplete
        If TextBox1.Text = "C" And TextBox1.Text <> "" Then
            ErrorProvider1.SetError(TextBox1, "Name still doesn't match")
            MsgBox("Name still doesn't match")
            Application.DoEvents()
            TextBox1.SelectionStart = 0
            TextBox1.SelectionLength = TextBox1.Text.Length
            TextBox1.Select()
        End If
    End Sub
    Private Sub Textboxsearch1_TextChanged(sender As Object, e As EventArgs) Handles Textboxsearch1.LostFocus
        If Textboxsearch1.Text = "" Then
            ErrorProvider1.SetError(Textboxsearch1, "")
        Else
            If TestAutoComplete.Count > 0 Then
                ErrorProvider1.SetError(Textboxsearch1, "")
            End If
        End If
    End Sub
    Private Sub Textboxsearch1_LostFocus(sender As Object, e As EventArgs) Handles Textboxsearch1.LostFocus
        TestAutoComplete.Add("Mahesh Chand")
        TestAutoComplete.Add("Mac Jocky")
        TestAutoComplete.Add("Millan Peter")
        Textboxsearch1.AutoCompleteCustomSource = TestAutoComplete
        If Textboxsearch1.Text = "C" And Textboxsearch1.Text <> "" Then
            ErrorProvider1.SetError(Textboxsearch1, "Name still doesn't match")
            MsgBox("Name still doesn't match")
            Application.DoEvents()
            'Textboxsearch1.SelectionStart = 0
            'Textboxsearch1.SelectionLength = TextBox1.Text.Length
            Textboxsearch1.Select()
        End If
    End Sub
End Class

resultintextbox

Upvotes: 0

Views: 68

Answers (0)

Related Questions