1

I am developing a MFC based SDI VC++ application. In my application I need to specify negative range value for my Slider Control. The minimum range for the Slider is -40 and maximum range is 255. I tried it using SetRange function. But it is not working. How can I set this range in the slider?Please Help..

My code for the Slider is as follows: Slider is declared as

CSliderCtrl m_ctrlECTSlider;

OnInitialUpdate function contains

m_ctrlECTSlider.SetRangeMin(-40);
int iValMin = m_ctrlECTSlider.GetRangeMin();
m_ctrlECTSlider.SetRangeMax(255);
int iValMax = m_ctrlECTSlider.GetRangeMax();

m_ctrlECTSlider.SetPos(0);
SetDlgItemInt( IDC_ECT_VALUE, m_ctrlECTSlider.GetPos(), FALSE);
SetDlgItemInt( IDC_MIN_ECT, iValMin, FALSE);
SetDlgItemInt( IDC_MAX_ECT, iValMax, FALSE);

OnBnClickedSet function contains

int nMin = GetDlgItemInt(IDC_MIN_ECT, 0, FALSE);
int nMax = GetDlgItemInt(IDC_MAX_ECT, 0, FALSE);

m_ctrlECTSlider.SetRange(nMin, nMax);
m_ctrlECTSlider.SetPos(nMin);

int pos = m_ctrlECTSlider.GetPos();
SetDlgItemInt(IDC_ECT_VALUE, m_ctrlECTSlider.GetPos(), FALSE);      
m_ctrlECTSlider.RedrawWindow();            

Here the problem is the value returned during debugg is all correct. But when it comes to UI on running the min value is 429496 and not -40 in the edit box..Why is it so..I am developing SDI application using CFormView class..

Thanks in advance.

Ferin Jose
  • 45
  • 1
  • 2
  • 9

2 Answers2

2

Your minimum value is '-40'which is signed integer and You are treating as unsigned integer as last parameter of 'SetDlgItemInt' and 'GetDlgItemInt' method is 'FALSE'. That's why you are getting wrong value in place of '-40'. Use following code for correct behavior.

//For setting value

SetDlgItemInt( IDC_ECT_VALUE, m_ctrlECTSlider.GetPos(), TRUE);

SetDlgItemInt( IDC_MIN_ECT, iValMin, TRUE);

SetDlgItemInt( IDC_MAX_ECT, iValMax, TRUE);

// For getting value use as follows

int nMin = GetDlgItemInt(IDC_MIN_ECT, 0, TRUE);

int nMax = GetDlgItemInt(IDC_MAX_ECT, 0, TRUE);

You can use this link http://msdn.microsoft.com/en-us/library/c7t43w0s%28v=vs.90%29.aspx for more details.

saroj kumar
  • 230
  • 1
  • 7
0

Call void SetRangeMin(int nMin, BOOL bRedraw = FALSE); as :

m_Slider.SetRangeMin(0);

And call void SetRangeMax(int nMax, BOOL bRedraw = FALSE); as :

m_Slider.SetRangeMax(50);

Also before calling GetPos() verify that the slider is in the range by calling the VerifyPos()

This is good tutorial on MFC Sliders - Windows Controls: Sliders (Track Bars).

Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112