-
Dec 21st, 2020, 07:56 AM
#1
Thread Starter
Lively Member
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.
-
Dec 21st, 2020, 08:53 AM
#2
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:
Private Sub TrackBar1_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar1.ValueChanged
Console.WriteLine("TrackBar1_ValueChanged")
TextBox1.Text = TrackBar1.Value.ToString()
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Console.WriteLine("TextBox1_TextChanged")
Dim value As Integer
If Integer.TryParse(TextBox1.Text, value) AndAlso
value >= TrackBar1.Minimum AndAlso
value <= TrackBar1.Maximum Then
TrackBar1.Value = value
End If
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.
-
Dec 21st, 2020, 10:09 AM
#3
Thread Starter
Lively Member
Re: Chain Reactions!
Originally Posted by jmcilhinney
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 ...
-
Dec 21st, 2020, 11:43 AM
#4
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.
-
Dec 21st, 2020, 12:38 PM
#5
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
-
Dec 21st, 2020, 12:53 PM
#6
Re: Chain Reactions!
Originally Posted by wavering
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
-
Dec 21st, 2020, 01:03 PM
#7
Re: Chain Reactions!
Originally Posted by passel
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:
Public Property FirstName() As String
Get
Return Me._firstName
End Get
Set(ByVal value As String)
If value <> Me._firstName Then
Me._firstName = value
RaiseEvent FirstNameChanged(Me, EventArgs.Empty)
End If
End Set
End Property
-
Dec 21st, 2020, 02:35 PM
#8
Re: Chain Reactions!
Originally Posted by Shaggy Hiker
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!
-
Dec 21st, 2020, 03:02 PM
#9
Re: Chain Reactions!
Originally Posted by jmcilhinney
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)
-
Dec 22nd, 2020, 04:37 AM
#10
Thread Starter
Lively Member
Re: Chain Reactions!
Originally Posted by passel
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!
-
Mar 16th, 2021, 04:14 PM
#11
Hyperactive Member
Re: Chain Reactions!
Originally Posted by Shaggy Hiker
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|