|
-
Jul 2nd, 2009, 09:41 PM
#1
Thread Starter
Lively Member
"interaction" between two from
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
-
Jul 2nd, 2009, 10:29 PM
#2
Re: "interaction" between two from
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.
-
Jul 3rd, 2009, 12:17 AM
#3
Thread Starter
Lively Member
Re: "interaction" between two from
 Originally Posted by jmcilhinney
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.
Could you please provide me the example?
-
Jul 3rd, 2009, 12:23 AM
#4
Re: "interaction" between two from
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?
-
Jul 3rd, 2009, 12:43 AM
#5
Thread Starter
Lively Member
Re: "interaction" between two from
 Originally Posted by jmcilhinney
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?
yup i have no idea how to define a method for TrackBar.
in form 1 :
Code:
private void trackBar1_Scroll(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
trackBar1.Value= frm2.
}
-
Jul 3rd, 2009, 12:47 AM
#6
Re: "interaction" between two from
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:
define a method in each form that takes an int and assigns it to the TrackBar's Value property
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.
-
Jul 3rd, 2009, 12:52 AM
#7
Thread Starter
Lively Member
Re: "interaction" between two from
 Originally Posted by jmcilhinney
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.
is it something like this?
Code:
int TrackRecord;
trackbar1.value=TrackRecord;
by the way, how to access the control in form1 (e.g. button1) from Form2
-
Jul 3rd, 2009, 12:59 AM
#8
Re: "interaction" between two from
 Originally Posted by yamaha102
by the way, how to access the control in form1 (e.g. button1) from Form2
Again with the getting ahead. This how people confuse themselves. You DON'T access the control in Form1 from Form2.
Do you know how to declare a method?
define a method in each form that takes an int
-
Jul 3rd, 2009, 01:03 AM
#9
Thread Starter
Lively Member
Re: "interaction" between two from
 Originally Posted by jmcilhinney
Again with the getting ahead. This how people confuse themselves. You DON'T access the control in Form1 from Form2.
Do you know how to declare a method?
I don't know how to declare it. as below ?
Private void myTrackbar(int m)
{
//
}
-
Jul 3rd, 2009, 01:09 AM
#10
Re: "interaction" between two from
 Originally Posted by yamaha102
I don't know how to declare it. as below ?
Private void myTrackbar(int m)
{
//
}
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:
define a method in each form that takes an int and assigns it to the TrackBar's Value property
-
Jul 3rd, 2009, 01:14 AM
#11
Thread Starter
Lively Member
Re: "interaction" between two from
 Originally Posted by jmcilhinney
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:
Code:
public void trackbarValue (int m)
{
trackbar1.value=m;
}
is it correct? should i put the method in form1 or form2?
what's the next step?
thanks for your time.
-
Jul 3rd, 2009, 01:47 AM
#12
Re: "interaction" between two from
Yes it is, and you've done it for yourself. 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?
should i put the method in form1 or form2?
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.
-
Jul 3rd, 2009, 01:55 AM
#13
Thread Starter
Lively Member
Re: "interaction" between two from
 Originally Posted by jmcilhinney
Yes it is, and you've done it for yourself.  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?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.
Both ways.
so next step, in each form,
Code:
private void trackBar1_Scroll(object sender, EventArgs e)
{
trackBar1.Value =????
}
please guide
-
Jul 3rd, 2009, 01:59 AM
#14
Re: "interaction" between two from
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?
-
Jul 3rd, 2009, 02:02 AM
#15
Thread Starter
Lively Member
Re: "interaction" between two from
 Originally Posted by jmcilhinney
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?
in Form 1, i insert
Code:
Form2 frm2 =New Form2();
frm2.trackBar1 ???
I have no idea how to continue
-
Jul 3rd, 2009, 02:15 AM
#16
Re: "interaction" between two from
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.
-
Jul 3rd, 2009, 02:29 AM
#17
Thread Starter
Lively Member
Re: "interaction" between two from
 Originally Posted by jmcilhinney
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.
i'm sorry. i am getting confused.
-
Jul 3rd, 2009, 03:42 AM
#18
Re: "interaction" between two from
 Originally Posted by yamaha102
i'm sorry. i am getting confused.
Which is exactly why you need to take it one step at a time. Look at what I said first:
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
Just do that for a start and then move onto the next part.
-
Jul 4th, 2009, 03:54 AM
#19
Thread Starter
Lively Member
Re: "interaction" between two from
 Originally Posted by jmcilhinney
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.
Thanks. Hope you could continue to guide me...
is it like below?
Form1:
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)
{
}
}
}
FORM2:
Code:
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void trackbarValue(int trackValue)
{
form2trackBar.Value = trackValue;
}
}
}
-
Jul 4th, 2009, 04:53 AM
#20
Re: "interaction" between two from
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.
-
Jul 4th, 2009, 08:09 AM
#21
Thread Starter
Lively Member
Re: "interaction" between two from
 Originally Posted by jmcilhinney
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.
As below? Actually i dont get what you mean. Think i'm lost somehow.
Code:
public Form2()
{
InitializeComponent();
int intMM;
this.form1trackBar.Value = intMM;
}
-
Jul 4th, 2009, 08:23 AM
#22
Re: "interaction" between two from
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?
-
Jul 4th, 2009, 08:58 AM
#23
Thread Starter
Lively Member
Re: "interaction" between two from
 Originally Posted by jmcilhinney
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?
Code:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
int intKK; //declare a member variable.
}
public void trackbarValue(int m)
{
form1trackBar.Value = m;
}
}
}
namespace WindowsFormsApplication1
{
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
-
Jul 4th, 2009, 10:47 AM
#24
Re: "interaction" between two from
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; } } }
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|