For text box...

Posted on 2002-04-15
Last Modified: 2010-05-02
Suppose I have a textbox control, say Text1. Whenever I focus on the textbox the text will automatically selected(ie. in windows it will be hilighted in blue) and whenever I write on it it deletes the previous content.
Please write some code.

Question by:mitra_subhasis
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
LVL 52

Accepted Solution

Ryan Chong earned 10 total points
ID: 6941443

Use SendKeys "{Home}+{End}", example:

Private Sub Text1_Click()
    SendKeys "{Home}+{End}"
End Sub

Private Sub Text1_GotFocus()
    SendKeys "{Home}+{End}"
End Sub
LVL 43

Expert Comment

ID: 6941445
Private Sub Text1_GotFocus()
 With Text1
  .SelStart = 0
  .SelLength = Len(.Text)
 End With
End Sub

Expert Comment

ID: 6941527
Hi mitra,

  Try the following code. Suppose I have a text box in the form1 name text1.

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Text1.BackColor = vbWhite
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    Text1.Text = ""
End Sub

Private Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Text1.BackColor = vbBlue
End Sub

Sweetpillow :)
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 6941534
That's very pretty...

Thanking you,'
LVL 43

Expert Comment

ID: 6941536
sweetpillow, it would indeed work, however you would need to reset the colours in the KeyDown event otherwise it will still stay white on blue.

Anyway the snippet I posted achieves everything required with less code. ryancys's suggestion would also work but sendkeys is not necessarily the most reliable method.
LVL 43

Expert Comment

ID: 6941538
Ah well mitra_subhasis, you chose a solution which is your right, I would stand by my last comment that sendkeys is not necessarily the MOST reliable way to achieve this when you can do it more simply with the suggestion I gave!

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

734 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