ADO DOT NET Posted February 9, 2007 Posted February 9, 2007 I just cannot understand why it is not working? It seems everything is alright: Delegate Sub SetFocusCallback(ByVal Focus As Boolean) Private Sub SetFocus(ByVal Focus As Boolean) If Me.OKButton.InvokeRequired Then Dim d As New SetFocusCallback(AddressOf SetFocus) Me.Invoke(d, New Object() {Focus}) Else Me.OKButton.Focus = Focus End If End Sub ... Me.SetFocus(True) ... Also in this line: Me.OKButton.Focus = Focus I get this error: Error 1 Expression is a value and therefore cannot be the target of an assignment. Quote
Administrators PlausiblyDamp Posted February 9, 2007 Administrators Posted February 9, 2007 Focus is a method not a property, try Me.OKButton.Focus() Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
ADO DOT NET Posted February 9, 2007 Author Posted February 9, 2007 So you mean I should change my code to something like this: Delegate Sub SetFocusCallback() Private Sub SetFocus() If Me.OKButton.InvokeRequired Then Dim d As New SetFocusCallback(AddressOf SetFocus) Me.Invoke(d, New Object() {Focus}) Else Me.OKButton.Focus() End If End Sub ... usage ... Me.SetFocus() I just am not sure if this section: If Me.OKButton.InvokeRequired Then Dim d As New SetFocusCallback(AddressOf SetFocus) Me.Invoke(d, New Object() {Focus}) Else is correct? Quote
ADO DOT NET Posted February 9, 2007 Author Posted February 9, 2007 Cross-thread operation not valid: Control 'SupportForm' accessed from a thread other than the thread it was created on. for this line: Me.Invoke(d, New Object() {Focus}) Quote
Leaders snarfblam Posted February 9, 2007 Leaders Posted February 9, 2007 What is "Focus" in the line: Me.Invoke(d, New Object() {Focus}) If it is not a declared variable then it will refer to the form's focus method, in which case you may be inadvertently invoking it, which you would be doing across threads, hence your error. Quote [sIGPIC]e[/sIGPIC]
ADO DOT NET Posted February 10, 2007 Author Posted February 10, 2007 So sorry for replying again! So how should I update it? It didn't work for me:( Quote
Leaders snarfblam Posted February 11, 2007 Leaders Posted February 11, 2007 Try changing: ____Me.Invoke(d, New Object() {Focus}) to either ____Me.Invoke(d, New Object(0) {}) or ____Me.Invoke(d, Nothing) Quote [sIGPIC]e[/sIGPIC]
ADO DOT NET Posted February 11, 2007 Author Posted February 11, 2007 What about: Me.Invoke(d) Which one do you prefer? Quote
MrPaul Posted February 11, 2007 Posted February 11, 2007 Me.Invoke(d) Me.Invoke(d) That is correct (since the delegate takes no arguments). Good luck :cool: Quote Never trouble another for what you can do for yourself.
ADO DOT NET Posted February 12, 2007 Author Posted February 12, 2007 Just wanted to thank you all for your helps, I have learned a lot from you:) Quote
ADO DOT NET Posted February 14, 2007 Author Posted February 14, 2007 Hi all, I just have one more last question: If SaveCheckBox.Checked = True Then SaveButton.Enabled = True I just wanna check if my check box on form is checked then enable a button in my safe thread. But could not implement this. Can someone help me? :) Quote
ADO DOT NET Posted February 14, 2007 Author Posted February 14, 2007 Delegate Sub SetFocusCallback() Private Sub SetFocus() If Me.SaveCheckBox.InvokeRequired Then Dim d As New SetFocusCallback(AddressOf Enabled) Me.Invoke(d, New Object() {Enabled}) Else If Me.SaveCheckBox.Enabled = True Then SaveButton.Enabled = True End If End Sub It won't work! :( Quote
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.