-1

i am making vb.net application in which there are 10 textbox in which i am changing background color when it got focus and lost focus. and adding validation number or character only. is there any way i can set or add custom code that every textbox added in form change color on got focus and lost focus and can assign textbox validation number only, alphanumeric. i don't want to add code on every event on keypress , gotfocus and lostfoucs. i just want to set it as default property of text box

here is my code

Private Sub txtProductDescc_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtProductDescc.GotFocus
    txtProductDescc.BackColor = interfaceDesign.gotFocusTxtColor
End Sub

Private Sub txtProductDescc_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtProductDescc.LostFocus
    txtProductDescc.BackColor = interfaceDesign.lostFocusTxtColor
End Sub
Allex
  • 141
  • 2
  • 16

1 Answers1

0

One way, which I am using myself often, is to make an array of textboxes. This way they all share the same "code" for every event and you can select them by index if you need to address a specific item.

Updated

You can also capture key events on the main form:

Community
  • 1
  • 1
Asons
  • 84,923
  • 12
  • 110
  • 165
  • yes but it makes designing very complicated. because controls are generate at run time – Allex Sep 12 '15 at 08:08
  • Yes they do at runtime and that is what you take advantage of ... check this answer for a simple "how-to": [Sample code](http://stackoverflow.com/a/5299986/2827823) – Asons Sep 12 '15 at 08:28
  • Actually all the answers in this question show more samples ... http://stackoverflow.com/questions/5299435/how-to-create-control-arrays-in-vb-net – Asons Sep 12 '15 at 08:32
  • thanks. i am adding controls panel inside panel . but when i looping mainPanel its only showing name of panel inside it not inside controls here is my code – Allex Sep 12 '15 at 08:50
  • For Each allControl As Control In containPanel.Controls If (allControl.GetType() Is GetType(Textbox)) Then MessageBox.Show(allControl.Name) End If Next – Allex Sep 12 '15 at 08:50
  • http://stackoverflow.com/questions/5299435/how-to-create-control-arrays-in-vb-net#comment5978844_5299590 – Asons Sep 12 '15 at 09:07