WinForm ".Show()" method doesn't show up in Visual studio - vb.net

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

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

Related

VB.net use custom control

I am trying to use the custom GenericDatagridview
(which I got here)
I have added the dll to my toolbox. It is also referenced in my project properties. However when I run my application. The form with the new control gives the following error : The error is: Object reference not set to an instance of an object
The code for the form load is :
Imports GenericDataGridView
Public Class GenericDGVTRASHFRM
Private Sub GenericDGVTRASHFRM_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'SkeduleringsDatabasis6DataSet.Blokkeklaar' table. You can move, or remove it, as needed.
Me.BlokkeklaarTableAdapter.Fill(Me.SkeduleringsDatabasis6DataSet.Blokkeklaar)
End Sub
End Class
Regards.

UserForm won't show

I'm using office developer tools and made a ribbon to access some functions. Thing is, it looks like I can't open a userform from a button, other commands seems to run normally.
Code:
Public Class Empresa
Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles btn_DBSol.Click
'Dim wnd As New frm_DBSolventes
'wnd.Show()
MsgBox("Hello World")
End Sub
End Class
This code have this result on excel ribbon:
https://s24.postimg.org/6z16l6g43/Print_1.jpg
Now using this code:
Public Class Empresa
Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles btn_DBSol.Click
Dim wnd As New frm_DBSolventes
wnd.Show()
'MsgBox("Hello World")
End Sub
End Class
Results in nothing:
There are no errors on Error List window. frm_DBSolventes is a userform on a userform referenced project, there is nothing on the form right now, just made a new project of userform and trying to show it. Is there something I'm missing? Is there any other way where I can use a userform on ribbon?
As Requested the frm_DBSolventes is
https://s29.postimg.org/6w6ae15qd/Print_3.jpg
Just add a datagridview cause I need to continue working. if it makes difference I can change it. There is no code on the form:
Public Class frm_DBSolventes
End Class
Try to show it as a modal Window.

VB.Net Code in class and WithEvents issue

I've created a VB.Net application which consists of a form and a series of classes. One class (GUI) contains all of the routines that handle the logic associated with the controls on my form. These controls are placed at design time.
I've having a problem with a couple of the timers on the form. They are declared in the GUI class thus:
Private WithEvents timerScreenUpdate As Timer
Private WithEvents timerDebug As Timer
but I'm getting an error in this line:
Private Sub timerScreenUpdate_Tick(sender As System.Object, e As System.EventArgs) Handles timerScreenUpdate.Tick
The error is "Event 'Tick' cannot be found". I'm getting the same error for the other timer.
It's probably related but I'm also getting a similar error in this line:
Private Sub Monitor_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
which is "Event 'FormClosing' cannot be found".
How can I make these events visible to my class ?
Make sure that the reference and the Namespace-Import of System.Windows.Forms is available in your project respectively class file.
Without it, VB will use a Timer from the namespace System.Timers which you don't want to use in your case.

Create And Implement a Custom Form in VB.NET

I am trying to create a customized form class (CustomBorderlessForm) in VB.NET.
My Progress So Far
I created a new Class and named it CustomBorderlessForm.vb
I then proceeded to write the following code:
CustomBorderlessForm.vb
Public Class CustomBorderlessForm
Inherits Form
Dim _form As Form = Nothing
Public Sub New(form As Form)
_form = form
MsgBox("Testing: New()")
End Sub
Protected Overrides Sub OnMouseMove(e As MouseEventArgs)
MyBase.OnMouseMove(e)
MsgBox("Testing OnMouseMove()")
End Sub
End Class
Form1.vb
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim form As New CustomBorderlessForm(Me)
End Sub
End Class
Results of progress
A message box displays "Testing: New()" on load
Nothing shows on mouse move
As you can see, my problem lies with the events
Questions
Is it possible to create a form object and use that instead of the pre-populating form?
If so, can I give this form custom properties, such as, a border and some boolean values (shadow...etc), just like any other custom object/class?
What am I doing wrong in my current approach?
Why isn't the OnMouseMove being overridden?
Am I initialising the class wrong?
Can it even be done this way?
After creating a form you also need to show it. Change your logic to:
Dim form As New CustomBorderlessForm(Me)
form.Show()
Before you do that, I'd recommend changing from MsgBox to Console.WriteLine(), otherwise you can run into a fun/frustrating little cat and mouse game.
EDIT
Based on the comments, if, from VS you did a "Add New, Windows Form" you can just right-click the project, select property and on the Application tab change the Startup object to your new form. VS only allows you to do this with forms it creates for you (by default, more on this later).
If you wrote that file by hand (which is absolutely fine) you can perform the Show() like I did above and call Me.Hide() to hide the "parent" form. Unfortunately the Load event is fired before the Show event so if you place this in Form1_Load() it won't work. Instead you can use the Shown event like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim form As New CustomBorderlessForm(Me)
form.Show()
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Me.Hide()
End Sub
Another option has to do with "Application framework". You can read about it here however it basically handles application events that other languages have to manually implement. If you go into your project properties you can uncheck the "Enable application framework" checkbox. This will give you more option in the "Startup object" dropdown. If you add the following code to your project one of the items in the Startup object dropdown menu should now be "Loader"
Public Module Loader
<STAThread()>
Public Sub Main()
Dim form As New CustomBorderlessForm(Nothing)
form.ShowDialog()
End Sub
End Module
You'll notice that the above bypasses Form1 completely. Also, instead of Show() I'm using ShowDialog() because otherwise the form shows and then the program ends.

How to use the Show Event in VB.net

I try tu use this msdn snipped to execute some code right after my form loads:
Private Sub Form1_Shown(sender as Object, e as EventArgs) _
Handles Form1.Shown
Some Code
End Sub
But it seems that I am missing something. I get an errormessage that translated sounds like this:
The Handle requiere an WithEvents-Variable, which is defined in the contained type or its basis class... My Form is named Form1 so that should be ok. the error is marked in the second line of the code. Any ideas?
Instead of:
Handles Form1.Shown
do this:
Handles Me.Shown
Usually that is the sort of error you would get if you create the form in code and not in the designer. The designer will automatically declare the generated form as WithEvents. If you create the Form in code instead you have to declare it as WithEvents.
For example:
Public Form1 as frmMain
Would generate that error unless you add the handler yourself.
AddHandler Form1.Shown, AddressOf Form1_Shown
If you do this instead:
Public WithEvents Form1 as frmMain
wouldn't generate the error.
WithEvents is necessary on any object created if you want to use the handles clause in that way.