Help Translate Code From C# to VBNet

pthsoon pthsoon 1 Reputation point
2022-10-19T00:26:47.217+00:00

Please help me to translate C# code below to Vb Net:

private void AssosiateAndRaiseViewEvents()
{
btnSearch.Click += delegate{searchEvent?.Invoke(this, EventArgs.Empty);};
txtSearch.KeyDown += (s, e) =>
{
if (e.KeyCode == Keys.Enter)
SearchEvent?.Invoce(this, EventArgs.Empty);
};

}

Developer technologies | VB
{count} votes

2 answers

Sort by: Most helpful
  1. Jiachen Li-MSFT 34,231 Reputation points Microsoft External Staff
    2022-10-19T02:36:53.823+00:00

    Hi @pthsoon pthsoon ,
    Please check if the follwing code works for you.

        Private Sub AssosiateAndRaiseViewEvents()  
            AddHandler btnSearch.Click, AddressOf btnSearch_Click  
            AddHandler txtSearch.KeyDown, AddressOf txtSearch_KeyDown  
        End Sub  
      
        Private Sub btnSearch_Click()  
            searchEvent?.Invoke(Me, EventArgs.Empty)  
        End Sub  
        Private Sub txtSearch_KeyDown(sender As Object, e As KeyEventArgs)  
            If e.KeyCode = Keys.Enter Then SearchEvent?.Invoce(Me, EventArgs.Empty)  
        End Sub  
    

    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. pthsoon pthsoon 1 Reputation point
    2022-10-19T03:14:59.087+00:00

    Thank you Jiachen Li @Jiachen Li-MSFT reply with you help and this website

    https://stackoverflow.com/questions/1013048/determine-if-an-event-has-been-attached-to-yet
    251831-image-3.png

    i manage make to code as below from C# to VB

    251744-image-4.png

    Pubic Event SearchEvent As EventHandler  
    
    Private Sub AssociateAndRaiseEvent()  
        AddHandler btnSearch.Click, AddressOf btnSearch_Click  
        AddHandler txtSearch.KeyDown, AddressOf txtSearch_KeyDown  
    End Sub  
    
    Private Sub btnSearch_Click()  
        If SearchEventEvent IsNot Nothing Then  
            SearchEventEvent.Invoke(Me, EventArgs.Empty)  
        End If  
    
    End Sub  
    Private Sub txtSearch_KeyDown(sender As Object, e As KeyEventArgs)  
        If e.KeyCode = Keys.Enter Then  
            If SearchEventEvent IsNot Nothing Then  
                SearchEventEvent.Invoke(Me, EventArgs.Empty)  
            End If  
        End If  
          
    End Sub  
    
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.