Solved

VB.net overrides Textbox.GotFocus event

Posted on 2001-08-24
12
1,636 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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
 
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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

624 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