opening and closing forms

juggernot

Well-known member
Joined
Sep 28, 2006
Messages
173
Programming Experience
Beginner
Sorry, I know this is a Noob question, but I've never come across this before. I have a form that I want to show only once (the first time the program is run). It's kindof a setup form. Anyhow, from that form I want to open my mainform, while closing the setup form on buttonpress. Whenever I do this I seem to run into problems. Depending on how I go about it, the setup form doesn't exit at all, or both forms exit. Can anyone tell me how to go about this?
 
I can see part of the problem now. In formload, I check to see if a file exists, and if it does I display the mainform and close the setupform. However, if the file doesn't exist, the setupform is displayed. Once a checkbox is checked on the setupform, a messagebox is displayed. It the dialog result of the messagebox is ok, then I want the setup form to close, and the mainform to open. If it is cancel, I just want the setup form to close. My formload code works perfectly, so the only thing I can think of is that somehow the messagebox is screwing things up. I'll post my code below:

VB.NET:
Private Sub Setup_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim f2 As New Form1
Me.TextBox1.Text = "mystring"
If IO.File.Exists(Application.StartupPath & "\accept.cfg") = True _
And IO.File.Exists(Application.StartupPath & "\unaccept.cfg") = True Then
f2.ShowDialog()
Me.Close()
End If
 
End Sub
 
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles CheckBox1.CheckedChanged
result = MessageBox.Show("MyString", "Configuration Settings", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If result = DialogResult.Cancel Then
Me.Timer1.Stop()
result = Nothing
Me.Close()
 
ElseIf result = DialogResult.OK Then
Dim f2 As New Form1
result = Nothing
Me.Timer1.Stop()
f2.ShowDialog()
Me.Close()
End If
 
End Sub
End Class
Note: Use of "MyString" in code is simply getting rid of long irelevant strings, which I don't want to bother you with.
Note: I had originally tried to do it without a timer, but it still didn't work right
 
Last edited:
yeah, sorry about that. My code is always sloppy, as I never intended for anyone but me to see it. Also, I just barely found out how to put the code in a seperate window. Is that file something that can help me? i tried opening it but it said it was made with a newer version of visual studio. I have 2003. Is there some way I can view it?

I've found out that it works to hide the previous form, but that causes some complications. I end up getting two notifyicons appearing(I guess because I have two forms open), and this is less effecient. Maybe I'll have to make my setup file a completely different project. This would work, but I would still like to figure out a way to get past this. As far as I can tell, if I use the .show to create a new form, than when I close the previous it closes the form I showed. If I use .showdialog, that problem goes away, but for somereason the previous form doesn't close. I think this is because no code is run on the previous form until the module form( I think that's what they call it) is closed.
 
Last edited:
ah.. balls. I didnt notice you were on 2003.

Yep, its a 2005 project that solves your problem exactly. I cant give it to you in 2003 because I no longer have it installed.

You can always open the .vb files in a text editor and read them! note- dont open the .designer.vb files - all they contain is code for laying out the forms..


You can, additionally, install vb express (alongside 2003) without a problem - which can read the project straight out and keep you more current than 2003 (which, at 4 years old, is on the declining side of the usage trend)
 
ps; really, do please read the code that i posted.. it's so simple, you'll be amazed. youre currently tracked into a way of solving the problem and there is a problem with the envisaged solution - to actually solve the root problem you need to approach it differently.. reading the code will give you that insight. If you have no luck, let me know and i'll post it here - it's only about 7 lines
 
here are the contents of the .vb files. Note that you can do the same thing yourself by opening them in e.g. notepad

VB.NET:
Module StartupThing
    Sub Main()

        If System.IO.File.Exists("C:\accept.cfg") Then
            Application.Run(New MainForm())
        Else
            Dim x As New SetupForm()
            x.ShowDialog() 'pause here until user answers

            If x.DialogResult = DialogResult.OK Then
                Application.Run(New MainForm())
            End If
        End If
    End Sub
End Module


VB.NET:
Public Class SetupForm

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked Then
            Me.DialogResult = MessageBox.Show("Press OK to close me and open main", "", MessageBoxButtons.OKCancel)
        End If
    End Sub
End Class
 
Back
Top