Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1712
  • Last Modified:

VB.net overrides Textbox.GotFocus event

in VB.net Windows Forms:

i have 2 textboxes:
Text1 and Text2
how do i write codes to overrides the base class TextBox's GotFocus event?

i want to write a line like this:
this.SelStart = 0
this.SelLength = len(this.Text)
or
this.SelectAll() <- new .net function

so that every textbox will automatically highlight its text while got focus.
0
chnelson
Asked:
chnelson
1 Solution
 
JagarCommented:
Why would you do that.  I have VB.NET Beta 2 and it already automatically selects the entire text when I enter into the textbox.

But if you really want to know how to do it then Create a New Windows Control Library and instead of inheriting it System.Windows.Forms.Ccontrol inherit it from System.Windows.Forms.TextBox.  The replace the GotFocus (or whatever you want) with your own function implementations.  You may have to use the Shadows or Overides keywords on the procedure declaration.
0
 
chnelsonAuthor Commented:
i am also using beta 2. Which property are you using to making those textbox automatically highlight all text while got focus?
0
 
JagarCommented:
None I just dropped a textbox on a form and highighted all of the text when I entered the textbox automatically
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
AzraSoundCommented:
The textbox should return to the state it was last left in.  Thus, if it were highlighted previously, when it receives focus again, it should, again, be highlighted.  I just went and looked, and it appears you can make it always highlighted when tabbed to:

Public Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter
    TextBox1.SelectionStart = 0
    Textbox1.SelectionLength = Len(Textbox1.Text)
End Sub
0
 
chnelsonAuthor Commented:
the text inside the textbox does not highlight while it got focus
how come yours can get highlight without any coding in the gotfocus event?

0
 
JagarCommented:
I'm not sure.  Let me try another project and see if it happens agains.
0
 
JagarCommented:
   AzraSound is right the textbox is just remembering whatever was selected last time I was in the textbox and I fill the textbox in during the load event for example then when I enter into that textbox everything will be selected by default.  But if I go into an empty textbox and type some stuff into it and then leave that textbox and go back into it nothing is selected, because the last time I was in that textbox I never selected anything.
    So you can either go each textbox and add that code to automatically select it.  Or you can look into using your own delegate that you attach each of the GotFocus events for the textboxes to that delegate.  This way you only have to write the selection code once and you just attach each of the textboxes to it.
    Or if you don't want to have to do anything (beside the first creation of it) you can create a new Windows Control Project.  Then at the top of the class module where it says it inherits from Windows.Forms.Control.  Change that you say Windows.Forms.Textbox.  Now your class will have all of the functionality of a textbox and you can override whatever methods you want, i.e. the GotFocus event.  
    If you go this method then you may want to add additional properties, like making it only accept numbers or whatever else you think may be usefull.  
    After you write this then you compile it and now you can reference the newly created control in you projects and it will automatically do everything you want.
0
 
chnelsonAuthor Commented:
thanks Jagar and AzraSound so much information.
what AzraSound's said i use it everytime in my VB6 programming.

now, in VB.net, i just wonder if any better way to prevent "repeating same codes".

and no need to reference the control name,
so far i am very new to VB.net and i havent find any "this" keyword in VB.net while there is already one in VC and C#.
0
 
chnelsonAuthor Commented:
no suitable answer replied
0
 
iboutchkineCommented:
try this

 Protected Overrides Sub OnMouseEnter(ByVal e As System.EventArgs)
        Dim ctl As TextBox
        For Each ctl In Me.Controls
            If ctl.Focused Then
                ctl.Select(0, Len(ctl.Text))
            End If
        Next
    End Sub
0
 
DanRollinsCommented:
Hi chnelson,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept AzraSound's comment(s) as an answer.

chnelson, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
NetminderCommented:
Per recommendation, force-accepted.

Netminder
CS Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now