locked
Set slider maximum programatically, c# RRS feed

  • Question

  • Hello,

    I'm new to c# (and wpf) and I have a very simple question.

    I am trying to set the maximum value of a slider in c# as opposed to doing so in XAML.  As a test I wrote a very simple function that will set an initial maximum value and then reset the value after some data is read in c# (new data is returned from a database query).

    The simple test function is triggered (only as a test) when data is returned. I have discovered that the function will successfully set the initial slider value but when triggered a second time (by the query return - causing the maximum value to change), the update causes the WPF application to crash.

    What am I doing wrong?

    // // //

    private Int32 temp_cache = 0;

    private void updateMySliderMax()
    {
        Int32 some_slider_max = 40;
        Int32 a_reduced_test_value = 4;

        if (temp_cache != some_slider_max)
        {
            My_Slider.Value = 20;
            My_Slider.Maximum = some_slider_max;
            temp_cache = some_slider_max;
        }
        else
        {
            My_Slider.Value = 2;
            My_Slider.Maximum = a_reduced_test_value;
        }
     }

    Friday, July 3, 2020 11:36 PM

All replies

  • Theoretically, the else block should include ‘temp_cache = a_reduced_test_value’. It seems to work. Do you have more details about the issue?

    Saturday, July 4, 2020 5:46 AM
  • Hi tree28,

    Thanks for posting here.

    Since this issue is more related to WPF, we suggest to redirect to Microsoft Q&A forum with wpf tag, and then you will get deeicated support about it.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Monday, July 6, 2020 9:00 AM