hey guys i need to know how to get a text of a textbox "blued" if focus = true
... for not clicking twice.... i mean when you click, background of the text become blue and what you write is replaced with the text...(i dont know what that is called)
Printable View
hey guys i need to know how to get a text of a textbox "blued" if focus = true
... for not clicking twice.... i mean when you click, background of the text become blue and what you write is replaced with the text...(i dont know what that is called)
Use Enter and Leave event of a TextBox to alter the background color as shown here where there are three TextBox controls.
Code:Public Class Form3
Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles TextBox1.Enter, TextBox2.Enter, TextBox3.Enter
CType(sender, TextBox).BackColor = Color.Blue
End Sub
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles TextBox1.Leave, TextBox2.Leave, TextBox3.Leave
CType(sender, TextBox).BackColor = Color.FromName("Window")
End Sub
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
ActiveControl = TextBox3
End Sub
End Class
thanks for your answer but i dont think your code will help me but im not sure anyways....my most important goal was not coloring the background...it was the ability of changing a text with getting focus into that textbox and by pressing a button...
e.g: a text of textbox1 is "123" and i click to that textbox only once...and then press 5 once and the last text is not including "123" in it i hope you understand it and if your code does that im sorry(tell me if it works for that but it didnt seem to doing that).....i got to go ....
I think, this is what you are looking for (if I understand your question):
The SelectAll() of a TextBox will highlight all the text inside it(the blue background that you are referring to seems to be this highlighting). So, you could simply call this method in the Click() event of the TextBox.vb.net Code:
Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Click TextBox1.SelectAll() End Sub
Thus whenever you click on the TextBox, the Click() event is fired and this will call the TextBox's SelectAll() method.
Hope this helps :wave:
I don't think that handling the Click event is the answer. If you do that, whenever the user tries to click to place the caret within the text, all the text will be selected. It would make more sense to handle the Enter event, so that all the text is selected every time the control receives focus, by mouse or keyboard. That allows the user to click the text while the control already has focus to position the cursor. If the control already has focus and the user wants to select all the text then they just use Ctrl+A, which is the universal Windows shortcut for select all.
but i have a lot of textbox must have this procedure how can i make the code handles them all(cant write all of them almost 50 or 60)
Select them all in the designer, open the Properties window, click the Events button and then double-click the desired event. That will create one event handler with all controls in the Handles clause. You can also later select one or more controls and then select the existing event handler from the drop-down in the Properties window.
Code:Private Sub TextBox4_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox7.Enter, TextBox6.Enter, TextBox4.Enter, TextBox1.Enter, kS3.Enter, kS2.Enter, kS1.Enter, Cnc.Enter
'i couldnt figure out what to write here...???
End Sub
Code:Private Sub TextBox4_Enter(sender As Object, e As EventArgs) Handles TextBox4.Enter, TextBox3.Enter, TextBox2.Enter, TextBox1.Enter
Dim txt As TextBox = DirectCast(sender, TextBox)
txt.SelectAll()
End Sub