GotFocus versus PreviewKeyDown or Other

S

Steve

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
 
C

Cor Ligthert [MVP]

Steve,

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

Cor
 
S

Shane

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 "ValueChanged" 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 "ValueChanged" 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.ComponentModel

with
Imports System.Text

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


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.EventArgs) Handles MyBase.Leave
If m_EnterValue <> Me.Text Then RaiseEvent ValueChanged(sender,

e)
End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top