For text box...

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.

regards
Subhasis
mitra_subhasisAsked:
Who is Participating?
 
Ryan ChongConnect With a Mentor Commented:
Hi,

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

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

Private Sub Text1_GotFocus()
    SendKeys "{Home}+{End}"
End Sub
0
 
TimCotteeCommented:
Private Sub Text1_GotFocus()
 With Text1
  .SelStart = 0
  .SelLength = Len(.Text)
 End With
End Sub
0
 
sweetpillowCommented:
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
    Text1.SetFocus
End Sub

Cheers,
Sweetpillow :)
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
mitra_subhasisAuthor Commented:
That's very pretty...

Thanking you,'
Subhasis
0
 
TimCotteeCommented:
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.
0
 
TimCotteeCommented:
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!
0
All Courses

From novice to tech pro — start learning today.