Solved

VB.net overrides Textbox.GotFocus event

Posted on 2001-08-24
12
1,604 Views
Last Modified: 2013-11-25
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
Comment
Question by:chnelson
12 Comments
 
LVL 2

Expert Comment

by:Jagar
ID: 6422543
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
 
LVL 1

Author Comment

by:chnelson
ID: 6422814
i am also using beta 2. Which property are you using to making those textbox automatically highlight all text while got focus?
0
 
LVL 2

Expert Comment

by:Jagar
ID: 6423287
None I just dropped a textbox on a form and highighted all of the text when I entered the textbox automatically
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 100 total points
ID: 6424020
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
 
LVL 1

Author Comment

by:chnelson
ID: 6424396
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
 
LVL 2

Expert Comment

by:Jagar
ID: 6424644
I'm not sure.  Let me try another project and see if it happens agains.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Expert Comment

by:Jagar
ID: 6424670
   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
 
LVL 1

Author Comment

by:chnelson
ID: 6424846
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
 
LVL 1

Author Comment

by:chnelson
ID: 6496127
no suitable answer replied
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 7029516
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 7202511
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
 
LVL 5

Expert Comment

by:Netminder
ID: 7216005
Per recommendation, force-accepted.

Netminder
CS Moderator
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now