0

I have two forms both with the same buttons on, and I want to have it so that if I click the button both buttons will do the same thing i.e. they are referencing each other on different forms. the way i found was:

Public Class Form2
 Dim form1 As New form1
  Private Sub Button2_Click
   form1.backcolor=black
   form2.backcolor=black
 end sub
end class

then

Public Class Form1
 Dim form2 As New form2
  Private Sub Button1_Click
   form1.backcolor=black
   form2.backcolor=black
 end sub
end class

only this doesn't work as there is an error:An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll as far as i can see there is no infinite loop or stack over flow. any help would be greatly appreciated.

2 Answers 2

4

You have an infinite loop, because each time one of the forms is instantiated, it is instantiating the other. Creating a Form1 will create a Form2, then Form2 immediately creates another Form1 and so on and so on...

Change your code to this:

Public Class Form2
   Private Sub Button2_Click
       Dim form1 As New Form1
       form1.backcolor=black
       form2.backcolor=black
   End sub
End class

Public Class Form1
    Private Sub Button1_Click
        form1.backcolor=black
        Dim form2 As New Form2
        form2.backcolor=black
    End sub
End class

Now it will only create the other class instances when you click a button.

Sign up to request clarification or add additional context in comments.

Comments

0

Like Karl Anderson said, there is a infinite loop in your code. His solution will create a new form every time you click the button. If you don't want this behavior, I think that the best approach is to use the mediator pattern. And it will be much more easy if you want to add new actions and new forms.

The code will look something like this:

Public Class Mediator
    Private forms As New List(Of BaseForm)

    Public Sub RegisterForm(form As BaseForm)
        forms.Add(form)
    End Sub

    Public Sub ChangeAllFormsBackColorToBlack()
        For Each form In forms
            form.ChangeBackColorToBlack()
        Next
    End Sub
End Class

Public Class BaseForm
    Private med As Mediator

    Public Sub New(med As Mediator)
        Me.med = med
        Me.med.RegisterForm(Me)
    End Sub

    Public Sub ChangeBackColorToBlack()
        backcolor = black
    End Sub

    Public Sub OnButtonClick()
        Me.med.ChangeAllFormsBackColorToBlack()
    End Sub
End Class

Public Class Form2
    Inherits BaseForm

    Public Sub New(med As Mediator)
        MyBase.New(med)
    End Sub

    Private Sub Button2_Click()
        Me.OnButtonClick()
    End Sub
End Class

Public Class Form1
    Inherits BaseForm

    Public Sub New(med As Mediator)
        MyBase.New(med)
    End Sub

    Private Sub Button1_Click()
        Me.OnButtonClick()
    End Sub
End Class

Module MediatorDemo
    Sub Main()
        Dim med As New Mediator
        Dim f1 As New Form1(med)
        Dim f2 As New Form2(med)

        f1.OnButtonClick()
    End Sub
End Module

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.