GotFocus versus PreviewKeyDown or Other

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    #1

    GotFocus versus PreviewKeyDown or Other

    I want to save the current value of the textbox before the user changes it -
    in case I want to restore it. Currently, I'm setting a local string
    variable equal to the text value in GotFocus. Should I be doing this in a
    different event, and is there a industry-standard approach to doing this?

    Thanks

    Steve


  • Cor Ligthert [MVP]

    #2
    Re: GotFocus versus PreviewKeyDown or Other

    Steve,

    For that you have first to tell what is a change.
    Leaving the textbox or every character.

    Cor

    "Steve" <s.arndt@comcas t.netschreef in bericht
    news:uiLVdjhKHH A.4848@TK2MSFTN GP04.phx.gbl...
    >I want to save the current value of the textbox before the user changes
    >it -
    in case I want to restore it. Currently, I'm setting a local string
    variable equal to the text value in GotFocus. Should I be doing this in a
    different event, and is there a industry-standard approach to doing this?
    >
    Thanks
    >
    Steve
    >
    >

    Comment

    • Shane

      #3
      Re: GotFocus versus PreviewKeyDown or Other

      I've done something like this by subclassing the textbox control and
      adding a property to store the text on Enter. I'm pulling some text
      from an old post, so it's a bit more information than you requested.
      You don't need the "ValueChang ed" event for your question.

      Here's the "how to".
      This code was written under 2003, but works with 2005 as well.
      By the way, I'm giving you a small subset of subclassing the text box.
      Some tweaking may be needed.


      The control will raise a "ValueChang ed" event, if the text box "text"
      changed on lost focus, instead of firing an event on every character
      change, which is annoying!


      1) Create a user control
      2) Switch to Text editing mode
      3) After you put in the "Inherits TextBox" line, you will no longer see

      the user control on the design screen. The beauty of this method is
      that any changes that Microsoft does to their Textbox control in the
      future, your textbox will inherit the look and functionality as well.
      4) When you're done, you replace all instances of the standard textbox
      with your new textbox.


      Replace text
      Imports System.Componen tModel

      with
      Imports System.Text

      Public Class YourControlName
      Inherits TextBox
      Private m_EnterValue As String = ""
      Public Event ValueChanged(By Val sender As Object, ByVal e As
      System.EventArg s)


      Overloads Property Text(ByVal FormatData As Boolean) As String
      Get
      Text = Me.Text
      End Get
      Set(ByVal Value As String)
      Me.Text = Value
      m_EnterValue = Me.Text
      End Set
      End Property


      Property EnterValue() As String
      Get
      EnterValue= m_EnterValue
      End Get
      Set(ByVal Value As String)
      m_EnterValue = Value
      End Set
      End Property


      Private Sub YourControlName _Leave(ByVal sender As Object, ByVal e
      As System.EventArg s) Handles MyBase.Leave
      If m_EnterValue <Me.Text Then RaiseEvent ValueChanged(se nder,

      e)
      End Sub
      End Class

      Comment

      Working...