Click here to Skip to main content
15,900,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team, i am trying to write an event in vb.net which will fire when the focus of control changes from one to another. I have form with 7 attributes for 15 users ( which includes text box and combo box as well), using this form i am entering user details to the DB. Now for the validation purpose i need to write an event that checks if the same data already exists in the DB, and it will show a label next to the control, hence i need to write event that will fire as soon as the focus from the control shifts to the other control. I am new to vb.net.Please help.
Thanks in advance
Posted

You simply add the Enter/Leave event handler of all the controls to the specific handler.

VB
for each AControl in Me.Controls 
        If TypeOf AControl Is TextBox Or _
           TypeOf AControl Is ComboBox Or _
           TypeOf AControl Is DateTimePicker Or _
           TypeOf AControl Is NumericUpDown Or _
           TypeOf AControl Is MaskedTextBox Then
            AddHandler AControl.Enter, AddressOf Control_Enter
            AddHandler AControl.Leave, AddressOf Control_Leave
        End If
Next


Good luck!
 
Share this answer
 
v2
Comments
souvikd 2-Aug-10 11:30am    
i didnt understand to which handler i should add this code, please explain
E.F. Nijboer 2-Aug-10 12:24pm    
The most easy way is to simply put a TextBox on a form and create an Enter and Leave handler using the editor. You will get two handlers called TextBox1_Enter and TextBox1_Leave. Use these two handlers instead of the Control_Enter and Control_Leave.
You can do the Enter and Leave as was suggested in the other box, or you can just use the LostFocus event.

If I were writing something similar to what you were describing, I would expect it to look something like this:

VB
Private Sub Controls_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) _
      Handles TextBox1.LostFocus, TextBox2.LostFocus, ComboBox1.LostFocus
  Using dbConn As New OleDb.OleDbConnection("some connection string")
    Dim cmd As New OleDb.OleDbCommand("SELECT * FROM Table WHERE User=? ")
    cmd.Parameters.Add(userName)

    Select Case CType(sender, Control).Name
        Case TextBox1.Name
            cmd.CommandText = cmd.CommandText & "TextBox1Attribute=?"
            cmd.Parameters.Add(TextBox1.Text)
        Case TextBox2.Name
            cmd.CommandText = cmd.CommandText & "TextBox2Attribute=?"
            cmd.Parameters.Add(TextBox2.Text)
        Case ComboBox1.Name
            cmd.CommandText = cmd.CommandText & "ComboBox1Attribute=?"
            cmd.Parameters.Add(ComboBox1.Text)
    End Select

    If Not IsDBNull(cmd.ExecuteScalar) Then
        'add the image indicating that this value already exists.
    End If    
  End Using
End Sub


[E.F. noted a problem. I actually started with:
VB
If TypeOf sender Is TextBox Then
  CType(sender, TextBox)...
End If

and changed it because I needed to include ComboBoxes as well and didn't change the CType line.
 
Share this answer
 
v2
Comments
E.F. Nijboer 2-Aug-10 14:13pm    
My first idea would be to use the LostFocus also except the reason for the use of the Enter and Leave events is because the e parameter with the System.EventArgs is a complete bogus variable. For some controls it is impossible to get the previous value of the control so there is absolutely no way to find out if it has changed or not. I came across it when working on an UndoRedoFramework I wrote.
http://www.codeproject.com/KB/vb/UndoRedoFramework.aspx

It was kind of a downer that no event gave the previous value because it would be very helpful. The problem that would arise from this would be that values will be updated to the database, also when they aren't changed. The same I encountered with the framework otherwise storing to many bogus undo commands.

By the way, you cast CType(sender, TextBox) but would this isn't really typesafe. CType(sender, Control) would be more safe and can also be used in a generic kind of way because most of the descendants of Control support the Text property and deliver the actual value as text. In my framework I use that mechanism for the SimpleControlMonitor and attach handlers of these simple controls and handle them all at once.

Well, just would like you to know that's the reason for choose the Enter end Leave handlers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900