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.
 
my startup object is a module that calls the main menu form I set as Public.

I don't close the forms in the class, I just hide them, but I call .showdialog for the other forms. Look at my code if you have time, is there anything specifically wrong with it.
 
Like I said, as soon as you hide frmMain, the app will end. Here is a stripped down example:

In a module:
Code:
Module Module1
  Public frm As New Form1()

  Sub main()
    frm.ShowDialog()
    MessageBox.Show("App is ending!")
  End Sub
End Module

In Form1:
Add a Button (Button1)
Code:
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Me.Hide()
  End Sub

As soon as frm1 is hiden, the msgbox will appear and when you hit enter, the app will end.

-Rick



VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
I think the mainform doesn't crash I think it hides. Whne you use showdialog the code won't continue to execute untill you close the childform.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
I am thinking it has something to do with how I set up the menu object.

I just copied and pasted the menu object into each form because each form is supposed to be identical to the main menu. Can someone help me with this?
 
Hi Rick,
That isn't the issue.
I put the msgbox in my module and set a breakpoint. It doesn't even hit this part of the code before it breaks down. Because I called .ShowDialog the module is waiting for the form to close, but I never close it. I just hide the form. This should not send and event back to the module when it is hidden, and even if it did, it is halting before the msgbox and the breakpoint.
 
I have set my breakpoints, and the program dies when I try to call .ShowDialog on any form that has been shown before. Is .ShowDialog not the correct method I need to use?
 
Like I said, run that code chunk I posted. When you hide the primary form of an application, even if it was shown with showdialog, control returns to the calling method. In this case, sub main, which then ends and closes the app.

The other thing that may be happening, since you are opening the new forms w/ show dialog before hiding the previous forms is that you have form1 opening form2 with showdialog, which then opens form1 with show dialog. I'll run a test real quick, but I don't think that'll fly either.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
you shouldn't hide a dialog because it will stop the rest from the code executing try me.close.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Hi Chrissie,

But then I run into the issue that Rick presented, I do not want to have Sub Main end because I need to show the correct form. I guess my question is if there is a better way to cycle through forms then.
 
Okay, new stripped down app:

In the module:
Code:
Module Module1
  Public frm1 As New Form8()
  Public frm2 As New Form8()

  Sub main()
    frm1.ShowDialog()
    MessageBox.Show("App is ending!")
  End Sub
End Module

In the form:
2 buttons (Button1 and Button2)
Code:
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    frm2.ShowDialog()
    Me.Hide()
  End Sub

  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    frm1.ShowDialog()
    Me.Hide()
  End Sub

In this case, when the app loads, frm1 is shown, if you click Button1, frm2 is shown and frm1 remains visible (doesn't hide until frm2 closes). When you click button1 on frm2 you get an exception about frm1 already being displayed modally.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Crap. Is there a way to get around this issue?
 
Okay, this has turned into a mind break for me ;) Here's what I got that I think will do what you want:

in the module:
Code:
Module Module1
  Public frm1 As New Form8()
  Public frm2 As New Form9()

  Sub main()
    frm1.ShowDialog()
    MessageBox.Show("App is ending!")
  End Sub
End Module

In Form8:
Add a button (Button1)
Code:
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    frm2.ShowDialog()
  End Sub

In Form9:
Add a button (Button1)
Code:
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    frm1.Visible = True
    Me.Close()
  End Sub

  Private Sub Form9_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    frm1.Hide()
  End Sub

This appears to give the behavior I beleive you are looking for.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Thanks Rick,

My code is supposed to allow for me to show any form from any other form. That means that I couldn't show the form until I .ShowDialog though correct? In which case this implementation might not work though.
 
The problem comes in with tracking it backwards. If you open frm1 from frmMain, then open frm2 from frm1, when you close frm2 should frm1 be displayed again? or frmMain?

if it goes back to frm1, what happens when a user goes from frm1 to frm2 to frm3 to frm1 to frm3 to frm1, and then starts closing forms... It quickly becomes a nightmare to track where the user has been and manage the forms.

I tried writing an multi-form interface that did just that once, long ago in the distant lands of VB5. In the end, it was horrendous, buggy, complexe code. It would have been significantly better to have the primary form be the central site and the other forms open from there, not from eachother.

If you have a chance, I would look into other interface design options.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
I used the same implementation in my program code, but it still crashes.

I left my module the same as before, but I changed my mainform script menu command to just call .ShowDialog to the other form.

In my other form I created a process on the Form_Load to hide the mainForm (it crashes right there). It will not hide the original form.

And on the menu command I set the Visible property of the mainform back to true and close me
 
the first one should be application.run(frmmain)

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top