Solved

Can't make my forms appear in Visual studio

Posted on 2009-04-09
29
307 Views
Last Modified: 2013-11-26
Hi all
I am using visual studio net for the first time (having moved from VB 6). This will probably sound totally stupid but I can't even get it to display a simple form. I have a main form called 'Form1' and a dialog box called 'Password'. Form1 is diaplaying fine but I cannot seem to get the 'Passwors' dialog box to display. I have tried:

Dim password as new form
password.showdialog

but all I get is a blank dialog box.

This is so easy in VB 6, but how on earth do you simply show a form in studio net? The help system seems to contradict itself at every turn.

Any advice or a pointer to a tutorial would be greatly appreciated.

Regards

Terry
0
Comment
Question by:Terrygordon
  • 12
  • 9
  • 6
29 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 24106027
You have to create a new instance of your form. If your form name is "Form1" you should do:
Dim password as new Form1
password.showdialog
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24106034
Sorry
Dim frm as new password
frm.showdialog  
0
 

Author Comment

by:Terrygordon
ID: 24106385
Hi ipaulino
This seems to work fine if I attach the code to an event such as a button click. How would I get it to trigger automatically from the main screen? I am not sure where to put it in the code. I have attached the code as it currently appears with the password box activated by the button but where would it go to activate without a button?

Regards

Terry
Public Class Form1

    Inherits System.Windows.Forms.Form
 

#Region " Windows Form Designer generated code "
 

    Public Sub New()

        MyBase.New()
 

        'This call is required by the Windows Form Designer.

        InitializeComponent()
 

        'Add any initialization after the InitializeComponent() call

        WindowState = FormWindowState.Maximized

    End Sub
 

    'Form overrides dispose to clean up the component list.

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

        If disposing Then

            If Not (components Is Nothing) Then

                components.Dispose()

            End If

        End If

        MyBase.Dispose(disposing)

    End Sub
 

    'Required by the Windows Form Designer

    Private components As System.ComponentModel.IContainer
 

    'NOTE: The following procedure is required by the Windows Form Designer

    'It can be modified using the Windows Form Designer.  

    'Do not modify it using the code editor.

    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox

    Friend WithEvents Button1 As System.Windows.Forms.Button

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))

        Me.PictureBox1 = New System.Windows.Forms.PictureBox

        Me.Button1 = New System.Windows.Forms.Button

        Me.SuspendLayout()

        '

        'PictureBox1

        '

        Me.PictureBox1.AccessibleName = "MainPicture"

        Me.PictureBox1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _

                    Or System.Windows.Forms.AnchorStyles.Left) _

                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)

        Me.PictureBox1.BackColor = System.Drawing.Color.FromArgb(CType(28, Byte), CType(45, Byte), CType(115, Byte))

        Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"), System.Drawing.Image)

        Me.PictureBox1.Location = New System.Drawing.Point(112, 8)

        Me.PictureBox1.Name = "PictureBox1"

        Me.PictureBox1.Size = New System.Drawing.Size(696, 688)

        Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage

        Me.PictureBox1.TabIndex = 0

        Me.PictureBox1.TabStop = False

        '

        'Button1

        '

        Me.Button1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

        Me.Button1.ForeColor = System.Drawing.SystemColors.ActiveCaptionText

        Me.Button1.Location = New System.Drawing.Point(424, 432)

        Me.Button1.Name = "Button1"

        Me.Button1.Size = New System.Drawing.Size(80, 32)

        Me.Button1.TabIndex = 1

        Me.Button1.Text = "Start"

        '

        'Form1

        '

        Me.AccessibleName = ""

        Me.AccessibleRole = System.Windows.Forms.AccessibleRole.None

        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

        Me.BackColor = System.Drawing.Color.FromArgb(CType(28, Byte), CType(45, Byte), CType(115, Byte))

        Me.ClientSize = New System.Drawing.Size(920, 694)

        Me.Controls.Add(Me.Button1)

        Me.Controls.Add(Me.PictureBox1)

        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)

        Me.Name = "Form1"

        Me.Text = "SimCo"

        Me.ResumeLayout(False)
 

    End Sub
 

#End Region

 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim frm As New Password

        frm.ShowDialog()

    End Sub

End Class

Open in new window

0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24106449
Ok, you want to make a login form!

Then go to My Project - Application Tab - View Application Events. Then in My Namespace you can use the snippet code that opens the the form and evaluates the result.

In the password form, if the login/password ir correct, you just have to do:

Me.DialogResult = Windows.Forms.DialogResult.OK
Me.Close()

Namespace My
 

    Partial Friend Class MyApplication
 

        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
 

            Dim frm As New Password

            If frm.ShowDialog <> DialogResult.OK Then

                e.Cancel = True

            End If
 

        End Sub
 

    End Class
 

End Namespace

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24107726
Some good info here:
http://msdn.microsoft.com/en-us/library/w3xx6ewx(VS.80).aspx

@jpaulino...you don't need the "Me.Close()" in a dialog bud...   ;)
(setting DialogResult causes the form to hide automatically and code to resume after the ShowDialog call)
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24108316
@Idle_Mind
Thanks for the tip Idle ... I always use this way and I din't knew that!  :P
0
 

Author Comment

by:Terrygordon
ID: 24121574
Sorry for the delay in replying P - been away for a few days. Either I am very stupid or my version of Visual Basic.NET is completely different. I can't see an 'Application' tab or anything resembling it.

I have attached a picture of what the project window looks like.

I am very new to this environment so please explain as simply as possible.

