-1

I have two Forms Form1 and Form2, both of them are already "connected" with eachother. I am already passing button signals, trackbar values, timer ticks..., between them.

The connections looks like this inside Form1:

private void Form1_Load(object sender, EventArgs e)
    {
        Form2 = new Form2(timer1,btnBoost,btnBrake);
        Form2.Show();
    }

and inside Form2:

public Form2(Timer timer,Button Boost,Button Brake)
    {
        InitializeComponent();
        _timer = timer;
        _boost = Boost;
        _brake = Brake;          
    }

Now I would like to pass a variable from Form1 which is changing it's value every timertick to Form2, to create a graph.

Inside Form1 it looks like this

public partial class Form1 : Form {

public double ValueThatIWant;

}

Way done im giving it a value

private void Timer1_Tick1(object sender, EventArgs e){

ValueThatIWant = Math.Sqrt(somevalue.X,somevalue.Y);
}

I've already tried to access the variable by calling Form1 from Form2

Form1.valueThatIWant

but since

public double valueThatIWant

is declared public, it's value is always 0.

private void FillChart()
    {

       this.chart1.Series["Velocity"].Points.AddXY(time,Form1.ValueThatIWant);

    }

//That's the method I've created in Form2 to create a chart.

I would like to call the variable from inside(?) the

public Form1()

method, so that i get the changing value, and not only 0.

Hope that kinda describes my problem.

Thanks in advance!

  • 1
    "is declared public, it's value is always 0." huuum? The access-modifier has absoluteley no effect on the value of the field. Of course you have to assign a value to the field somewhere. However I don´t see any code where you´re doing this. – MakePeaceGreatAgain Jun 20 '19 at 09:53
  • We need a [mre] here. We cannot guess what you did, you need to show us. – Camilo Terevinto Jun 20 '19 at 09:57
  • Soo I've declared my variable in Form1 as public double ValueThatIWant. Way done i pass this variable a value, every timer tick. Inside Form1 i can work with it just fine, but from Form2 the variable seems to be always 0. – Hannes Hauser Jun 20 '19 at 09:58
  • How did you "connect" your two classes and pass the signals and values to one another? You need the correct instance of your `Form1`. I suppose in your `Form2` you have created a new instance of `Form1`, which is not related to the instance where you set the value. However it´s hard to help yopu any further without seeing how this "connection" looks like. – MakePeaceGreatAgain Jun 20 '19 at 09:58
  • made some edits hope this helps – Hannes Hauser Jun 20 '19 at 10:04

1 Answers1

0

You need access to the instance where you change the values. In your code Form2 does not know anything about that instance except btnBoost and btnBreak. You need to provide a reference to your Form1-instance to your constructor of Form2:

public Form2(Timer timer, Form1 f1, Button Boost, Button Brake)
{
    InitializeComponent();
    _form1 = f1;
    _timer = timer;
    _boost = Boost;
    _brake = Brake;          
}

In your Form1-code you now need this:

private void Form1_Load(object sender, EventArgs e)
{
    Form2 = new Form2(timer1,this,btnBoost,btnBrake);
    Form2.Show();
}

instead of:

private void Form1_Load(object sender, EventArgs e)
{
    Form2 = new Form2(timer1,btnBoost,btnBrake);
    Form2.Show();
}

Now you can access _form1.ValueIWant within Form2.

Furthermore you could also omit the two buttons from the constructor, as they probably are components of Form1 anyway. In this case you need to make the appropriate fields within Form1 public properties, however:

class Form1
{
    public Button BreakButton => btnBreak;
}

in which way you can now use _form1.BreakButton.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • Oh ok, I get the problem. I've done as you said and created a private Form1 f1 in Form2 and furthermore provided a reference _form1 = f1. But I'm not sure how to proceed in Form1? Would be glad if you could help... – Hannes Hauser Jun 20 '19 at 10:21
  • @HannesHauser I don´t get what you mean by "proceed further". What do you want to achieve? – MakePeaceGreatAgain Jun 20 '19 at 10:26
  • Inside my method Form1_Load I've written Form2 = new Form2(timer1,btnBoost,btnBrake). Visual studio ask me for an argument that matches Form1 f1... Hope you get what i mean! – Hannes Hauser Jun 20 '19 at 10:29
  • Hell yes it works!! You're a life saver. Also _form1.Button works too! Great thank you very much. – Hannes Hauser Jun 20 '19 at 10:38
  • Hey, i have a follow-up question! I would like to pass on a class of form1 to form2. I've tried it with this.ClassWhatever but it doens't work. If you could help me again that would be greatly appreciated! – Hannes Hauser Jun 20 '19 at 19:41
  • @HannesHauser That´s going to be far too broad for this question. Apart from this you should read some basics (e.g. a tutorial) about object-oriented programming to understand the difference between classes, instances, and how to pass them to one another. And last: "doesn´t work" is **very** unspecific and thus a bad term here on SO. – MakePeaceGreatAgain Jun 21 '19 at 06:45