# Slider text value other than Slider value

**URL:** <https://forum.juce.com/t/slider-text-value-other-than-slider-value/27298>\
**Category:** General JUCE discussion\
**Created:** [April 18, 2018, 7:00pm UTC](https://forum.juce.com/t/slider-text-value-other-than-slider-value/27298 "2018-04-18T19:00:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![pajczur](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@pajczur](https://forum.juce.com/u/pajczur)\
**Post date:** [April 18, 2018, 7:00pm UTC](https://forum.juce.com/t/slider-text-value-other-than-slider-value/27298/1 "2018-04-18T19:00:37Z")

</div>

Hello,  
probably it’s stupid question, but I’ve spent last 2 hours to find the solution, with no result.  
Everybody tell to use getTextFromValue(); or getValueFromText(); But nobody tells how exactly use it?  
Those methods just return value, but I don’t want to return anything. I want to set text value other than Slider value. What I mean: my Slider range is from 0.0 to 1.0. But I want text box to display from 20 Hz to 20000 Hz.  
Please could anyone help me?

---

<div class="post-metadata">

**Author:** ![alassandro](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/alassandro/32/4297_2.png) [@alassandro](https://forum.juce.com/u/alassandro)\
**Post date:** [April 18, 2018, 7:44pm UTC](https://forum.juce.com/t/slider-text-value-other-than-slider-value/27298/2 "2018-04-18T19:44:28Z")

</div>

If you want the slider to format the text it displays you can set the lambdas `juce::Slider::textFromValueFunction` and `juce::Slider::valueFromTextFunction` for your slider.

Using your range for a basic example:

```auto
mySlider.textFromValueFunction = [](double value)
{
    double freq = (value * (20000.0 - 20.0)) + 20.0;
    return juce::String(freq) + "Hz";
};

mySlider.valueFromTextFunction = [](const String &text)
{
    double freq = text.removeCharacters("Hz").getDoubleValue();
    return (freq - 20.0) / (20000.0 - 20.0);
};

```

The `valueFromTextFunction` can allow the user to use your units & formatted range when they enter in values. In this case typing `10010Hz` into the text box would display that frequency while setting the slider’s actual value to `0.5`

---

<div class="post-metadata">

**Author:** ![pajczur](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@pajczur](https://forum.juce.com/u/pajczur)\
**Post date:** [April 18, 2018, 7:53pm UTC](https://forum.juce.com/t/slider-text-value-other-than-slider-value/27298/3 "2018-04-18T19:53:49Z")

</div>

Hello,  
great thanks for reply and for help. Probably first I should try your code and then ask additional question. But my first impression is: When I do that code I have lambda functions stored in `mySlider.textFromValueFunction` and in `mySlider.valueFromTextFunction`, but both of those lambda functions return some value. So anytime I call `mySlider.textFromValueFunction(someValue);` and `mySlider.valueFromTextFunction("some text");` they return values. But how to put those values in the text box. That is what I am asking for.

---

<div class="post-metadata">

**Author:** ![alassandro](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/alassandro/32/4297_2.png) [@alassandro](https://forum.juce.com/u/alassandro)\
**Post date:** [April 18, 2018, 7:57pm UTC](https://forum.juce.com/t/slider-text-value-other-than-slider-value/27298/4 "2018-04-18T19:57:53Z")

</div>

`textFromValueFunction` returns a `juce::String`, which the slider will put into its text box

---

<div class="post-metadata">

**Author:** ![pajczur](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@pajczur](https://forum.juce.com/u/pajczur)\
**Post date:** [April 18, 2018, 8:03pm UTC](https://forum.juce.com/t/slider-text-value-other-than-slider-value/27298/5 "2018-04-18T20:03:19Z")

</div>

> [@TonyAtHarrison](#):
>
> mySlider.textFromValueFunction = (double value)  
> {  
> double freq = (value \* (20000.0 - 20.0)) + 20.0;  
> return juce::String(freq) + “Hz”;  
> };

Great man, it works. Thanks

---

<div class="post-metadata">

**Author:** ![pajczur](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@pajczur](https://forum.juce.com/u/pajczur)\
**Post date:** [April 18, 2018, 8:05pm UTC](https://forum.juce.com/t/slider-text-value-other-than-slider-value/27298/6 "2018-04-18T20:05:44Z")

</div>

But you should mention that after I create that lambda function I should use it by calling `mySlider.textFromValue(someValue);`  
Because at first I tried to call `mySlider.textFromValueFunction(someValue);` and nothing happened, just error 🙂

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [April 19, 2018, 12:18pm UTC](https://forum.juce.com/t/slider-text-value-other-than-slider-value/27298/7 "2018-04-19T12:18:16Z")

</div>

> [@pajczur](#):
>
> double freq = (value \* (20000.0 - 20.0)) + 20.0;

Just wanted to point you to a more readable approach using [jmap](https://docs.juce.com/develop/group __juce__ core-maths.html#ga5b7171b191704c949f942076d4d2a82c)

```auto
auto freq = jmap (value, 20.f, 20000.f);

auto v = jmap (freq, 20.f, 20000.f, 0.f, 1.f);

```

If you’re having trouble to compile, make sure that all arguments have the same type, since these templated functions are picky and don’t do implicit casts.

Another option is, let the range of the slider take care of it, but that would mean, that you set the Range to `Range<float> (20.f, 20000.f)` and all is sorted…

---

<div class="post-metadata">

**Author:** ![pajczur](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@pajczur](https://forum.juce.com/u/pajczur)\
**Post date:** [June 25, 2018, 6:07pm UTC](https://forum.juce.com/t/slider-text-value-other-than-slider-value/27298/8 "2018-06-25T18:07:59Z")

</div>

Hey everybody,  
It’s old subject, but now I’ve found one more issue.  
When I set:

> ```
> mySlider.textFromValueFunction = [](double value)
> {
> double freq = (value * (20000.0 - 20.0)) + 20.0;
> return juce::String(freq) + “Hz”;
> };
> 
> ```

Slider works great but unfortunately if I have editable textBox, and I edit text I get crazy values. It looks like that lambda calculates my edited value also. How to prevent that?

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [June 25, 2018, 10:02pm UTC](https://forum.juce.com/t/slider-text-value-other-than-slider-value/27298/9 "2018-06-25T22:02:19Z")

</div>

Look at @TonyAtHarrison’s answer, he gave you both needed functions.

---

<div class="post-metadata">

**Author:** ![pajczur](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@pajczur](https://forum.juce.com/u/pajczur)\
**Post date:** [June 30, 2018, 10:34am UTC](https://forum.juce.com/t/slider-text-value-other-than-slider-value/27298/10 "2018-06-30T10:34:38Z")

</div>

Yes I’ve already figured it out, thanks.
