Results 1 to 24 of 24

Thread: "interaction" between two from

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    92

    "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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    92

    Re: "interaction" between two from

    Quote Originally Posted by jmcilhinney View Post
    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?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    92

    Re: "interaction" between two from

    Quote Originally Posted by jmcilhinney View Post
    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.
            }

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    92

    Re: "interaction" between two from

    Quote Originally Posted by jmcilhinney View Post
    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

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: "interaction" between two from

    Quote Originally Posted by yamaha102 View Post
    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
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    92

    Re: "interaction" between two from

    Quote Originally Posted by jmcilhinney View Post
    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)
    {
    //
    }

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: "interaction" between two from

    Quote Originally Posted by yamaha102 View Post
    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
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    92

    Re: "interaction" between two from

    Quote Originally Posted by jmcilhinney View Post
    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.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: "interaction" between two from

    is it correct?
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    92

    Re: "interaction" between two from

    Quote Originally Posted by jmcilhinney View Post
    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

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    92

    Re: "interaction" between two from

    Quote Originally Posted by jmcilhinney View Post

    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

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    92

    Re: "interaction" between two from

    Quote Originally Posted by jmcilhinney View Post
    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.

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: "interaction" between two from

    Quote Originally Posted by yamaha102 View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    92

    Re: "interaction" between two from

    Quote Originally Posted by jmcilhinney View Post
    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;
    
            }
    
        }
    }

  20. #20
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  21. #21

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    92

    Re: "interaction" between two from

    Quote Originally Posted by jmcilhinney View Post
    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;
    
            }

  22. #22
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    92

    Re: "interaction" between two from

    Quote Originally Posted by jmcilhinney View Post
    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

  24. #24
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: "interaction" between two from

    csharp Code:
    1. using System;
    2. using System.Windows.Forms;
    3.  
    4. namespace WindowsFormsApplication1
    5. {
    6.     public partial class Form1 : Form
    7.     {
    8.         private Form2 f2;
    9.  
    10.         public Form1()
    11.         {
    12.             InitializeComponent();
    13.         }
    14.  
    15.         private void button1_Click(object sender, EventArgs e)
    16.         {
    17.             this.f2 = new Form2(this);
    18.             f2.Show();
    19.         }
    20.  
    21.         private void trackBar1_Scroll(object sender, EventArgs e)
    22.         {
    23.             this.f2.SetTrackBarValue(this.trackBar1.Value);
    24.         }
    25.  
    26.         public void SetTrackBarValue(int value)
    27.         {
    28.             this.trackBar1.Value = value;
    29.         }
    30.     }
    31. }
    csharp Code:
    1. using System;
    2. using System.Windows.Forms;
    3.  
    4. namespace WindowsFormsApplication1
    5. {
    6.     public partial class Form2 : Form
    7.     {
    8.         private Form1 f1;
    9.  
    10.         public Form2(Form1 f1)
    11.         {
    12.             InitializeComponent();
    13.  
    14.             this.f1 = f1;
    15.         }
    16.  
    17.         private void trackBar1_Scroll(object sender, EventArgs e)
    18.         {
    19.             this.f1.SetTrackBarValue(this.trackBar1.Value);
    20.         }
    21.  
    22.         public void SetTrackBarValue(int value)
    23.         {
    24.             this.trackBar1.Value = value;
    25.         }
    26.     }
    27. }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width