Question

Locked

Using VB.Net (Visual Studio.Net 2003)

By brianmilke ·
I am having trouble making a form hide after collecting data from that form's TextBox. I use the code:

Dim f1 as New Form1
f1.Show
Me.Hide

This works as long as no data was collected. But if data was collected, an instance of that form is still open. If I go to the next choice, I then get two forms, plus the original 1, then 3 plus two, plus 1.

The code for the forms is below:

This is the form for choosing a part...
Public Class FormCLED
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

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 MainMenu1 As System.Windows.Forms.MainMenu
Friend WithEvents PictureBox4 As System.Windows.Forms.PictureBox
Friend WithEvents PictureBox3 As System.Windows.Forms.PictureBox
Friend WithEvents PictureBox2 As System.Windows.Forms.PictureBox
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
Friend WithEvents CheckBox2 As System.Windows.Forms.CheckBox
Friend WithEvents CheckBox3 As System.Windows.Forms.CheckBox
Friend WithEvents CheckBox4 As System.Windows.Forms.CheckBox
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(FormCLED))
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.PictureBox4 = New System.Windows.Forms.PictureBox
Me.PictureBox3 = New System.Windows.Forms.PictureBox
Me.PictureBox2 = New System.Windows.Forms.PictureBox
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.CheckBox1 = New System.Windows.Forms.CheckBox
Me.CheckBox2 = New System.Windows.Forms.CheckBox
Me.CheckBox3 = New System.Windows.Forms.CheckBox
Me.CheckBox4 = New System.Windows.Forms.CheckBox
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.TextBox3 = New System.Windows.Forms.TextBox
Me.TextBox4 = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'PictureBox4
'
Me.PictureBox4.BackgroundImage = CType(resources.GetObject("PictureBox4.BackgroundImage"), System.Drawing.Image)
Me.PictureBox4.Location = New System.Drawing.Point(388, 231)
Me.PictureBox4.Name = "PictureBox4"
Me.PictureBox4.Size = New System.Drawing.Size(144, 12
Me.PictureBox4.TabIndex = 27
Me.PictureBox4.TabStop = False
'
'PictureBox3
'
Me.PictureBox3.BackgroundImage = CType(resources.GetObject("PictureBox3.BackgroundImage"), System.Drawing.Image)
Me.PictureBox3.Location = New System.Drawing.Point(100, 231)
Me.PictureBox3.Name = "PictureBox3"
Me.PictureBox3.Size = New System.Drawing.Size(144, 12
Me.PictureBox3.TabIndex = 26
Me.PictureBox3.TabStop = False
'
'PictureBox2
'
Me.PictureBox2.BackgroundImage = CType(resources.GetObject("PictureBox2.BackgroundImage"), System.Drawing.Image)
Me.PictureBox2.Location = New System.Drawing.Point(388, 39)
Me.PictureBox2.Name = "PictureBox2"
Me.PictureBox2.Size = New System.Drawing.Size(144, 12
Me.PictureBox2.TabIndex = 25
Me.PictureBox2.TabStop = False
'
'PictureBox1
'
Me.PictureBox1.BackgroundImage = CType(resources.GetObject("PictureBox1.BackgroundImage"), System.Drawing.Image)
Me.PictureBox1.Location = New System.Drawing.Point(100, 39)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(144, 12
Me.PictureBox1.TabIndex = 24
Me.PictureBox1.TabStop = False
'
'CheckBox1
'
Me.CheckBox1.Cursor = System.Windows.Forms.Cursors.Hand
Me.CheckBox1.Location = New System.Drawing.Point(120, 184)
Me.CheckBox1.Name = "CheckBox1"
Me.CheckBox1.Size = New System.Drawing.Size(88, 24)
Me.CheckBox1.TabIndex = 28
Me.CheckBox1.Text = "Blue Fan"
'
'CheckBox2
'
Me.CheckBox2.Cursor = System.Windows.Forms.Cursors.Hand
Me.CheckBox2.Location = New System.Drawing.Point(416, 184)
Me.CheckBox2.Name = "CheckBox2"
Me.CheckBox2.Size = New System.Drawing.Size(88, 24)
Me.CheckBox2.TabIndex = 29
Me.CheckBox2.Text = "Red Fan"
'
'CheckBox3
'
Me.CheckBox3.Cursor = System.Windows.Forms.Cursors.Hand
Me.CheckBox3.Location = New System.Drawing.Point(120, 376)
Me.CheckBox3.Name = "CheckBox3"
Me.CheckBox3.Size = New System.Drawing.Size(88, 24)
Me.CheckBox3.TabIndex = 30
Me.CheckBox3.Text = "Green Fan"
'
'CheckBox4
'
Me.CheckBox4.Cursor = System.Windows.Forms.Cursors.Hand
Me.CheckBox4.Location = New System.Drawing.Point(416, 376)
Me.CheckBox4.Name = "CheckBox4"
Me.CheckBox4.Size = New System.Drawing.Size(88, 24)
Me.CheckBox4.TabIndex = 31
Me.CheckBox4.Text = "Orange Fan"
'
'TextBox1
'
Me.TextBox1.Font = New System.Drawing.Font("Arial", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.TextBox1.Location = New System.Drawing.Point(208, 184)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(40, 22)
Me.TextBox1.TabIndex = 32
Me.TextBox1.Text = ""
Me.TextBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
'
'TextBox2
'
Me.TextBox2.Font = New System.Drawing.Font("Arial", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.TextBox2.Location = New System.Drawing.Point(504, 184)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(40, 22)
Me.TextBox2.TabIndex = 33
Me.TextBox2.Text = ""
Me.TextBox2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
'
'TextBox3
'
Me.TextBox3.Font = New System.Drawing.Font("Arial", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.TextBox3.Location = New System.Drawing.Point(208, 376)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.Size = New System.Drawing.Size(40, 22)
Me.TextBox3.TabIndex = 34
Me.TextBox3.Text = ""
Me.TextBox3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
'
'TextBox4
'
Me.TextBox4.Font = New System.Drawing.Font("Arial", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.TextBox4.Location = New System.Drawing.Point(504, 376)
Me.TextBox4.Name = "TextBox4"
Me.TextBox4.Size = New System.Drawing.Size(40, 22)
Me.TextBox4.TabIndex = 35
Me.TextBox4.Text = ""
Me.TextBox4.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("Arial", 18.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.Location = New System.Drawing.Point(48, 416)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(552, 23)
Me.Label1.TabIndex = 36
Me.Label1.Text = "Click on a checkbox to order that type of fan"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'Label2
'
Me.Label2.Font = New System.Drawing.Font("Arial", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label2.Location = New System.Drawing.Point(104,
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(432, 23)
Me.Label2.TabIndex = 37
Me.Label2.Text = "All LED Fans On SALE!! Only $4.99 each while supplies last!"
Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'FormCLED
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(632, 446)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.TextBox4)
Me.Controls.Add(Me.TextBox3)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.CheckBox4)
Me.Controls.Add(Me.CheckBox3)
Me.Controls.Add(Me.CheckBox2)
Me.Controls.Add(Me.CheckBox1)
Me.Controls.Add(Me.PictureBox4)
Me.Controls.Add(Me.PictureBox3)
Me.Controls.Add(Me.PictureBox2)
Me.Controls.Add(Me.PictureBox1)
Me.Menu = Me.MainMenu1
Me.Name = "FormCLED"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "FormCLED"
Me.ResumeLayout(False)

End Sub

#End Region

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

If BlueFanCount > 0 Then CheckBox1.Checked = True ' Check if Ordered
If CheckBox1.Checked = True Then TextBox1.Text = BlueFanCount ' Display Amount
If RedFanCount > 0 Then CheckBox2.Checked = True
If CheckBox2.Checked = True Then TextBox2.Text = RedFanCount
If GreenFanCount > 0 Then CheckBox3.Checked = True
If CheckBox3.Checked = True Then TextBox3.Text = GreenFanCount
If OrangeFanCount > 0 Then CheckBox4.Checked = True
If CheckBox4.Checked = True Then TextBox4.Text = OrangeFanCount

MainMenu()

End Sub

Private Sub MainMenu()

Dim mnuMenuItem1 As MenuItem, mnuMenuItem2 As MenuItem, mnuMenuItem3 As MenuItem

mnuMenuItem1 = New MenuItem("RETURN HOME", New EventHandler(AddressOf mnuMenuItem1_Click))
MainMenu1.MenuItems.Add(mnuMenuItem1)

mnuMenuItem2 = New MenuItem("CHECK OUT", New EventHandler(AddressOf mnuMenuItem2_Click))
MainMenu1.MenuItems.Add(mnuMenuItem2)

mnuMenuItem3 = New MenuItem("EXIT", New EventHandler(AddressOf mnuMenuItem3_Click))
MainMenu1.MenuItems.Add(mnuMenuItem3)

End Sub

Private Sub mnuMenuItem1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim f1 As New Form1
f1.Show()
Me.Hide()
End Sub

Private Sub mnuMenuItem2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim f1 As New FormSALE
f1.Show()
Me.Hide()
End Sub

Private Sub mnuMenuItem3_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MessageBox.Show("Thanks for trying my program!", "Good-Bye!")
End
End Sub


Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
Dim f1 As New FormBFan
f1.Show()
Me.Hide()
End Sub

Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
Dim f1 As New FormRFan
f1.Show()
Me.Hide()

End Sub

Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
Dim F1 As New FormGFan
F1.Show()
Me.Hide()

End Sub

Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
Dim f1 As New FormOFan
f1.Show()
Me.Hide()
End Sub


End Class

Then the user chooses a part, and a new form opens, say Orange Fan part...

Public Class FormOFan
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

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 MainMenu1 As System.Windows.Forms.MainMenu
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(FormOFan))
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.Label2 = New System.Windows.Forms.Label
Me.Button1 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'Label2
'
Me.Label2.Font = New System.Drawing.Font("Arial", 15.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label2.Location = New System.Drawing.Point(107, 230)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(256, 23)
Me.Label2.TabIndex = 11
Me.Label2.Text = "Enter 0 to Clear Order"
Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'Button1
'
Me.Button1.Font = New System.Drawing.Font("Arial", 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Button1.Location = New System.Drawing.Point(499, 230)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(50, 50)
Me.Button1.TabIndex = 10
Me.Button1.Text = "OK"
'
'TextBox1
'
Me.TextBox1.Font = New System.Drawing.Font("Arial", 15.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.TextBox1.Location = New System.Drawing.Point(491, 166)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(56, 32)
Me.TextBox1.TabIndex = 8
Me.TextBox1.Text = ""
Me.TextBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("Arial", 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.Location = New System.Drawing.Point(64, 166)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(408, 32)
Me.Label1.TabIndex = 9
Me.Label1.Text = "How Many Orange Fans Would You Like?"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'FormOFan
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackgroundImage = CType(resources.GetObject("$this.BackgroundImage"), System.Drawing.Image)
Me.ClientSize = New System.Drawing.Size(632, 446)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Label1)
Me.Menu = Me.MainMenu1
Me.Name = "FormOFan"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "FormOFan"
Me.ResumeLayout(False)

End Sub

#End Region

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

End Sub

Private Sub MainMenu()
Dim mnuMenuItem1 As MenuItem, mnuMenuItem2 As MenuItem, mnuMenuItem3 As MenuItem

mnuMenuItem1 = New MenuItem("RETURN HOME", New EventHandler(AddressOf mnuMenuItem1_Click))
MainMenu1.MenuItems.Add(mnuMenuItem1)

mnuMenuItem2 = New MenuItem("CHECK OUT", New EventHandler(AddressOf mnuMenuItem2_Click))
MainMenu1.MenuItems.Add(mnuMenuItem2)

mnuMenuItem3 = New MenuItem("EXIT", New EventHandler(AddressOf mnuMenuItem3_Click))
MainMenu1.MenuItems.Add(mnuMenuItem3)

End Sub

Private Sub mnuMenuItem1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim f1 As New Form1
f1.Show()
Me.Hide()
End Sub

Private Sub mnuMenuItem2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim f1 As New FormSALE
f1.Show()
Me.Hide()
End Sub

Private Sub mnuMenuItem3_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MessageBox.Show("Thanks for trying my program!", "Good-Bye!")
End
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
OrangeFanCount = TextBox1.Text ' Get value for Orange Fans Ordered
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f1 As New FormCLED
f1.Show()
Me.Hide()
End Sub
End Class

Now the user click the button "OK" is redirected to the first page, FormCLED, but when this happens, FormOFan stays open. When I click on another form, say FormBFan, and enter the number of fans wanted, click ok, Two more forms are opened, FormOFan and FormBFan, along with FOrmOFan from the first click.

This continues, say if I choose all the fans, and input the number of fans I want, I could have 30 or more forms open at once.

Help me figure this out please!!

Thank you!

Brian C Milke
brianmilke@comcast.net

This conversation is currently closed to new comments.

3 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Back to Windows Forum
3 total posts (Page 1 of 1)  

Related Discussions

Related Forums