Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Having problems displaying forms in vb.net

Status
Not open for further replies.

dashen

Programmer
Jul 14, 2005
233
US
Module startup
Public frmMain As New FormMainClass
Public frmSubComponents As New FormSubComponentsClass
Public frmPrograms As New FormProgramsClass
Public frmSummary As New FormSummaryClass
Public frmDetails1 As New FormDetails1Class
Public frmDetails2 As New FormDetails2Class
Public frmDetails3 As New FormDetails3Class

Public Sub Main()
frmMain.ShowDialog()
End Sub
End Module

Public Class FormMainClass
Inherits System.Windows.Forms.Form
'Regions info
'/Regions
Private Sub mnuClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuClose.Click
End
End Sub

Private Sub mnuShowMain_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuShowMain.Click
frmMain.ShowDialog()
Me.Hide()
End Sub

Private Sub mnuShowSubComp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuShowSubComp.Click
frmSubComponents.ShowDialog()
Me.Hide()
End Sub
.
.
.
End Class

I get to the Main Menu form I created and I cycle through the Form using menu items, but if I try to cycle back to the main mene, there is a run-time issue, or something, because the program crashes. All my forms are basically the same right now. The Menu object is a friend type.
 
Oh, okay, thanks.

I still need to run the module first though, because I have some backend stuff I need to run before I run the forms. I cache the information on in public recordset in a ADO structure (ODBC doesn't use recordsets so I didn't use it) in the module, so that I can call it later in any form. Any suggestions.
 
I'm not sure that I fully understand what you are trying to achieve, but this may be of use:

Four forms:
(1) The Main Application - with a Menu bar: FormMainMenu
(2) The Main work form: FormMain
(3) A Secondary from: Form1
(4) Another Secondary form: Form2


When the program starts it does any required initialisation and then displays FormMain
..closing FormMain closes the program
From the menu in FormMainMenu user can select any form
..since all forms have been pre-created and each form has its DockSytle set to Fill in the main application window, when a form is selected BringToFront is used to display it.
..Form1 and Form2 both have a "close" button, this DOES NOT close the form - it raises an event received in the main application to BringToFront FormMain

Code:
Public Class FormMainMenu
  Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
.
.
#End Region

  Private WithEvents f1 As New Form1
  Private WithEvents f2 As New Form2
  Private fmain As New FormMain

  Private Sub SetUp()

    'do all required setup here

  End Sub


  Private Sub FormMainMenu_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

    SetUp()
    f1.TopLevel = False
    f1.Dock = DockStyle.Fill
    f2.TopLevel = False
    f2.Dock = DockStyle.Fill
    fmain.TopLevel = False
    fmain.Dock = DockStyle.Fill
    Me.Controls.Add(f1)
    Me.Controls.Add(f2)
    Me.Controls.Add(fmain)
    fmain.Show()

  End Sub

  Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click

    Application.Exit()

  End Sub

  Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click

    f1.Show()
    f1.BringToFront()

  End Sub

  Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem5.Click

    f2.Show()
    f2.BringToFront()

  End Sub

  Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem6.Click

    fmain.Show()
    fmain.BringToFront()

  End Sub

  Private Sub HideMe() Handles f1.HideMe, f2.HideMe

    fmain.Show()
    fmain.BringToFront()

  End Sub
End Class


Public Class FormMain
Code:
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Application.Exit()

  End Sub


Public Class Form1
Code:
  Public Event HideMe()


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

    RaiseEvent HideMe()

  End Sub

Public Class Form2
Code:
  Public Event HideMe()


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

    RaiseEvent HideMe()

  End Sub


Hope this helps.
 
Question: What if all of the forms need the menu bar?
 
earthandfire,

I used your implementation, but slightly tweaked it.

I am still using the startup module with a Sub Main to call the forms. Is there a way to modify this module's sub main to not call the form using ShowDialog, so that I can hide the MainMenu when I open the other forms?

Public Sub Main()
' Connection ADO string opened`

gblConn = New ADODB.Connection

On Error GoTo ErrorHandler

gblDSN = // sorry can't show
With gblConn
.ConnectionString = gblDSN
.CommandTimeout = 20
.CursorLocation = ADODB.CursorLocationEnum.adUseClient
.Open()
End With

gblCmd.ActiveConnection = gblConn

frmMain.ShowDialog()
/*
I want to change this so that when I open the form it finishes the Sub Main but keeps the form open. Any Ideas? */
ErrorHandler:

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top