[VB.net] Running several subs with one button

[VB.net] Running several subs with one button

ChristianAndersenIsmyname
Advocate Advocate
1,274 Views
5 Replies
Message 1 of 6

[VB.net] Running several subs with one button

ChristianAndersenIsmyname
Advocate
Advocate

Hello, I've come across a issue with my vb.net code for a custom add-in.

 

Code below creates a button in one of my ribbon panels, and the CreatePDF sub is partly working with this button.

 

Public Sub BtnPDFCreate_OnExecute(Context As NameValueMap) Handles BtnPDFCreate.OnExecute
Dim Myform As New PDF
Myform.CreatePDF()
End Sub

 

 

When I press the button from the ribbon, the "PDF Created" messagebox shows, and it should continue to the "if lines()", but it skips that for some reason. If I add a messagebox between the "if lines()", these messageboxes shows up.

 

Public Sub CreatePDF()
'hidden interesting code to create a PDF
MsgBox("PDF Created!")
Dim Lines() As String = IO.File.ReadAllLines("C:\TEMP\Personal options.txt")
' Personal options
'Msgbox("Test 1")
If Lines(4) = "True" Then Button10.PerformClick()
'Msgbox("Test 2")
If Lines(5) = "True" Then Button9.PerformClick()
'Msgbox("Test 3")

End Sub

 

I've tried to use Call Button.10.PerformClick(), but this did nothing.

 

However, if I run the code with a button within the PDF form, everything works perfectly.

0 Likes
Accepted solutions (1)
1,275 Views
5 Replies
Replies (5)
Message 2 of 6

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @ChristianAndersenIsmyname 

I don't think you can use PerformClick if the form containing the buttons isn't drawn...

I'd suggest maybe calling the sub handling the buttons click event instead. If you've written your code in visual studio and havn't renamed the subs the names of these should be like: Button10_Click(sender As Object, e As EventArgs).

 

So you could try having your CreatePDF-sub look like this instead 🙂

 

Public Sub CreatePDF()
        'hidden interesting code to create a PDF
        MsgBox("PDF Created!")
        Dim Lines() As String = IO.File.ReadAllLines("C:\TEMP\Personal options.txt")
        ' Personal options
        'Msgbox("Test 1")
        If Lines(4) = "True" Then Button10_Click(Nothing, Nothing)
        'Msgbox("Test 2")
        If Lines(5) = "True" Then Button9_Click(Nothing, Nothing)
        'Msgbox("Test 3")

    End Sub
Message 3 of 6

ChristianAndersenIsmyname
Advocate
Advocate

That was almost the solution, but I had to switch it up a bit.

It seems like I can't call for "Button9_Click(sender As Object, e As EventArgs)",  so I created Sub1() and Sub2() for each button.

Then called for those subs with Button9 and Button10 click.

Public Sub CreatePDF()
        'hidden interesting code to create a PDF
        MsgBox("PDF Created!")
        Dim Lines() As String = IO.File.ReadAllLines("C:\TEMP\Personal options.txt")
        ' Personal options
        If Lines(4) = "True" Then Sub1()
        If Lines(5) = "True" Then Sub2()
End Sub

 

Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
   Sub1()
End Sub

Public Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
   Sub2()
End Sub

 

Public Sub Sub1()
   'Some cool code here
End Sub

Public Sub Sub2()
   'Some other code here
End Sub

 

Message 4 of 6

JhoelForshav
Mentor
Mentor

Yes, keeping the code outside the sub handling the click event is how I'd do it too. Definitely a more robust way of coding if the sub should be called in other ways than clicking the button aswell...

But It makes me curious that it didn't work to just call Button10_Click.

You did call it with Nothing as arguments right?

Button10_Click(Nothing, Nothing) and not Button10_Click(sender As Object, e As EventArgs)

Message 5 of 6

ChristianAndersenIsmyname
Advocate
Advocate

Oh, I didn't try with (Nothing, Nothing). But without testing it in Inventor, that also seems to works. At least Visual Studio accept it.

Message 6 of 6

JhoelForshav
Mentor
Mentor

Good to know 🙂 But it doesn't really matter in your case. The way you're doing it now is better anyways 😉