Jordan Matas
Jordan Matas

Reputation: 112

Wait for function to end VB

I'm starting a ne project, in VB. And I have a problem. So maybe I don't understand the logic - can you explain it to me?

In my function Feuil1_BeforeDoubleClick i would like to wait for Button1_Clickto end. But i don't know how to achieve this.

Here's the relevant code:

My Sheet1 :

 Imports System.Threading.Tasks
 Imports Microsoft.Office.Interop.Excel

Public Class Feuil1

Friend actionsPane1 As New ActionsPaneControl1
Public list As String


Public Sub Feuil1_BeforeDoubleClick(Target As Range, ByRef Cancel As Boolean) Handles Me.BeforeDoubleClick
    If Target.Column <> 1 Then
        If Target.Row = 16 Then
            Globals.ThisWorkbook.ActionsPane.Controls.Add(actionsPane1)
            Globals.ThisWorkbook.Application.DisplayDocumentActionTaskPane = True
            'marche pas  SendKeys.Send("{ESC}")
            '
            'wait here for the end of Button1_click

            Target.Value = list
            list = ""
        End If
    End If
    MsgBox("doubleclick end")

End Sub


End Class

And there is my actionpane1 :

Public Class ActionsPaneControl1


Friend Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim i As Integer
    Dim itemChecked As Object
    Const barre As String = " / "
    For Each itemChecked In CheckedListBox1.CheckedItems

        Globals.Feuil1.list = Globals.Feuil1.list + itemChecked.ToString() + barre
    Next


    ' Boucle pour reset la list
    For i = 0 To (CheckedListBox1.Items.Count - 1)

        CheckedListBox1.SetItemChecked(i, False)
    Next

    Globals.ThisWorkbook.Application.DisplayDocumentActionTaskPane = False




End Sub



End Class

Upvotes: 0

Views: 980

Answers (2)

Jordan Matas
Jordan Matas

Reputation: 112

So i think about the problem, and it seems I didn't understand my function doubleclick, was not working after the double click but before ! that was my mistake.

So i change my function to detect, the change of selection in my sheet. Then i call my action pane. And work with the event of the button of the action pane.

There is the entire code ( maybe because i don't explain me correctly)

my Sheet1.vb :

Imports Microsoft.Office.Interop.Excel

Public Class Feuil1

 Friend actionsPane1 As New ActionsPaneControl1
 Public list As String
 Public cell As Range

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) Handles Me.SelectionChange
    If Target.Column <> 1 Then
        If Target.Row = 16 Then
            Globals.ThisWorkbook.ActionsPane.Controls.Add(actionsPane1)
            Globals.ThisWorkbook.Application.DisplayDocumentActionTaskPane = True

            cell = Target
        End If
    End If



 End Sub
End Class

and my actionpanecontrol1.vb :

Public Class ActionsPaneControl1

 Friend Button1Click = False

 Friend Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim i As Integer
    Dim itemChecked As Object
    Const barre As String = " / "
    For Each itemChecked In CheckedListBox1.CheckedItems

        Globals.Feuil1.list = Globals.Feuil1.list + itemChecked.ToString() + barre
    Next


    ' Boucle pour reset la list
    For i = 0 To (CheckedListBox1.Items.Count - 1)

        CheckedListBox1.SetItemChecked(i, False)
    Next

    Globals.ThisWorkbook.Application.DisplayDocumentActionTaskPane = False

    Button1Click = True

    Globals.Feuil1.cell.Value = Globals.Feuil1.list


    Globals.Feuil1.list = ""

 End Sub

End Class

Thanks a lot for all your reply

Upvotes: 0

Alessandro Mandelli
Alessandro Mandelli

Reputation: 581

Example taken from https://www.daniweb.com/programming/software-development/threads/139395/how-to-check-if-a-button-was-clicked . Credit is given. Basicly declare a variable at form level and then set it to true whenever the button is clicked. Reset it when appropriate

    Dim bBtnClicked As Boolean = False
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If bBtnClicked = True Then
            MessageBox.Show("This button is clicked already ....")
        Else
            MessageBox.Show("This button is clicked First time ....")
        End If
        bBtnClicked = True
    End Sub

Alternatively whatever it is that you want to happen after the button is clicked, just put that code in the handler for the button-click event.

Upvotes: 1

Related Questions