WxSlider step intervals

From WxWiki

Jump to: navigation, search

A quick way to enable step intervals in wxSlider is to intercept the scroll event and override the slider's value to conform to some incremental value.

Catch the EVT_COMMAND_SCROLL event:

BEGIN_EVENT_TABLE ( MyDialog, wxDialog )
  EVT_COMMAND_SCROLL(SLIDER_mySliderID, MyDialog::OnSliderChange)
END_EVENT_TABLE()

Handle the step interval adjustment:

void MyDialog::OnSliderChange(wxScrollEvent &event) {

// _mySlider stored as a class member variable. Could also be fetched from the event.
int val = _mySlider->GetValue();

int remainder = val % 25; // The step interval. Specify your value here.

// If the value is not evenly divisible by the step interval,
// snap the value to an even interval.
if (remainder != 0){ 
  val -= remainder; 
  _mySlider->SetValue(val);
}
}

A similar technique may also work with wxSpinCtrl...

Personal tools