I have two forms

Form 1: Contains
1. Trackbar (adjusts opacity of form 2)
2. Button ( to show form 2)

Form 2: Contains
1. only trackbar (adjusts opacity of form 1)

I m able to control only opacity of form 2 via form1, I want it to be vice versa!

Here is the code, I get 'StackOverFlowException' was unhandled error in 14 line of form 1

Form 1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace formaccesstest
{
    public partial class Form1 : Form
    {
        Form2 frm2 = new Form2();
        public Form1()
        {
            InitializeComponent();
        }

        private void form2btn_Click(object sender, EventArgs e)
        {
            frm2.Show();
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            frm2.Opacity = trackBar1.Value * 0.01;
        }

       
    }
}

and form 2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace formaccesstest
{
    public partial class Form2 : Form
    {
        Form1 frm1 = new Form1();
        public Form2()
        {
            InitializeComponent();
        }

        private void form1btn_Click(object sender, EventArgs e)
        {
            
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            frm1.Opacity = trackBar1.Value * 0.01;
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            
        }
    }
}

C'mon guys!

i think it might be because it's in an infinite loop. form1 is accessing form2 while form2 is trying to access form2 so they are just in an infinite loop.

i think you can make a middle-man type class that references both forms to do the functionality (it's always best to have a class that does most the functionality rather than doing it in the forms cs file, especially in bigger projects).

also what are you using the form2_Load() for ? the form2() is the constructor method and will execute anything in it when form 2 is loaded.

Think about this for a second.

In form1 you have a member of type form2

In form2 to you a member of type form1

When these classes are created they will follow this general order:
form1 created
has member form2 set to create new object on init, therefore construct form2
form2 created
has member form1 set to create new object on init, therefore construct form1
form1 #2 is created
has member form 2....

See where this is going? What's causing this to happen is the 'new' keyword. If you leave that out you won't have these circular calls. If you need a reference to the calling form (ie, not a NEW one) then pass it in the constructor of the form:

//Form2 code
Form1 _otherForm;
public Form2(Form1 otherForm)
{
   _otherForm=otherForm;
}
public void DoStuff()
{
   if (_otherForm != null)
       _otherForm.DoSomething();
}

//Form 1 code ->
//to construct a new form2, with a reference to this form
public MakeForm2()
{
   Form2 myOtherForm = new Form2(this);
   myOtherForm.Show();
}

Hey Just Use a static int & a timer.. It's Just toooooo Simple ... Make Sure u enable the timer , I have also attached the Application ZIP (If u need)

Form1
------

public static int trackbarvalue = 100;

        public Form1()
        {
            InitializeComponent();
        }

       

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            trackbarvalue = trackBar1.Value;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Opacity = trackbarvalue * 0.01;
            trackBar1.Value = trackbarvalue;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2();
            f.Show();
        }

Form2
-----

private void trackBar1_Scroll(object sender, EventArgs e)
        {
            Form1.trackbarvalue = trackBar1.Value;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Opacity = Form1.trackbarvalue * 0.01;
            trackBar1.Value = Form1.trackbarvalue;
        }

Is it possible without timer?

Create a separate .cs file for each of the form (not the one when you right click on a form control and click view source) and call them something like Frm1Functions and Frm1Functions, then from Frm1Functions call form2 and from Frm2Functions call form2. this means that the actual form are constructed when the form is instantiated (done in the rightclick view source cs file and not the ones you've created) this means that the forms wont be in a constant loop anymore.

Hope this makes sense and is helpful . . .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.