Button.Focus

ADO DOT NET

Centurion
Joined
Dec 20, 2006
Messages
160
I just cannot understand why it is not working?
It seems everything is alright:
Visual Basic:
    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.
 
So you mean I should change my code to something like this:
Visual Basic:
    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:
Visual Basic:
        If Me.OKButton.InvokeRequired Then
            Dim d As New SetFocusCallback(AddressOf SetFocus)
            Me.Invoke(d, New Object() {Focus})
        Else
is correct?
 
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})
 
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.
 
Try changing:

____Me.Invoke(d, New Object() {Focus})

to either

____Me.Invoke(d, New Object(0) {})

or

____Me.Invoke(d, Nothing)
 
Hi all,
I just have one more last question:
Visual Basic:
        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? :)
 
Visual Basic:
    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! :(
 
Back
Top