Textbox Class question

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

I'm looking to create multiple textboxs that change to a light blue on
focus. What is the most efficient way to do this?



-Peter
 
I assume you are talking about a Windows Forms application, right?

Public Sub ChangeTextBoxColor(ByVal Sender as System.Object, e as EventArgs)
_
Handles Textbox1.Enter, Textbox2.Enter, Textbox3.Enter, etc., etc.

Dim theTB as TextBox = CType(sender, Textbox)
theTB.BackColor = Color.Blue

End Sub
 
I agree - or you could add the other text boxes to one of the boxes 'enter'
event - in the handles clause.
I still miss those control arrays - and Recordset -
 
Hal,

There are a lot of control arrays in VBNet.

I have a complete sample using it, I have pasted it bellow. It looks very
much your enter leave problem. Just copy it to a new project and add 2
buttons and a textbox on the form. It is hoovering a button, however for a
textbox it goes the same.

This is one approacht of using an array of controls in VBNet. However you
can as well use for this the control.collection.

However the behaviour of control arrays has changed from VB6 what was a
little bit strange approach. (More a table of all controls of a certaintype
than a control array, how you would deal with that when you by instance
inherit from a control to make an almost the same look alike).

About the dataset you will probably in future always hope that you don't
ever have to use the recordset again.

(The sample from Scott is very good, it is the same as this just an other
approach using the array for setting the handlers, which is here for 2
buttons, however could be a bunch)

I hope this gives some ideas?

Cor

\\\needs two buttons and a label on a form
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim btnArea As Button() = New Button() {Button1, Button2}
For Each btn As Button In btnArea
AddHandler btn.MouseLeave, AddressOf Button_MouseLeave
AddHandler btn.MouseEnter, AddressOf Button_MouseEnter
Next
End Sub
Private Sub Button_MouseLeave(ByVal sender As Object, _
ByVal e As System.EventArgs)
DirectCast(sender, Button).BackColor = Color.Black
Me.Label1.Text = ""
End Sub
Private Sub Button_MouseEnter(ByVal sender As Object, _
ByVal e As System.EventArgs)
DirectCast(sender, Button).BackColor = Color.Red
Me.Label1.Text = DirectCast(sender, Button).Name
End Sub
End Class
///
 
Peter said:
I'm looking to create multiple textboxs that change to a light blue on
focus. What is the most efficient way to do this?

Create a UserControl and change it to inherit directly from TextBox,
then pop code in the OnGotFocus() routine, as in

[MyTextBox.vb]
Public Class MyTextBox
Inherits System.Windows.Forms.TextBox

Public Overrides Sub OnGotFocus( ...
MyBase.OnGotFocus( ...
Me.BackColor = ...
End Sub

End Class

OK, getting it into the Toolbox to use in other applications is fiddly, but
the code's there and ready to roll ...

HTH,
Phill W.
 
Why bother with that when regular textboxes already expose an "Enter" event
for receiving focus? Also, you didn't address the real question, which was
the most effective way to have several textboxes and handle the gotFocus of
any of them to change the particular one's back color.


Phill. W said:
Peter said:
I'm looking to create multiple textboxs that change to a light blue on
focus. What is the most efficient way to do this?

Create a UserControl and change it to inherit directly from TextBox,
then pop code in the OnGotFocus() routine, as in

[MyTextBox.vb]
Public Class MyTextBox
Inherits System.Windows.Forms.TextBox

Public Overrides Sub OnGotFocus( ...
MyBase.OnGotFocus( ...
Me.BackColor = ...
End Sub

End Class

OK, getting it into the Toolbox to use in other applications is fiddly, but
the code's there and ready to roll ...

HTH,
Phill W.
 
Amazingly enough, I found a article in the knowledgebase that was
written in VB speak not C# speak. LOL.

I created a class in a vb.net application template named Bluetextbox.
At the top I wrote inherits textbox
I compiled the program.

The I added the class to my control box by simply browsing to the new
exe. It took like 1 minute. Making controls in vb.net is so easy
now.

Thank god, now I don't have to type handles...80 million times.

http://msdn.microsoft.com/library/d...y/en-us/dnwinforms/html/custcntrlsampover.asp
 
Peter,

When you had looked to the approach I took it was as well only creating in a
few lines of code creating all the handles for 80 textboxes.

While it is in the approach from Scott better when you have only some
textboxes.

However that does not mean that your approach is wrong.
When that fits you better take that.

Cor
 
Thanks for the help. I have been reading book after book on intuitive
user app design and now I'm trying to impliment some of the lessons I
have learned. Thanks for all the posts. This newsgroup audience is
awesome.
 
Cor,
Thanks - useful info and tip -
Hal

Cor Ligthert said:
Hal,

There are a lot of control arrays in VBNet.

I have a complete sample using it, I have pasted it bellow. It looks very
much your enter leave problem. Just copy it to a new project and add 2
buttons and a textbox on the form. It is hoovering a button, however for a
textbox it goes the same.

This is one approacht of using an array of controls in VBNet. However you
can as well use for this the control.collection.

However the behaviour of control arrays has changed from VB6 what was a
little bit strange approach. (More a table of all controls of a certaintype
than a control array, how you would deal with that when you by instance
inherit from a control to make an almost the same look alike).

About the dataset you will probably in future always hope that you don't
ever have to use the recordset again.

(The sample from Scott is very good, it is the same as this just an other
approach using the array for setting the handlers, which is here for 2
buttons, however could be a bunch)

I hope this gives some ideas?

Cor

\\\needs two buttons and a label on a form
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim btnArea As Button() = New Button() {Button1, Button2}
For Each btn As Button In btnArea
AddHandler btn.MouseLeave, AddressOf Button_MouseLeave
AddHandler btn.MouseEnter, AddressOf Button_MouseEnter
Next
End Sub
Private Sub Button_MouseLeave(ByVal sender As Object, _
ByVal e As System.EventArgs)
DirectCast(sender, Button).BackColor = Color.Black
Me.Label1.Text = ""
End Sub
Private Sub Button_MouseEnter(ByVal sender As Object, _
ByVal e As System.EventArgs)
DirectCast(sender, Button).BackColor = Color.Red
Me.Label1.Text = DirectCast(sender, Button).Name
End Sub
End Class
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top