0

I'm trying to apply a formula in code. But this formula won't work unless I change the starting tick for the TrackBar (without changing my min tick value).

For example:

TRmin = 1 
Trmax = 10 

instead of starting from 1 i need to start from 2 or 3

Blackwood
  • 4,504
  • 16
  • 32
  • 41
  • What the heck are you talking about? Are we supposed to just know what this all means? – rory.ap Jul 05 '16 at 19:20
  • hey @roryap I'm sorry if this is confusing to you. i'm working on something that require to change the star tick value of the track bar. for ex i have Track bar with min = 1 max = 10 and my freq = 1 so the track bar have 10 tick (or 10 point of change) all what i want is how i can start the bar from the 2 or 3 point without change the min value – Alaa Noah Jul 05 '16 at 19:26
  • Have you considered using `Math.Min(trackBar.Value, 2)` for your formula? Or do you want to prevent the user from moving the trackbar to values lower than 2 or 3? (Please choose one of those values or indicate how it is determined.) – Andrew Morton Jul 05 '16 at 19:52
  • No, I didn't used it `Math.Min(trackBar.Value, 2)`. BUT like you said i want to prevent the user from moving the taskbar to values lower than 2 or 3 – Alaa Noah Jul 05 '16 at 19:58
  • Subscribe to its `ValueChanged` event and then programmatically set the value to what Andrew wrote. – Visual Vincent Jul 05 '16 at 21:06
  • It's the `Scroll` event for a TrackBar, and I should have written Math.Max, so `trackbar1.Value = Math.Max(3, trackbar1.Value)`. – Andrew Morton Jul 06 '16 at 17:04
  • @AndrewMorton : It actually contains the `ValueChanged` event too. The difference between them is that `Scroll` is only fired when the TrackBar is actually scrolled by the user (via the mouse/keyboard), while `ValueChanged` is fired whenever the `Value` property has changed (which means it reacts to programmatic change too). – Visual Vincent Jul 07 '16 at 22:43
  • 1
    @VisualVincent I was confused by the non-appearance of the ValueChanged event in the designer's properties events pane in the "Property Changed" section. Although it does appear in the properties properties pane under Behavior->Load once there is a handler in the code. I thought maybe there would be some problem changing the value in the ValueChanged handler. A quick test suggests that sometimes the handler gets called more than once. I've edited my answer. – Andrew Morton Jul 08 '16 at 08:30

1 Answers1

1

You can use the TrackBar.Scroll event to limit the value:

Option Strict On
Option Infer On
' ...
Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
    Dim tb = DirectCast(sender, TrackBar)
    tb.Value = Math.Max(3, tb.Value)
    lbTrackBar1Value.Text = tb.Value.ToString()
    lbResult.Text = Calc(tb.Value).ToString()

End Sub

Which supposes that you have labels to display the trackbar value and the result of some function Calc.

I used Dim tb = DirectCast(sender, TrackBar) so it is easier to generalise the method to use for other trackbars.

Or you can use the TrackBar.ValueChanged Event, which has the feature that it will also be raised if you set the value programatically, as Visual Vincent was kind enough to point out.

If it matters that the handler is sometimes called more than once on a Scroll or ValueChanged event, then you can guard against running the code more than once like this:

Sub TrackBar1_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar1.ValueChanged
    Static inVC As Boolean = False
    If inVC Then Return
    Dim tb = DirectCast(sender, TrackBar)
    inVC = True
    tb.Value = Math.Max(3, tb.Value)
    inVC = False

End Sub

The local static variable inVC is initialised only once and retains its value between invocations.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • Hmm, interesting that `ValueChanged` sometimes get fired multiple times... I wonder why. – Visual Vincent Jul 08 '16 at 08:57
  • @VisualVincent The `Scroll` event can be fired multiple times too. I assume your musing as to why the `ValueChanged` handler gets called again when the value is changed within the handler was rhetorical ;) – Andrew Morton Jul 08 '16 at 09:05
  • Oh, so you only meant it gets called again because you set the `Value` property? I thought you meant that sometimes when you change it, it fires for example two times instead of one. – Visual Vincent Jul 08 '16 at 09:13
  • 1
    @VisualVincent Yes. One could instead use `RemoveHandler` and `AddHandler`, but checking a variable is a little bit less work for the computer, and perhaps simpler if the handler can be used by several events. It could of course be that in some situations it doesn't matter that the handler can be called more than once. – Andrew Morton Jul 08 '16 at 09:29
  • 1
    @VisualVincent I found some relevant SO material which you might find interesting: [How to prevent firing CheckedChanged event when checking a control programmatically?](http://stackoverflow.com/q/8089072/1115360) and [EventHandler and Delegate misunderstanding](http://stackoverflow.com/a/15074895/1115360). – Andrew Morton Jul 08 '16 at 09:31