Select to view content in your preferred language

VB .NET Calling a Form

6494
11
Jump to solution
03-05-2012 06:01 AM
Gregde_Bruin
Deactivated User
Hi,

I'm in the process of converting code from VBA to VB. NET, and I'm having trouble calling a form I created from a button.
Here's what I've tried so far:

Protected Overrides Sub OnClick()

Dim FormLogin As New System.Windows.Forms.Form
FormLogin.Show()

End Sub

This brings up the form, but it's a completely blank form, which the form I'm attempting to call is not.
Any help is appreciated.

Thank you,
cdebruin
0 Kudos
1 Solution

Accepted Solutions
EdgarBejarano
Deactivated User
Hi,

In your VB.NET project you have defined a form class, e.g. myFormClass, that inherits or is derived from the System.Windows.Forms.Form class.  It is that form class that you have designed with various Windows Forms controls.  That is the class that you should be using when you declare your object variable FormLogin and that you set equal to a new instance of your myFormClass.  Instead you are creating an instance of the generic System.Windows.Forms.Form class.

It should looks something like this in the .vb class file corresponding to your ArcMap or ArcCatalog custom button:

Protected Overrides Sub OnClick()

Dim FormLogin As New myFormClass
FormLogin.Show()

End Sub

There should be a myFormClass.vb class file (the form class) in your VB.NET project when you do the above code, and that form class in turn should be inheriting from the System.Windows.Forms.Form class.

View solution in original post

0 Kudos
11 Replies
GeorgeFaraj
Frequent Contributor
Hi, 

I'm in the process of converting code from VBA to VB. NET, and I'm having trouble calling a form I created from a button. 
Here's what I've tried so far: 

Protected Overrides Sub OnClick()

Dim FormLogin As New System.Windows.Forms.Form
FormLogin.Show()

End Sub


This brings up the form, but it's a completely blank form, which the form I'm attempting to call is not. 
Any help is appreciated. 

Thank you, 
cdebruin


rem What form were you trying to create?
rem Dim FormLogin As New System.Windows.Forms.Form
Dim FormLogin As New TheActualFormIAmTryingToCreateAndNotJustANonSpecificFormObject
rem And how do I want the form to be displayed and handled 
rem FormLogin.Show() ' A floating non-modal form that can be ignored
FormLogin.ShowDialog() ' A modal form that requires the user to use it
0 Kudos
EdgarBejarano
Deactivated User
Hi,

In your VB.NET project you have defined a form class, e.g. myFormClass, that inherits or is derived from the System.Windows.Forms.Form class.  It is that form class that you have designed with various Windows Forms controls.  That is the class that you should be using when you declare your object variable FormLogin and that you set equal to a new instance of your myFormClass.  Instead you are creating an instance of the generic System.Windows.Forms.Form class.

It should looks something like this in the .vb class file corresponding to your ArcMap or ArcCatalog custom button:

Protected Overrides Sub OnClick()

Dim FormLogin As New myFormClass
FormLogin.Show()

End Sub

There should be a myFormClass.vb class file (the form class) in your VB.NET project when you do the above code, and that form class in turn should be inheriting from the System.Windows.Forms.Form class.
0 Kudos
Gregde_Bruin
Deactivated User
Thank you!

I see what you mean by creating instances of the generic windows form class as opposed to my own form.
I fixed the code.

Thanks again.
0 Kudos
JimIsbell
Occasional Contributor
Now is there a way to set the Form.Owner property to be the ArcMap application? This would bring the form forward if the application window is made active.
I found some documentation that shows how to do this with ArcGIS Explorer, which exposes a Application.Window reference that the Form.Owner property will accept.
Unfortunately, the ArcMap Application class only exposes a hWnd integer. I have no clue what to do with that.

Can anyone help?
0 Kudos
RichardWatson
Deactivated User
var parentWindow = new NEWindowsHandlerWrapper((IntPtr)_application.hWnd);

    class NEWindowsHandlerWrapper : IWin32Window
    {
        private readonly IntPtr _handle;
       
        internal NEWindowsHandlerWrapper(IntPtr handle)
        {
            if(handle==IntPtr.Zero)
            {
                throw new ArgumentOutOfRangeException("handle", "Handle cannot be empty.");
            }
            _handle = handle;
        }

        public IntPtr Handle
        {
            get { return _handle; }
        }
    }
0 Kudos
JimIsbell
Occasional Contributor
@rlwatson and @jtkknelson,
Thanks for the code and the links. I have it working now, thanks to helpful folks like you, here in these forums. Much appreciated!
-Jim
0 Kudos
KevinAndras
Emerging Contributor
isbellj,
Would you mind posting your code showing how you accomplished this?  I can't figure it out.
Thanks!!!
0 Kudos
KenBuja
MVP Esteemed Contributor
Here's an example for my VB.NET addin. In my project, I have created the class Win32HWNDWrapper and I have a form FieldInfoForm.

'this is the code in my main program to open the form
Dim FieldForm As New FieldInfoForm
FieldForm.ShowDialog(New Win32HWNDWrapper(My.ArcMap.Application.hWnd))

'this is the code for the Win32HWNDWrapper class
Public Class Win32HWNDWrapper

    Implements System.Windows.Forms.IWin32Window
    Private _hwnd As System.IntPtr

    Public ReadOnly Property Handle As System.IntPtr Implements System.Windows.Forms.IWin32Window.Handle
        Get
            Return _hwnd
        End Get
    End Property

    Public Sub New(ByVal Handle As System.IntPtr)
        _hwnd = Handle
    End Sub

End Class
0 Kudos