Regards

Terry
SimCo.jpg
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24122223
In the menu Project - "Project Name" Properties you have the tabs or right click in the solution explorer on your project root
0
 

Author Comment

by:Terrygordon
ID: 24231893
Sorry guys - I couldn't really see how any of this works. I have since solved the problem.
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24235740
You could not implement the suggested solution and you delete the question !!!
We have showed code, link and detailed information.
0
 

Author Comment

by:Terrygordon
ID: 24236072
I have been trying to implement the solution suggested by the expert but, despite having said that I am a complete novice at VB.NET, I have not received any clear, easy to follow, instructions. I cannot relate any of the menu items mentioned in the solutions to any tabs or sub-menus on my version of VB.NET. For example, the post on 04/11 mentions "menu Project - Project Name".  When I click on the Project menu there is no "Project Name" option. It is things like "Add windows form", "Add Inherited Form", etc.

After many years of successfully implementing dozens of excellent solutions from EE, and, until now, having never closed a question because I didn't understand what the experts were getting at, I don't think I am being unreasonable here, or in breach of any Member Agreement.  

I gave the experts a picture of the actual project window which they could have referred to in the solution. They could also have told me what or where "My Namespace" is, etc.

I have closed the question because I am pulling my hair out trying to understand the answer.

I appreciate the hard work of the experts and have no wish to upset anyone but, on this occasion, I simply can't understand the suggested solution.

Regards

Terry

0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24236120
What version of VB.NET do you have ?
0
 

Author Comment

by:Terrygordon
ID: 24236195
MY version is Microsoft Visual Studio.NET 2003

All I have done as a workaround is to build the password box into the start page as a textbox and OK button which displays the main menu if the password is ok and a warning if it isn't.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 48

Expert Comment

by:jpaulino
ID: 24236233
That changes everything!
My namespace is available since .Net Framework 2.0/Visual Studio 2005
0
 

Author Comment

by:Terrygordon
ID: 24236263
So what's next? Is the question closed or do I wait for an answer. :-)
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24236371
Well your question was about open a form and I helped you on that.
I never used VB.NET 2003 (only 2002, 2005 and not 2008) and I don't remember if you have any option to choose the shutdown mode in the properties. Do you want to start with the password form or popup later ?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24236637
Is your app starting with the "password" form, the "main" form, or Sub Main?
0
 

Author Comment

by:Terrygordon
ID: 24236679
It starts with a large main form with nothing showing except a picture (logo, etc.). I want the password box to appear on top of the main form as soon as the application is launched (i.e. the main form shows and then the password box appears automatically). When the correct password is given it can then either reveal hidden objects on the main form or close it and reveal a new form.

Regards

Terry
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24236822
Ok...so in the Load() event of Form1, show the Password form as a Dialog:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Show()
        Application.DoEvents() ' Force THIS form to show itself before the password dialog below

        Dim pw As New Password
        Dim result As DialogResult = pw.ShowDialog()
        Select Case result
            Case Windows.Forms.DialogResult.OK

            Case Windows.Forms.DialogResult.Cancel

        End Select
    End Sub

End Class



If you want Form1 to be closed and a new form appear in its place then we'll need a different approach.  One is to start in Sub Main() and launch with a class that Inherits from ApplicationContext.  That allows you to freely close/open forms without worrying about the app "dying" on you.  Let me know if you want to see an example of that...
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24237046
Since you already have some validations I just show how to start from the sub main, under a module
    Public Sub Main(ByVal CmdArgs() As String)
 

        ' If password validation is ok
 

        Dim frm As New frmMain()

        frm.Show()

        Application.Run()
 

    End Sub

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24237176
Except that it would be:

        Dim frm As New frmMain()
        Application.Run(frm)
0
 

Author Comment

by:Terrygordon
ID: 24237194
Hi Idle
This works great. I would be interested in seeing an example of the second approach.
Regards
Terry
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 24237229
Note that when using the ApplicationContext approach you can freely CLOSE any form you want and open another without worrying about the application closing on you.  To close the app, call "Application.Exit()" from somewhere...

In the below code snippets, I show Module1, Password, and Form1 so look for the breaks inbetween:
(In Project --> Properties, change the "Startup Object" to "Sub Main")
Module Module1
 

    Public Sub Main()

        Application.Run(New MyContext)

    End Sub
 

    Public Class MyContext

        Inherits ApplicationContext
 

        Public Sub New()

            ' this is the "entry point" into the application now

            Dim pw As New Password

            pw.show()

        End Sub
 

    End Class
 

End Module
 

Public Class Password
 

    ' somehow validate user input and launch the "main" form when appropriate

    ' notice that you can call "Me.Close()" to close the password form
 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If someCondition Then

            Dim f1 As New Form1

            f1.Show()

            Me.Close()

        End If

    End Sub
 

End Class
 

Public Class Form1
 

    ' Here I'm showing that you can close the app down by calling "Application.Exit()"
 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Application.Exit()

    End Sub
 

End Class

Open in new window

0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24237297
Except that it would be:

I didn't tried :) I don't have VS2002 or VS2003 to test it!
 
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24237320
Lol...it was close though!  I don't have it on this computer either.  I have to boot up my OLD, SLOW desktop to play with VB.Net 2003.  =)
0
 

Author Comment

by:Terrygordon
ID: 24237375
Thanks idle. I appreciate the work that you have put in Jpaulino but idle's code did what I needed to do.
Regards
Terry
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24237450
No problem Terry :)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now