We help IT Professionals succeed at work.
Private

Textbox Custom Controll.

emi_sastra
emi_sastra asked
on
40 Views
1 Endorsement
Last Modified: 2019-12-05
Hi Experts,

I create a custom textbox controller.

Imports System.Windows.Forms
Imports System.Drawing
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports ERV_SCREEN_FUNCTION
Imports ERV_GLOBAL_FUNCTION

Public Class ERV_TEXTBOX
    Inherits TextBox

    Implements INotifyPropertyChanged

    Private _Name As String = ""
    Protected lbl As New Label
    Private blnTrimOnValided As Boolean = False
    Private blnUpperCaseAll As Boolean = False

    Private blnOnFocus As Boolean = False 

    Public Event PropertyChanged As PropertyChangedEventHandler _
       Implements INotifyPropertyChanged.PropertyChanged

    Private Sub NotifyPropertyChanged(ByVal intMode As Integer)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(intMode))

        Show_Hide_Controls()

    End Sub

    Private Sub InitializeComponent()

        Me.SuspendLayout()

        Me.Text = ""
        Me.BackColor = ControlColor.Get_LostFocus_Color
        Me.Font = Screen_Function.CreateFont(Screen_Function.Get_Control_Font_Name, Screen_Function.Get_Control_Font_Size, False, False, False)

                Me.ResumeLayout(False)

    End Sub

    Private intMode As Integer = 1

    <Category("ERV Property")> _
    Property Mode() As Integer
        Get
            Return intMode
        End Get
        Set(ByVal Value As Integer)
            intMode = Value

            NotifyPropertyChanged(intMode)
        End Set
    End Property


    Private blnAllowChange As Boolean = True

    <Category("ERV Property")> _
    Property AllowChange() As Boolean
        Get
            Return blnAllowChange
        End Get
        Set(ByVal Value As Boolean)
            blnAllowChange = Value

        End Set
    End Property

    <Category("ERV Property")> _
    Property UpperCaseAll() As Boolean
        Get
            Return blnUpperCaseAll
        End Get
        Set(ByVal Value As Boolean)
            blnUpperCaseAll = Value

        End Set
    End Property

    Private blnViewModeVisible As Boolean = False

    <Category("ERV Property")> _
    Property ViewModeVisible() As Boolean
        Get
            Return blnViewModeVisible
        End Get
        Set(ByVal Value As Boolean)
            blnViewModeVisible = Value

        End Set
    End Property

    Private blnAddModeVisible As Boolean = True

    <Category("ERV Property")> _
    Property AddModeVisible() As Boolean
        Get
            Return blnAddModeVisible
        End Get
        Set(ByVal Value As Boolean)
            blnAddModeVisible = Value

        End Set
    End Property

    Private blnUpdateModeVisible As Boolean = True

    <Category("ERV Property")> _
    Property UpdateModeVisible() As Boolean
        Get
            Return blnUpdateModeVisible
        End Get
        Set(ByVal Value As Boolean)
            blnUpdateModeVisible = Value

        End Set
    End Property

    <Category("ERV Property")> _
    Property TrimOnValided() As Boolean
        Get
            Return blnTrimOnValided
        End Get
        Set(ByVal Value As Boolean)
            blnTrimOnValided = Value
            'OnPropertyChanged("AllowChange")
        End Set
    End Property

    Protected Overrides Sub OnGotFocus(e As EventArgs)
        MyBase.OnGotFocus(e)

        If blnOnFocus Then Exit Sub

        blnOnFocus = True

                Me.BackColor = ControlColor.Get_Focus_Color
        Me.Select()
    End Sub

    Protected Overrides Sub OnLostFocus(e As EventArgs)
        MyBase.OnLostFocus(e)

        blnOnFocus = False 

        Me.BackColor = ControlColor.Get_LostFocus_Color
    End Sub
 
    Private Sub ERV_TEXTBOX_TextChanged(sender As Object, e As EventArgs) Handles Me.TextChanged

        If blnUpperCaseAll Then
            Me.Text = Me.Text.ToUpper
        End If

        If lbl.Name.Trim <> "" Then
            Me.lbl.Text = Me.Text
            lbl.ForeColor = Me.ForeColor
        End If

    End Sub

    Private Sub ERV_TEXTBOX_Validating(sender As Object, e As CancelEventArgs) Handles Me.Validating

        Me.Text = Me.Text.Trim

    End Sub

    Private Sub ERV_TEXTBOX_VisibleChanged(sender As Object, e As EventArgs) Handles Me.VisibleChanged

        If (Me.DesignMode) Then Exit Sub

        If lbl.Name.Trim = "" Then
            If Screen_Function.Is_Create_Label(Me.Name) Then
                Screen_Function.Create_Label_Control(Me.Parent, Me, lbl, "lbl" & Me.Name, True, 4)
            End If
        Else
            Me.lbl.Text = Me.Text
            lbl.ForeColor = Me.ForeColor
        End If
        Me.lbl.Location = Me.Location
        Me.lbl.Size = Me.Size
        If Me.Visible = True Then
            lbl.Visible = False
        Else
            lbl.Visible = True
        End If

    End Sub

    Private Sub Show_Hide_Controls()

        Select Case intMode
            Case ERV_Global.MB_ADD
                Me.Visible = blnAddModeVisible
            Case ERV_Global.MB_UPD
                Me.Visible = blnUpdateModeVisible
            Case ERV_Global.MB_VIEW
                Me.Visible = blnViewModeVisible
        End Select

    End Sub


