Board index » Visual Studio » Textbox question

Textbox question

Visual Studio65
Hi all,



In my form have more then 10 textbox. I would like to make all textbox

when lostfocus backcolor is white, when gotfocus backcolor is yellow. But I

don't want to write same code in the form. How to solve this problem. Is it

can make a textbox class to control or some other method?



Thanks


-
 

Re:Textbox question

Localbar,



Use one event handler for all the textboxs's Gotfocus and Lostfocus events.

For example:



Private Sub Textbox_GotFocus(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles txtName.GotFocus, _

txtTest1.GotFocus, _

txtTest2.GotFocus, _

txtAverage.GotFocus



Kerry Moorman





"Localbar" wrote:



Quote
Hi all,



In my form have more then 10 textbox. I would like to make all textbox

when lostfocus backcolor is white, when gotfocus backcolor is yellow. But I

don't want to write same code in the form. How to solve this problem. Is it

can make a textbox class to control or some other method?



Thanks







-

Re:Textbox question

Create a base form that has no UI, all it does it loop through the controls

on the form and set the event handlers. I would recommend using Enter and

Leave instead of the Focus events, by the way.



Then have all of your forms inherit from the base form, and it will behave

the same all over the place.



Robin S.

--------------------------------

"Localbar" <localbar.hk@gmail.com>wrote in message

Quote
Hi all,



In my form have more then 10 textbox. I would like to make all

textbox

when lostfocus backcolor is white, when gotfocus backcolor is yellow. But

I

don't want to write same code in the form. How to solve this problem. Is

it

can make a textbox class to control or some other method?



Thanks









-

Re:Textbox question

Hi Robin,



Can tell me more detail, After I create the form is no UI. and then how

to do? ..No need to add textbox in baseform ?



Thanks



"RobinS" <RobinS@NoSpam.yah.none>¦b¶l¥ó

Quote
Create a base form that has no UI, all it does it loop through the

controls

on the form and set the event handlers. I would recommend using Enter and

Leave instead of the Focus events, by the way.



Then have all of your forms inherit from the base form, and it will behave

the same all over the place.



Robin S.

--------------------------------

"Localbar" <localbar.hk@gmail.com>wrote in message

news:u2uoZO9fHHA.4692@TK2MSFTNGP03.phx.gbl...

>Hi all,

>

>In my form have more then 10 textbox. I would like to make all

>textbox

>when lostfocus backcolor is white, when gotfocus backcolor is yellow.

But

>I

>don't want to write same code in the form. How to solve this problem. Is

>it

>can make a textbox class to control or some other method?

>

>Thanks

>

>









-

Re:Textbox question

Thanks Kerry..Let me try now



"Kerry Moorman" <KerryMoorman@discussions.microsoft.com>¦b¶l¥ó

Quote
Localbar,



Use one event handler for all the textboxs's Gotfocus and Lostfocus

events.

For example:



Private Sub Textbox_GotFocus(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles txtName.GotFocus, _

txtTest1.GotFocus, _

txtTest2.GotFocus, _

txtAverage.GotFocus



Kerry Moorman





"Localbar" wrote:



>Hi all,

>

>In my form have more then 10 textbox. I would like to make all

textbox

>when lostfocus backcolor is white, when gotfocus backcolor is yellow.

But I

>don't want to write same code in the form. How to solve this problem. Is

it

>can make a textbox class to control or some other method?

>

>Thanks

>

>

>





-

Re:Textbox question

Create a form and call it something like BaseForm. Don't put any controls

on it.



Put this in the code behind the form:



Private Sub AddEventHandlers(ByVal ctrlContainer As Control)

For Each ctrl As Control In ctrlContainer.Controls

If TypeOf ctrl Is TextBox _

OrElse TypeOf ctrl Is ComboBox Then

AddHandler ctrl.Enter, AddressOf ProcessEnter

AddHandler ctrl.Leave, AddressOf ProcessLeave

End If

'If control has children, call this function recursively

If ctrl.HasChildren Then

AddEventHandlers(ctrl)

End If

Next

End Sub

Private Sub ProcessEnter(ByVal sender as Object, ByVal e as

System.EventArgs)

DirectCast(sender, Control).BackColor = Color.Lavender

End Sub

Private Sub ProcessLeave(ByVal sender as Object, ByVal e as

System.EventArgs)

DirectCast(sender, Control).BackColor =

Color.FromKnownColor(KnownColor.Window)

End Sub



Add the BaseForm_Load event and put this in there:



AddEventHandlers(me)



Then create your new form, and either change it to inherit from BaseForm

(this is probably in the designer-generated code), or use the Inheritance

Picker to create your new form (which does the same thing).



Put your textboxes on your new form, and run it, and voila! You get colors

when you enter a textbox, and back to the original color when they leave.



If you have every form inherit from the BaseForm, it will work the same on

every form with no further effort on your part.



Robin S.

-----------------------------

"Localbar" <localbar.hk@gmail.com>wrote in message

Quote
Hi Robin,



Can tell me more detail, After I create the form is no UI. and then

how

to do? ..No need to add textbox in baseform ?



Thanks



"RobinS" <RobinS@NoSpam.yah.none>¦b¶l¥ó

news:dKmdndWW5MXqlL7bnZ2dnUVZ_rSjnZ2d@comcast.com ¤¤¼¶¼g...

>Create a base form that has no UI, all it does it loop through the

controls

>on the form and set the event handlers. I would recommend using Enter

>and

>Leave instead of the Focus events, by the way.

>

>Then have all of your forms inherit from the base form, and it will

>behave

>the same all over the place.

>

>Robin S.

>--------------------------------

>"Localbar" <localbar.hk@gmail.com>wrote in message

>news:u2uoZO9fHHA.4692@TK2MSFTNGP03.phx.gbl...

>>Hi all,

>>

>>In my form have more then 10 textbox. I would like to make all

>>textbox

>>when lostfocus backcolor is white, when gotfocus backcolor is yellow.

But

>>I

>>don't want to write same code in the form. How to solve this problem.

>>Is

>>it

>>can make a textbox class to control or some other method?

>>

>>Thanks

>>

>>

>

>









-

Re:Textbox question

Thanks RobinS..It done.



"RobinS" <RobinS@NoSpam.yah.none>¦b¶l¥ó

Quote
Create a form and call it something like BaseForm. Don't put any controls

on it.



Put this in the code behind the form:



Private Sub AddEventHandlers(ByVal ctrlContainer As Control)

For Each ctrl As Control In ctrlContainer.Controls

If TypeOf ctrl Is TextBox _

OrElse TypeOf ctrl Is ComboBox Then

AddHandler ctrl.Enter, AddressOf ProcessEnter

AddHandler ctrl.Leave, AddressOf ProcessLeave

End If

'If control has children, call this function recursively

If ctrl.HasChildren Then

AddEventHandlers(ctrl)

End If

Next

End Sub

Private Sub ProcessEnter(ByVal sender as Object, ByVal e as

System.EventArgs)

DirectCast(sender, Control).BackColor = Color.Lavender

End Sub

Private Sub ProcessLeave(ByVal sender as Object, ByVal e as

System.EventArgs)

DirectCast(sender, Control).BackColor =

Color.FromKnownColor(KnownColor.Window)

End Sub



Add the BaseForm_Load event and put this in there:



AddEventHandlers(me)



Then create your new form, and either change it to inherit from BaseForm

(this is probably in the designer-generated code), or use the Inheritance

Picker to create your new form (which does the same thing).



Put your textboxes on your new form, and run it, and voila! You get colors

when you enter a textbox, and back to the original color when they leave.



If you have every form inherit from the BaseForm, it will work the same on

every form with no further effort on your part.



Robin S.

-----------------------------

"Localbar" <localbar.hk@gmail.com>wrote in message

news:u4nkVp%23fHHA.5056@TK2MSFTNGP02.phx.gbl...

>Hi Robin,

>

>Can tell me more detail, After I create the form is no UI. and then

>how

>to do? ..No need to add textbox in baseform ?

>

>Thanks

>

>"RobinS" <RobinS@NoSpam.yah.none>¦b¶l¥ó

>news:dKmdndWW5MXqlL7bnZ2dnUVZ_rSjnZ2d@comcast.com ¤¤¼¶¼g...

>>Create a base form that has no UI, all it does it loop through the

>controls

>>on the form and set the event handlers. I would recommend using Enter

>>and

>>Leave instead of the Focus events, by the way.

>>

>>Then have all of your forms inherit from the base form, and it will

>>behave

>>the same all over the place.

>>

>>Robin S.

>>--------------------------------

>>"Localbar" <localbar.hk@gmail.com>wrote in message

>>news:u2uoZO9fHHA.4692@TK2MSFTNGP03.phx.gbl...

>>>Hi all,

>>>

>>>In my form have more then 10 textbox. I would like to make all

>>>textbox

>>>when lostfocus backcolor is white, when gotfocus backcolor is yellow.

>But

>>>I

>>>don't want to write same code in the form. How to solve this problem.

>>>Is

>>>it

>>>can make a textbox class to control or some other method?

>>>

>>>Thanks

>>>

>>>

>>

>>

>

>









-

Re:Textbox question

Good. Glad I could help.

Robin S.

--------------------------

"Localbar" <localbar.hk@gmail.com>wrote in message

Quote
Thanks RobinS..It done.



"RobinS" <RobinS@NoSpam.yah.none>¦b¶l¥ó

news:06qdnXR1heRru77bnZ2dnUVZ_gGdnZ2d@comcast.com ¤¤¼¶¼g...

>Create a form and call it something like BaseForm. Don't put any

>controls

>on it.

>

>Put this in the code behind the form:

>

>Private Sub AddEventHandlers(ByVal ctrlContainer As Control)

>For Each ctrl As Control In ctrlContainer.Controls

>If TypeOf ctrl Is TextBox _

>OrElse TypeOf ctrl Is ComboBox Then

>AddHandler ctrl.Enter, AddressOf ProcessEnter

>AddHandler ctrl.Leave, AddressOf ProcessLeave

>End If

>'If control has children, call this function recursively

>If ctrl.HasChildren Then

>AddEventHandlers(ctrl)

>End If

>Next

>End Sub

>Private Sub ProcessEnter(ByVal sender as Object, ByVal e as

>System.EventArgs)

>DirectCast(sender, Control).BackColor = Color.Lavender

>End Sub

>Private Sub ProcessLeave(ByVal sender as Object, ByVal e as

>System.EventArgs)

>DirectCast(sender, Control).BackColor =

>Color.FromKnownColor(KnownColor.Window)

>End Sub

>

>Add the BaseForm_Load event and put this in there:

>

>AddEventHandlers(me)

>

>Then create your new form, and either change it to inherit from BaseForm

>(this is probably in the designer-generated code), or use the

>Inheritance

>Picker to create your new form (which does the same thing).

>

>Put your textboxes on your new form, and run it, and voila! You get

>colors

>when you enter a textbox, and back to the original color when they

>leave.

>

>If you have every form inherit from the BaseForm, it will work the same

>on

>every form with no further effort on your part.

>

>Robin S.

>-----------------------------

>"Localbar" <localbar.hk@gmail.com>wrote in message

>news:u4nkVp%23fHHA.5056@TK2MSFTNGP02.phx.gbl...

>>Hi Robin,

>>

>>Can tell me more detail, After I create the form is no UI. and then

>>how

>>to do? ..No need to add textbox in baseform ?

>>

>>Thanks

>>

>>"RobinS" <RobinS@NoSpam.yah.none>¦b¶l¥ó

>>news:dKmdndWW5MXqlL7bnZ2dnUVZ_rSjnZ2d@comcast.com ¤¤¼¶¼g...

>>>Create a base form that has no UI, all it does it loop through the

>>controls

>>>on the form and set the event handlers. I would recommend using Enter

>>>and

>>>Leave instead of the Focus events, by the way.

>>>

>>>Then have all of your forms inherit from the base form, and it will

>>>behave

>>>the same all over the place.

>>>

>>>Robin S.

>>>--------------------------------

>>>"Localbar" <localbar.hk@gmail.com>wrote in message

>>>news:u2uoZO9fHHA.4692@TK2MSFTNGP03.phx.gbl...

>>>>Hi all,

>>>>

>>>>In my form have more then 10 textbox. I would like to make all

>>>>textbox

>>>>when lostfocus backcolor is white, when gotfocus backcolor is

>>>>yellow.

>>But

>>>>I

>>>>don't want to write same code in the form. How to solve this

>>>>problem.

>>>>Is

>>>>it

>>>>can make a textbox class to control or some other method?

>>>>

>>>>Thanks

>>>>

>>>>

>>>

>>>

>>

>>

>

>









-