Results 1 to 11 of 11

Thread: Chain Reactions!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Posts
    87

    Chain Reactions!

    I have a track bar which when it is moved updates a text box to display the actual value. Or if I input to the text box it needs to update the trackbar.

    The problem is (I think) that they each fire off the other backwards and forwards in a sort of chain reaction! I am sure there must be a simple way of handling (no pun intended) this situation without using a whole lot of static values.

    Any ideas?
    Last edited by wavering; Dec 21st, 2020 at 08:30 AM.

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

    Re: Chain Reactions!

    How can we tell you what you did wrong if we don't know what you did? ALWAYS provide all the relevant information, which almost always means the code that causes the issue and, in this case, evidence of a StackOverflowException. If there is no such exception the there is no issue.
    vb.net Code:
    1. Private Sub TrackBar1_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar1.ValueChanged
    2.     Console.WriteLine("TrackBar1_ValueChanged")
    3.  
    4.     TextBox1.Text = TrackBar1.Value.ToString()
    5. End Sub
    6.  
    7. Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    8.     Console.WriteLine("TextBox1_TextChanged")
    9.  
    10.     Dim value As Integer
    11.  
    12.     If Integer.TryParse(TextBox1.Text, value) AndAlso
    13.        value >= TrackBar1.Minimum AndAlso
    14.        value <= TrackBar1.Maximum Then
    15.         TrackBar1.Value = value
    16.     End If
    17. End Sub
    That code works exactly as you'd expect and you can see it do so if you watch the Output window while debugging.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Posts
    87

    Re: Chain Reactions!

    Quote Originally Posted by jmcilhinney View Post
    How can we tell you what you did wrong if we don't know what you did?
    Because the project is still in the planning stage so there is no code yet for this aspect but I foresaw this a potential problem! Sorry to have wasted your time ...

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

    Re: Chain Reactions!

    It took me a few minutes to throw together the demo above to show that it would work without issue. You really ought to have done that for yourself if you weren't sure what would happen. Many people seem to ignore the fact that they can create little test projects as often as they like to test specific things like this. You don;t have to build something into your major project to see whether it will work. Any time something is giving you trouble, creating a test project to isolate just that thing should be one of the first things you consider doing.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Chain Reactions!

    I prefer to have one, utterly messy, test project. For WinForms, it just has a couple forms. One is for testing code constructs for speed. That form doesn't change much. The other form has whatever control I wanted to test stuck wherever there was some room for it. By now it's a jumble of the remnants of many past tests. Kind of like a scrap book with more scrap than book.
    My usual boring signature: Nothing

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Chain Reactions!

    Quote Originally Posted by wavering View Post
    I have a track bar which when it is moved updates a text box to display the actual value. Or if I input to the text box it needs to update the trackbar.

    The problem is (I think) that they each fire off the other backwards and forwards in a sort of chain reaction! I am sure there must be a simple way of handling (no pun intended) this situation without using a whole lot of static values.

    Any ideas?
    Only if they actually change the value.
    If you set the value to the value it already is, then it doesn't generate a value changed event.
    You should get two hits on a paired value change event, unless the value is somehow converted differently between the controls so that the value is never the same between the two controls.
    e.g. if in the changed event you set the value to the value + 1, then the value would continue to increment as it ping-ponged back and forth.

    In fact, I'm pretty sure I've seen the case where it can do that with only one control, i.e. changing the value in the value changed event handler causes another value changed event, so the control gets in a endless loop with itself.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Chain Reactions!

    Quote Originally Posted by passel View Post
    If you set the value to the value it already is, then it doesn't generate a value changed event.
    Indeed. Here's an example from a blog post of mine that shows how exactly that's implemented:
    vb.net Code:
    1. Public Property FirstName() As String
    2.     Get
    3.         Return Me._firstName
    4.     End Get
    5.     Set(ByVal value As String)
    6.         If value <> Me._firstName Then
    7.             Me._firstName = value
    8.  
    9.             RaiseEvent FirstNameChanged(Me, EventArgs.Empty)
    10.         End If
    11.     End Set
    12. End Property

  8. #8
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,834

    Re: Chain Reactions!

    Quote Originally Posted by Shaggy Hiker View Post
    I prefer to have one, utterly messy, test project. For WinForms, it just has a couple forms. One is for testing code constructs for speed. That form doesn't change much. The other form has whatever control I wanted to test stuck wherever there was some room for it. By now it's a jumble of the remnants of many past tests. Kind of like a scrap book with more scrap than book.
    I agree with jmcihinney...I call it proof of concept myself but on a small scale. Some of our systems require you to really drill down through the application to get the affected code. It is a serious waste of time to debug changes like that. I also have a large test project that I have all kinds of working examples. Some from jmcihinney. I can just cut and paste various code blocks.
    Please remember next time...elections matter!

  9. #9
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Chain Reactions!

    Quote Originally Posted by jmcilhinney View Post
    It took me a few minutes to throw together the demo above to show that it would work without issue. You really ought to have done that for yourself if you weren't sure what would happen. Many people seem to ignore the fact that they can create little test projects as often as they like to test specific things like this. You don;t have to build something into your major project to see whether it will work. Any time something is giving you trouble, creating a test project to isolate just that thing should be one of the first things you consider doing.
    I do that a lot. I have several small test projects full of controls that I use for tutorials, new ideas or to check an answer before posting. For major functionality I create a separate project on which I work until I integrate it to the main one when it is working.

    By the way like TysonLPrice, I think I have almost all jmcilhinney blog's code examples in such test projects. I have also a word file when I put all tips and tricks I get from forum or internet, it is full of bit of code. A sort of reminder/reference/fact sheet, whatever you call it in English (Aide-mémoire in French).
    Last edited by Delaney; Dec 21st, 2020 at 03:09 PM.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  10. #10

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Posts
    87

    Re: Chain Reactions!

    Quote Originally Posted by passel View Post
    Only if they actually change the value.

    If you set the value to the value it already is, then it doesn't generate a value changed event.

    You should get two hits on a paired value change event, unless the value is somehow converted differently between the controls so that the value is never the same between the two controls.
    e.g. if in the changed event you set the value to the value + 1, then the value would continue to increment as it ping-ponged back and forth.
    Thanks for the replies. I was really trying to establish whether it was good practice rather than if it appeared to work. It might appear to "work" for example if there was a cut off after maybe 50 iterations or whatever. The reply I have just quoted here makes the point that there needs to be a real change in vlaue rather than just for example changing "20" to "20" and considering that to be a change.

    So, thanks for the answers and the debate!

  11. #11
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    Re: Chain Reactions!

    Quote Originally Posted by Shaggy Hiker View Post
    I prefer to have one, utterly messy, test project. For WinForms, it just has a couple forms. One is for testing code constructs for speed. That form doesn't change much. The other form has whatever control I wanted to test stuck wherever there was some room for it. By now it's a jumble of the remnants of many past tests. Kind of like a scrap book with more scrap than book.
    Yeah, me too!
    Please dont forget to add good reputation if my advices were useful for you.
    How? Under this post there is "RATE THIS POST" button. Click on it.

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