End Class

Open in new window


The problem is when I copy data to the textbox it produces twice data. Say I copy from notepad "abc", then the textbox become "abcabc'

One more thing is when I type "abc", it become "cba"

What is the problem?

Thank you.
Comment
Watch Question

Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
I don't see anything in here that reverse the text. Do you have other event handlers in your form?

Author

Commented:
- Do you have other event handlers in your form?
I have below code at the form load:

Public Shared Sub Set_Control_Behavior(ByVal ctl As Control, _
                                              Optional ByVal blnAutoFormatNumber As Boolean = True)


        For Each childCtrl As Control In ctl.Controls
            If TypeOf childCtrl Is TextBox Or _
               TypeOf childCtrl Is ComboBox Then

                If InStr(childCtrl.Name, "AtGrid") = 0 Then
                    AddHandler childCtrl.GotFocus, AddressOf TextBox_Got_Focus
                    AddHandler childCtrl.LostFocus, AddressOf TextBox_Lost_Focus
                    AddHandler childCtrl.VisibleChanged, AddressOf Hide_Label
                Else
                    AddHandler childCtrl.GotFocus, AddressOf TextBox_Lost_Focus
                    AddHandler childCtrl.LostFocus, AddressOf TextBox_Got_Focus
                End If

                If TypeOf childCtrl Is TextBox Then
                    If blnAutoFormatNumber Then
                        AddHandler childCtrl.TextChanged, AddressOf Number_Text_Format
                    End If
                End If

                AddHandler childCtrl.KeyPress, AddressOf Control_KeyPress
            ElseIf TypeOf childCtrl Is ComboBox Or _
              TypeOf childCtrl Is Button Then
                AddHandler childCtrl.KeyPress, AddressOf Control_KeyPress
            ElseIf TypeOf childCtrl Is Label Then
                AddHandler childCtrl.MouseDown, AddressOf Copy_Value_Of_Label
                If blnAutoFormatNumber Then
                    AddHandler childCtrl.TextChanged, AddressOf Number_Text_Format
                End If
            ElseIf TypeOf childCtrl Is DateTimePicker Then
                AddHandler childCtrl.VisibleChanged, AddressOf Hide_Label
            ElseIf TypeOf childCtrl Is DataGridView Then
                Dim dgv As DataGridView = DirectCast(childCtrl, DataGridView)

                With dgv
                    .AllowUserToAddRows = False
                    .AllowUserToDeleteRows = False
                    .EditMode = DataGridViewEditMode.EditProgrammatically
                    .SelectionMode = DataGridViewSelectionMode.FullRowSelect
                    .RowHeadersDefaultCellStyle.SelectionForeColor = Color.White

                    .EnableHeadersVisualStyles = False

                    .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize
                    .ColumnHeadersDefaultCellStyle.ForeColor = Color.Lime
                    .ColumnHeadersDefaultCellStyle.Font = New Font("Arial", 10, FontStyle.Bold)
                    .DefaultCellStyle.Font = New Font("Courier New", 8)
                    .AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(192, 192, 255)
                    .AlternatingRowsDefaultCellStyle.SelectionBackColor = Color.FromArgb(255, 128, 0)
                    '.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells

                    Dim row As DataGridViewRow = .RowTemplate
                    row.MinimumHeight = 22

                    '.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders
                    '.RowsDefaultCellStyle.WrapMode = DataGridViewTriState.True
                    '.DefaultCellStyle.WrapMode = DataGridViewTriState.True
                    '.RowTemplate.DefaultCellStyle.WrapMode = DataGridViewTriState.True

                    .RowHeadersWidth = 30
                End With

                AddHandler childCtrl.MouseDown, AddressOf Copy_Value_Of_Datagridview_Cell
            ElseIf childCtrl.HasChildren Then
                Set_Control_Behavior(childCtrl)
            End If
        Next

    End Sub

Open in new window


Thank you.
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
still can't see! you will have to place breakpoints everywhere and play with your app until your find where it goes! unless you can replicate your issue in a small demo form?

Author

Commented:
Ok. I will provide it tomorrow.

Thank you.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Get access with a 7-day free trial.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.