Hi, say i have 2 forms , form1 and form2.
both forms have a trackbar respectively.
I wish to link the two trackbars, meaning when i drag on the trackbar in form1, the trackbar in form2 itself will be move as well.
Thanks
Printable View
Hi, say i have 2 forms , form1 and form2.
both forms have a trackbar respectively.
I wish to link the two trackbars, meaning when i drag on the trackbar in form1, the trackbar in form2 itself will be move as well.
Thanks
You should define a method in each form that takes an int and assigns it to the TrackBar's Value property. You should then handle the ValueChanged event of each TrackBar and then call that method of the other form.
That means that each form must have a reference to the other. Presumably Form1 creates Form2 so, in that case, Form1 can simply assign the Form2 instance to a member variable when it creates it. For the other direction, Form1 will have to pass Form2 a reference to itself, i.e. this, when it creates it. Form2 can then assign that reference to a member variable.
You're not really asking for an example, are you? You're asking for it to be done for you. How about you have a go at it for yourself and then ask about the parts that give you trouble? The first step I mentioned was to declare a method in each form that will set the TrackBar's Value property. Can you do that?
You're trying to leap ahead. Don't try to implement the last steps if you can't implement the first steps. Read what I posted again and do that:I didn't say anything about handling events of the TrackBar or creating a new form. Do just what I posted. Once that's done, then you can move on to the next step.Quote:
define a method in each form that takes an int and assigns it to the TrackBar's Value property
That's getting there. Two things:
1. The method can't be private because it needs to be called from outside the form.
2. You wouldn't name it 'myTrackbar'. You would give it a name that indicates what it does. As I said in my previous posts, this method will be used to set the Value of the TrackBar so the name should indicate that.
Once you've fixed those issues, then you can add the method body:Quote:
define a method in each form that takes an int and assigns it to the TrackBar's Value property
Yes it is, and you've done it for yourself. :thumb: Just with regards to the method name again though, 'trackbarValue' is still not representative of the methods purpose. If you knew nothing about the project would you know that 'trackbarValue' meant that it was setting that value?Quote:
is it correct?
You should put it in whichever form(s) need to have their TrackBar's Value set from outside. Can I just confirm, do you want this to work both ways or only one, i.e. if the TrackBar changes in Form2 do you want Form1 to update or just the other way? If it's only one way then you only need to put it in one form but, if it's both ways, then you need to put it in both forms.Quote:
should i put the method in form1 or form2?
No, that's not the next step. Once you've got that method you declared in both forms, the next step is to make sure that each form has a reference to the other. Form1 can't make anything happen on Form2 if it has no way to refer to Form2 and vice versa.
So, the next step is for Form1 to declare a member variable of type Form2 and, when it creates an instance of Form2, it must assign it to that variable. How are you creating your Form2 instance at the moment?
Instead of assigning your new Form2 instance to a local variable, i.e. one declared inside a method, you need to assign it to a member variable, i.e. one declared outside all methods and therefore available to all. You can then handle the ValueChanged event of the TrackBar in Form1 and call the method that you declared in Form2 using that member variable.
Which is exactly why you need to take it one step at a time. Look at what I said first:Just do that for a start and then move onto the next part.Quote:
Instead of assigning your new Form2 instance to a local variable, i.e. one declared inside a method, you need to assign it to a member variable, i.e. one declared outside all methods and therefore available to all
Thanks. Hope you could continue to guide me...
is it like below?
Form1:
FORM2:Code:namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
int intKK;
this.form1trackBar.Value = intKK;
}
public void trackbarValue(int m)
{
form1trackBar.Value = m;
}
private void form1trackBar_Scroll(object sender, EventArgs e)
{
}
}
}
Code:namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void trackbarValue(int trackValue)
{
form2trackBar.Value = trackValue;
}
}
}
The next step is to declare a member variable, i.e. a variable declared outside any method, in Form1 so that, when you create your new Form2 object, you can assign it to that variable. I don't see a Form2 variable in your Form1 code.
Please read carefully. In Form1, you need to declare a member variable. A local variable is one declared inside a method and a member variable is one declared outside all methods. When you create your Form2 object you assign it to that member variable. You're writing this code in Form1, OK?
namespace WindowsFormsApplication1Code:namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
int intKK; //declare a member variable.
}
public void trackbarValue(int m)
{
form1trackBar.Value = m;
}
}
}
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public form1 frm1;
public form2(form1 frm11)
{
InitializeComponent();
frm1 = frm11;
}
public void trackbarValue(int trackValue)
{
form2trackBar.Value = frm1.intKK; //assign it to that member variable.
}
}
}
Thanks
csharp Code:
using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { private Form2 f2; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { this.f2 = new Form2(this); f2.Show(); } private void trackBar1_Scroll(object sender, EventArgs e) { this.f2.SetTrackBarValue(this.trackBar1.Value); } public void SetTrackBarValue(int value) { this.trackBar1.Value = value; } } }csharp Code:
using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form2 : Form { private Form1 f1; public Form2(Form1 f1) { InitializeComponent(); this.f1 = f1; } private void trackBar1_Scroll(object sender, EventArgs e) { this.f1.SetTrackBarValue(this.trackBar1.Value); } public void SetTrackBarValue(int value) { this.trackBar1.Value = value; } } }