I want to get the slider value when it changes.

Asked 2 years ago, Updated 2 years ago, 104 views

I would like to write a code for receiving volume from the screen as an event handler on C#.
I would like to get the volume from the slide bar as shown in the image below.
screen

This is a system that uses a slider to change the volume, so when the slider value changes, I want to get that value.

How do I write my values to other components?

c# wpf

2022-09-30 17:11

1 Answers

It's probably written in the article introduced in the comments.
C#WPF Path #18!Easy to understand how to write and use Slider
How to use WPF slider (scrollbar-like)

Microsoft's page is here.
Slider class

<Slider Width="200"
        TicketPlacement="Both"
        Foreground="Black"
        Margin="10"
        IsSnapToTickEnabled="True"
        TickFrequency="10"
        SmallChange="20"
        LargeChange="50"
        Minimum="0"
        Maximum="100"
        ValueChanged = "Slider_ValueChanged"/>
<StackPanel Orientation="Horizontal"HorizontalAlignment="Center">
    <TextBlock Text="SliderValue:"/>
    <TextBlock x:Name="ASlider"/>
</ StackPanel>
  • In the RoutedPropertyChangedEventArgs parameter at event notification, NewValue property contains a new value, so use it.If necessary, retrieve the pre-modification value from the OldValue property.
    The following ASlider.Text=e.NewValue.ToString(); portion
private void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgse)
{
    ASlider.Text=e.NewValue.ToString();
}
  • Slider control's own Value(RangeBase.Value) property may be acceptable if you just read it, but rewriting the value in the event handler will cause an event to occur again and cause a loop.
    The SliderValue.Text=slider.Value.ToString(); portion of the second article above
private void slider_ValueChanged (object sender, RoutedPropertyChangedEventArgs<double>e)
{
    SliderValue.Text=slider.Value.ToString();
}

I will add the questions before they are updated, but you probably want to update the screen (UI component) from within the event handler.
You can use BeginInvoke, Invoke, Invoke, InvokeAsync, and so on in Microsoft and other articles below.
Thread Model

However, no matter how well designed, a single threaded solution cannot be provided in the UI framework for any type of problem. WPF is just one step away, but there are still situations where user interface (UI) responsiveness or application performance improves across multiple threads.

Note
This topic describes thread processing using the BeginInvoke method for asynchronous calls. You can also invoke the InvokeAsync method to receive Action or Func as a parameter to make an asynchronous call... ...abbreviated...Invoke method may also receive Action or Func as a parameter... ...abbreviated

Tried learning about Dispatcher in WPF

WPF uses a "single-threaded model" in which most objects run on UI threads, so access to the object from outside the UI thread results in an exception.
For example, adding items to ListBox in parallel as shown below throws InvalidOperationException.

private void button1_Click(object sender, RoutedEventArgse){
    listBox1.Items.Clear();

    Parallel.For(0,10000,(i)=>{
        listBox1.Items.Add(i);//Exception here
    });
}

Dispatcher.BeginInvoke provides asynchronous access to View elements.

private void button1_Click(object sender, RoutedEventArgse){
    listBox1.Items.Clear();

    Parallel.For(0,10000,(i)=>{
        listBox1.Dispatcher.BeginInvoke(
             new Action(()=>{
                    listBox1.Items.Add(i);
             })
        );
    });
}


2022-09-30 17:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.