0

I am pretty a beginner in using visual studio so I may missed a lot of things. I want to create a custom group in MS Project with a button which I want when I click on it, it would open up a WinForm with some buttons on it. So I created a VSTO project for MS project then added a Ribbon(Visual designer) and a form.

My custom group in Task tab created with a button in it. I double clicked on this button to jump right into it's click handler code, then tried to write a simple code to show my form but it seems it lacks something because intellisense doesn't show up .Show() method and it can not be built. The error I got when I try to build is this:

error BC30469: Reference to a non-shared member requires an object reference.

My form label is Form1 and the simplest code which I wrote in my button click event handler is as follow:

Imports Microsoft.Office.Tools.Ribbon
Imports System.Windows.Forms '(I added this line after the hints)

Public Class Ribbon1
    Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e 
        As RibbonUIEventArgs) Handles MyBase.Load
    End Sub

    Private Sub Button1_Click(sender As Object, e As 
    RibbonControlEventArgs) Handles Button1.Click
        Form1.Show() '(The Line which error occurs)
    End Sub
End Class
Milad
  • 77
  • 11
  • Is there an error message when you try to build? Something along the lines of "[Missing a using directive or assembly reference](https://stackoverflow.com/questions/17344295/im-getting-the-missing-a-using-directive-or-assembly-reference-and-no-clue-wh)"? Given that [`Form()`](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.show?view=windowsdesktop-6.0) isn't in your Intellisense, you're likely missing a using statement for `System.Windows.Forms` or a reference to the same assembly. – D M Apr 05 '22 at 14:15
  • 1
    Where is `Form1` defined? What is its type? What does the compiler tell you when you try to use this code? – David Apr 05 '22 at 14:16
  • @D M Doesn't visual studio automatically add required references when you add a new item through "project>add new item" ? – Milad Apr 05 '22 at 16:06
  • @David I didn't define it my code, I just added it by "add new item" through "project>add new item" and then selecting a WinForm from Common Controls category. So I guess despite inserting a new item (Form1.vb in my case) to the project I should redefine it in my ribbon code too, right? – Milad Apr 05 '22 at 16:11
  • When you hoover your mouse over Fom1 what does the hint tells you ? Can you see the type ? Is it actually a Form or is it another type ? – GuidoG Apr 06 '22 at 12:04

2 Answers2

0

First of all, you need to check references of your add-in project. By default VSTO add-in projects don't include the System.Windows.Forms assembly reference. If the reference is not there, you need to add it manually. Then in the code you need to use the fully qualified namespace or include it in the beginning of the code file (by using the Imports keyword).

To be able to call the Show method you need to create the Form1 instance in the code first. The method is not static, so you can't invoke it on the class declaration. You must have an object reference.

The code may look like that:

Private Sub Button1_Click(sender As Object, e As 
    RibbonControlEventArgs) Handles Button1.Click
      Dim f as Form1 = new Form1()  
      f.Show() 
    End Sub
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
0

The given answer from Eugene Astafiev is absoluteyl correct, but as Visual Basic has it's historic failures, the simplyfied

Form.Show()

can work for historic VB6 and older versions. That's why one important recommendation for vb.net Developers is to switch

Option Explicit On

Seems that this Option is set by Milad and that's why the error message appears. But there are a lot of vb.net articles opening a Form only using Form.Show() :(