dhj Posted July 27, 2004 Posted July 27, 2004 hi i need to change the background colour of a control (say textbox for a example) when it has gotfocus and change back to the original colour when lostfocus. (in a vb.net windows application) i know i can write subs for each and every control as follows Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus End Sub Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus End Sub but i don't want to do this since i have too much of controls in my form do i have any other option? Quote
Malfunction Posted July 27, 2004 Posted July 27, 2004 I the controls are of the same type you can derive from that control and add these two event handlers. Quote Debug me...
dhj Posted July 27, 2004 Author Posted July 27, 2004 but i have many types of controls how can i handle that ? Quote
Administrators PlausiblyDamp Posted July 27, 2004 Administrators Posted July 27, 2004 You could dynamically wire up the event handlers in your form load event like... Private Sub control_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Dim c As Control = DirectCast(sender, Control) 'handle the lost focus here End Sub Private Sub control_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Dim c As Control = DirectCast(sender, Control) 'handle got focus here End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For Each c As Control In Me.Controls AddHandler DirectCast(c, Control).GotFocus, AddressOf control_GotFocus AddHandler DirectCast(c, Control).LostFocus, AddressOf control_LostFocus Next End Sub Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
dhj Posted July 27, 2004 Author Posted July 27, 2004 hi PlausiblyDamp Thank you very much it is working fine but now my problem is when i click on a button it's changing the colour of the button too . how can i prevent this problem? Quote
Malfunction Posted July 27, 2004 Posted July 27, 2004 Don't know VB but here: For Each c As Control In Me.Controls AddHandler DirectCast(c, Control).GotFocus, AddressOf control_GotFocus AddHandler DirectCast(c, Control).LostFocus, AddressOf control_LostFocus Next you have to check wether the control is a button and only if it isn't add the event handler. in c#: foreach (Control c in this.Controls) { if (!(c is Button)) { //Add Handlers here } } Quote Debug me...
Administrators PlausiblyDamp Posted July 27, 2004 Administrators Posted July 27, 2004 For Each c As Control In Me.Controls If Not TypeOf c Is Button Then AddHandler DirectCast(c, Control).GotFocus, AddressOf control_GotFocus AddHandler DirectCast(c, Control).LostFocus, AddressOf control_LostFocus End If Next Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.