Click here to Skip to main content
15,881,600 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: multiple forms Pin
JUNEYT25-Feb-07 1:09
JUNEYT25-Feb-07 1:09 
GeneralRe: multiple forms Pin
manni_n25-Feb-07 1:35
manni_n25-Feb-07 1:35 
GeneralRe: multiple forms Pin
Christian Graus25-Feb-07 8:48
protectorChristian Graus25-Feb-07 8:48 
GeneralRe: multiple forms Pin
JUNEYT25-Feb-07 1:21
JUNEYT25-Feb-07 1:21 
Generalits solved Pin
manni_n25-Feb-07 1:45
manni_n25-Feb-07 1:45 
GeneralRe: multiple forms [modified] Pin
TwoFaced25-Feb-07 8:21
TwoFaced25-Feb-07 8:21 
GeneralRe: multiple forms Pin
JUNEYT25-Feb-07 11:49
JUNEYT25-Feb-07 11:49 
GeneralRe: multiple forms Pin
TwoFaced25-Feb-07 14:59
TwoFaced25-Feb-07 14:59 
The first form needs to have a global variable to track the form it wants to call. If it already exists then we just show it instead. The only reason the following code is a problem is because in your case instead of closing form2 you hid it. Thus everytime you clicked the button a new form was created and the hidden forms stayed hidden.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim frm As Form = New Form2
    frm.Show()
    me.hide
End Sub
It's probably better to close form2 if that's possible which avoid the problem altogether. In that case this code is just fine.

If the form creating form2 is the main form you can't close it so hiding it is your only option. If you can close form1 then again you could avoid the whole problem of many instances by doing so. That way when form2 makes a new instance we don't leave hidden form1's all over the place. If you do need to hide form1 then you could pass the current instance onto form2 so form2 knows which form called it and can show that form again. This sample demonstrates all of this. It's not complex and I'm sure there are other ways. I think the technique used would depend on the situation. But here is one option.
Public Class Form1
    Dim frm As Form2
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Create a new instance of Form2 if we don't have one
        'Notice I passed this instance in the constructor
        If frm Is Nothing OrElse frm.IsDisposed Then frm = New Form2(Me)

        frm.Show()
        Me.Hide()
    End Sub
End Class
Public Class Form2
    Private m_Caller As Form    'The calling form

    Public Sub New(ByVal caller As Form)
        InitializeComponent()

        m_Caller = caller
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        m_Caller.Show()
        Me.Hide() 'You could also call me.close instead which may be more appropriate
    End Sub
End Class

Generalacceptable answer Pin
JUNEYT25-Feb-07 22:59
JUNEYT25-Feb-07 22:59 
GeneralRe: acceptable answer Pin
TwoFaced26-Feb-07 13:53
TwoFaced26-Feb-07 13:53 
GeneralYour solution is not appropriate! Pin
JUNEYT26-Feb-07 23:22
JUNEYT26-Feb-07 23:22 
GeneralRe: Your solution is not appropriate! [modified] Pin
TwoFaced27-Feb-07 9:36
TwoFaced27-Feb-07 9:36 
Questionwhat is 192 bit encryption Pin
onrivman24-Feb-07 22:01
onrivman24-Feb-07 22:01 
AnswerRe: what is 192 bit encryption Pin
Guffa25-Feb-07 1:49
Guffa25-Feb-07 1:49 
Questionevents and delegates in vb.net Pin
Pankaj Garg24-Feb-07 19:31
Pankaj Garg24-Feb-07 19:31 
AnswerRe: events and delegates in vb.net Pin
JUNEYT25-Feb-07 0:50
JUNEYT25-Feb-07 0:50 
QuestionReferential classes in vb.net Pin
Pankaj Garg24-Feb-07 19:27
Pankaj Garg24-Feb-07 19:27 
AnswerRe: Referential classes in vb.net Pin
JUNEYT25-Feb-07 0:59
JUNEYT25-Feb-07 0:59 
GeneralRe: Referential classes in vb.net Pin
amaneet25-Feb-07 1:54
amaneet25-Feb-07 1:54 
GeneralRe: Referential classes in vb.net Pin
JUNEYT25-Feb-07 2:33
JUNEYT25-Feb-07 2:33 
QuestionAuthenticaton mode(Desktop application) Pin
Pankaj Garg24-Feb-07 19:07
Pankaj Garg24-Feb-07 19:07 
Questionoption explicit and option strict Pin
Pankaj Garg24-Feb-07 19:05
Pankaj Garg24-Feb-07 19:05 
AnswerRe: option explicit and option strict Pin
JUNEYT25-Feb-07 0:53
JUNEYT25-Feb-07 0:53 
GeneralRe: option explicit and option strict Pin
amaneet25-Feb-07 1:55
amaneet25-Feb-07 1:55 
GeneralRe: option explicit and option strict Pin
JUNEYT25-Feb-07 2:38
JUNEYT25-Feb-07 2:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.