473,545 Members | 2,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Textbox question

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
Apr 16 '07 #1
7 1585
Localbar,

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

Private Sub Textbox_GotFocu s(ByVal sender As Object, _
ByVal e As System.EventArg s) Handles txtName.GotFocu s, _
txtTest1.GotFoc us, _
txtTest2.GotFoc us, _
txtAverage.GotF ocus

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
Apr 16 '07 #2
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" <lo*********@gm ail.comwrote in message
news:u2******** ******@TK2MSFTN GP03.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


Apr 16 '07 #3
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" <Ro****@NoSpam. yah.none¦b¶l¥ó
news:dK******** *************** *******@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" <lo*********@gm ail.comwrote in message
news:u2******** ******@TK2MSFTN GP03.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


Apr 16 '07 #4
Thanks Kerry..Let me try now

"Kerry Moorman" <Ke**********@d iscussions.micr osoft.com¦b¶l¥ó
news:C0******** *************** ***********@mic rosoft.com ¤¤¼¶¼g...
Localbar,

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

Private Sub Textbox_GotFocu s(ByVal sender As Object, _
ByVal e As System.EventArg s) Handles txtName.GotFocu s, _
txtTest1.GotFoc us, _
txtTest2.GotFoc us, _
txtAverage.GotF ocus

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


Apr 16 '07 #5
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 AddEventHandler s(ByVal ctrlContainer As Control)
For Each ctrl As Control In ctrlContainer.C ontrols
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.HasChildre n Then
AddEventHandler s(ctrl)
End If
Next
End Sub
Private Sub ProcessEnter(By Val sender as Object, ByVal e as
System.EventArg s)
DirectCast(send er, Control).BackCo lor = Color.Lavender
End Sub
Private Sub ProcessLeave(By Val sender as Object, ByVal e as
System.EventArg s)
DirectCast(send er, Control).BackCo lor =
Color.FromKnown Color(KnownColo r.Window)
End Sub

Add the BaseForm_Load event and put this in there:

AddEventHandler s(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" <lo*********@gm ail.comwrote in message
news:u4******** ********@TK2MSF TNGP02.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" <Ro****@NoSpam. yah.none¦b¶l¥ó
news:dK******** *************** *******@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" <lo*********@gm ail.comwrote in message
news:u2******* *******@TK2MSFT NGP03.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




Apr 16 '07 #6
Thanks RobinS..It done.

"RobinS" <Ro****@NoSpam. yah.none¦b¶l¥ó
news:06******** *************** *******@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 AddEventHandler s(ByVal ctrlContainer As Control)
For Each ctrl As Control In ctrlContainer.C ontrols
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.HasChildre n Then
AddEventHandler s(ctrl)
End If
Next
End Sub
Private Sub ProcessEnter(By Val sender as Object, ByVal e as
System.EventArg s)
DirectCast(send er, Control).BackCo lor = Color.Lavender
End Sub
Private Sub ProcessLeave(By Val sender as Object, ByVal e as
System.EventArg s)
DirectCast(send er, Control).BackCo lor =
Color.FromKnown Color(KnownColo r.Window)
End Sub

Add the BaseForm_Load event and put this in there:

AddEventHandler s(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" <lo*********@gm ail.comwrote in message
news:u4******** ********@TK2MSF TNGP02.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" <Ro****@NoSpam. yah.none¦b¶l¥ó
news:dK******** *************** *******@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" <lo*********@gm ail.comwrote in message
news:u2******** ******@TK2MSFTN GP03.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





Apr 16 '07 #7
Good. Glad I could help.
Robin S.
--------------------------
"Localbar" <lo*********@gm ail.comwrote in message
news:Ob******** ******@TK2MSFTN GP05.phx.gbl...
Thanks RobinS..It done.

"RobinS" <Ro****@NoSpam. yah.none¦b¶l¥ó
news:06******** *************** *******@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 AddEventHandler s(ByVal ctrlContainer As Control)
For Each ctrl As Control In ctrlContainer.C ontrols
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.HasChildre n Then
AddEventHandler s(ctrl)
End If
Next
End Sub
Private Sub ProcessEnter(By Val sender as Object, ByVal e as
System.EventAr gs)
DirectCast(send er, Control).BackCo lor = Color.Lavender
End Sub
Private Sub ProcessLeave(By Val sender as Object, ByVal e as
System.EventAr gs)
DirectCast(send er, Control).BackCo lor =
Color.FromKnow nColor(KnownCol or.Window)
End Sub

Add the BaseForm_Load event and put this in there:

AddEventHandler s(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" <lo*********@gm ail.comwrote in message
news:u4******* *********@TK2MS FTNGP02.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" <Ro****@NoSpam. yah.none¦b¶l¥ó
news:dK******** *************** *******@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" <lo*********@gm ail.comwrote in message
news:u2******* *******@TK2MSFT NGP03.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




Apr 17 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
8376
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when submitting the form to update the database. The server doesn't have the client side value any more. It seems to me that as I begin to write the...
2
5397
by: Devinim ÞÖLEN | last post by:
Hi; I try to fetch data and show in the textbox as follows; .. .. .. .. <asp:XmlDataSource Id="m_ChXmlDataSource" runat="server" DataFile="http://localhost/ChHumanica/template/ChMenuItem.xml?Id=1">
3
1696
by: Henry | last post by:
Hi. I've also posted this at another discussion board and here is the original question. ------------------------- "I have this problem and I don't know what I can do. First of all, I have a page with and button, and 5 <asp:TextBox>'s and when an user makes changes to each of the textbox content, javascript client side code is...
2
2087
by: Jon | last post by:
Hello All, I have a datalist whose itemtemplate contains a label and a textbox. The label text is set on itemdatabound with the text, which is a question, from the database column. Each question has an associated textbox. I want the user to put a response to each question in the textbox, and click save. How do I get the values from the...
0
1380
by: jason | last post by:
Hello everyone. I am trying to write some custom command events into a DataGrid. The command that is currently giving me trouble is an "Add" custom command in the footer of a template column. Question #1: In the Add button Command Event I am trying to access the Text value of the TextBox in the other Footer columns. However, inside the...
1
1474
by: melanieab | last post by:
Hi, If there's a textbox and the text entered is longer than what's visible (the textbox length), how do you make it so that the beginning chunk of text is visible (instead of the last part of it)? Thanks again!!! Mel
13
3313
by: WALDO | last post by:
I have a .Net TextBox (TextBoxBase, really) in which I am appending about 20 lines of text per second. I use the AppendText() method to accomplish this. This is a great substitute for taking the Text property and concatenating it... Me.tb.Text &= newText ' Instead use Me.tb.AppendText(newText) ....ultimately setting the SelectionStart...
1
3200
by: rn5a | last post by:
I want to create a custom control that encapsulates a Button & a TextBox. When the Button is clicked, the user is asked a question using JavaScript confirm (which shows 2 buttons - 'OK' & 'Cancel'). Till this point, no problem. Initially, the TextBox is empty. The Button has a property named 'ConfirmMessage' so that the developer using this...
10
2448
by: garyusenet | last post by:
I have a multiline textbox. The size of the text box should be 75 characters wide, and 5 lines in height like this: - <---75 characters--> <---75 characters--> <---75 characters--> <---75 characters--> <---75 characters-->
4
3691
by: Randy | last post by:
My situation is that I have a form on which a number of textboxes and comboboxes are added dynamically based on interaction with the user. As these controls are added, they are given names based on how many times the user has added the control. For example, the first textbox is on the form at design time; its name is tbItem1. If the user...
0
7425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7682
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7449
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6009
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5069
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3479
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3465
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.