I have a form with 20 controls on it. I want the on focus event to fire when one of the 'data' controls get the focus. (combo, Textbox, Checkbox)
Please have a look at the code and see if you can spot why the event is not firing when any of the controls get the focus.
Thank you.
Private Sub getDataValues(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateBirth.GotFocus, Address1.GotFocus, Address2.GotFocus, _ Town.GotFocus, City.GotFocus, PostCode.GotFocus, cboMemberSection.GotFocus, cboMembershipType.GotFocus, _ cboMembershipType.GotFocus, chkActive.GotFocus, chkFeeEarner.GotFocus, CurrentGrade.GotFocus, Date_First_Joined.GotFocus, _ EC1_Mobile.GotFocus, EC1_Name.GotFocus, EC1_Number.GotFocus, EC1_Relationship.GotFocus, EC2_Mobile.GotFocus, _ EC2_Name.GotFocus, EC2_Number.GotFocus, EC2_Relationship.GotFocus, Email.GotFocus, FirstNM.GotFocus, Gender.GotFocus, _ LastNM.GotFocus, Mobile.GotFocus, PostCode.GotFocus, Telephone.GotFocus '================================================================================================================= 'Description: Fires when any of the handled controls gets the focus ' The current value in the control is passed into the controls tag property for retrieval later on if ' the user clicks the 'undo' button on the form. 'Author: 'Date: 'Revised By: 'Last Revised: '================================================================================================================= 'Dim ctl As System.Windows.Forms.Control If UpdatingCombos = True Or LoadEvent = True Then 'stop this running if the form is loading up the sellected record Exit Sub Else Dim ctlVal As String = "" Dim ctlType As String = "" Dim cbo As ComboBox Dim txt As TextBox Dim chk As CheckBox Dim dte As DateTimePicker Dim sType As String = sender.GetType.ToString Dim sName As String = "" Dim intLen As Integer = (Len(sender.GetType.ToString) - 21) sType = sType.Substring(21, intLen) Select Case sType Case "TextBox" txt = DirectCast(sender, TextBox) sName = txt.Name ctlVal = txt.Text txt.Tag = ctlVal Case "ComboBox" cbo = DirectCast(sender, ComboBox) sName = cbo.Name ctlVal = cbo.SelectedText Case "checkBox" chk = DirectCast(sender, CheckBox) sName = chk.Name ctlVal = chk.Checked Case "DateTimePicker" dte = DirectCast(sender, DateTimePicker) sName = dte.Name ctlVal = dte.Value End Select LoadEvent = False btnClear.Enabled = True End If End Sub
Instead of that ugly hack with the GetType.Tostring, I would suggest a more direct If .. ElseIf structure to handle the detection of the type of control. It is possible that your control type string prefix is no longer exactly 21 characters if there was a change of project name or namespace.
If TypeOf (sender) Is TextBox Then txt = DirectCast(sender, TextBox) sName = txt.Name ctlVal = txt.Text txt.Tag = ctlVal ElseIf TypeOf (sender) Is ComboBox Then cbo = DirectCast(sender, ComboBox) sName = cbo.Name ctlVal = cbo.SelectedText ElseIf TypeOf (sender) Is CheckBox Then chk = DirectCast(sender, CheckBox) sName = chk.Name ctlVal = chk.Checked ElseIf TypeOf (sender) Is DateTimePicker Then dte = DirectCast(sender, DateTimePicker) sName = dte.Name ctlVal = dte.Value End If
Your lofty and rather curt opinion of my code is duly noted........Perhaps you would now be good enough to use your expertise and have a go at answering my question - which was why my ugly hack code never fires. It does not raise errors - it just does not fire.
You may be a VB genuis, but you clearly lack any social skills. Re-read your comment and consider why your answer might just put a beginner off. I am just learning ,,,,do you get it, or are you such a genuis do you not remember that far back?
Didn't mean to get your hackles up. I've written my share of ugly hacks over the years, and really was not being judgmental, and sorry if I came across as harsh.
Since you never stated how you know that the event is not firing, I was offering instead that maybe it is firing, but the case statements are not being hit because the sType string is not picking up the right string. You can put a breakpoint on the first line that reads "If UpdatingCombos = True ... " to determine for sure that your code is not firing. (You should also confirm that that condition allows the subsequent code to run if the break point actually does get hit.)
Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!
Apology happily accepted..It can get frustrating being a beginner.
Anyway, have run the code with a breakpoint in place and no joy. That was my first port of call, so I knew it was not the code causing an unexpected outcome.
I thought there may be a limit on the number of controls that can be handled by a procedure or something like that that I had missed.
To be honest I cannot find any articles or book chapters that are clear on the whole event, event raising and event handling business in VB.Net and I am therefore disadvantaged in terms on tracking what may be wrong.
Do you know of any good, clear ABC type type articles on events? Maybe after reading those and understanding, I may be able to solve the problem myself.
>I thought there may be a limit on the number of controls that can be handled by a procedure or something like that that I had missed.
I'm not aware of any, and I can do 20 controls without any difficulties in a sample form.
MS suggests that you use GotFocus only for UICues, and suggests using the Enter event instead:
"Note The GotFocus and LostFocus events are low-level focus events that are tied to the WM_KILLFOCUS and WM_SETFOCUS Windows messages. Typically, the GotFocus and LostFocus events are only used when updating UICues or when writing custom controls. Instead the Enter and Leave events should be used for all controls except the Form class, which uses the Activated and Deactivate events."
If there is something interfering with the messages being sent to the window, then you would see interference with this event (and likely with other UI features, like repainting.) Otherwise, I'm not sure what it could be. I would try the Enter event as a possible workaround.
I've used multiple controls in the "Handles" clause of the event handler before, usually with a smallish number of controls. I think when I have a large number of controls with a common event handler, I'm more likely to wire them up with an AddHandler statement. (I would be surprised if it will make a difference to your problem however.)
Thanks for the information, I'll do some research on the points you mentioned
Not the solution you were looking for?
IT issues often require a personalized solution. With Ask the Experts™, submit your questions to our certified professionals and receive unlimited, customized solutions that work for you.
Premium Content
You need an Expert Office subscription to comment.Start Free Trial