Link to home
Start Free TrialLog in
Avatar of Terrygordon
Terrygordon

asked on

Can't make my forms appear in Visual studio

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
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

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
Sorry
Dim frm as new password
frm.showdialog  
Avatar of Terrygordon
Terrygordon

ASKER

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

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

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)
@Idle_Mind
Thanks for the tip Idle ... I always use this way and I din't knew that!  :P
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
In the menu Project - "Project Name" Properties you have the tabs or right click in the solution explorer on your project root
Sorry guys - I couldn't really see how any of this works. I have since solved the problem.
You could not implement the suggested solution and you delete the question !!!
We have showed code, link and detailed information.
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

What version of VB.NET do you have ?
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.
That changes everything!
My namespace is available since .Net Framework 2.0/Visual Studio 2005
So what's next? Is the question closed or do I wait for an answer. :-)
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 ?
Is your app starting with the "password" form, the "main" form, or Sub Main?
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
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...
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

Except that it would be:

        Dim frm As New frmMain()
        Application.Run(frm)
Hi Idle
This works great. I would be interested in seeing an example of the second approach.
Regards
Terry
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Except that it would be:

I didn't tried :) I don't have VS2002 or VS2003 to test it!
 
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.  =)
Thanks idle. I appreciate the work that you have put in Jpaulino but idle's code did what I needed to do.
Regards
Terry
No problem Terry :)