Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

I have placed a Slider control on the dialog box from the MFC Tools. What I want is when the user moves the slider, I want the position / value where user has placed the slider on track bar (at runtime) and Send that value to USB Port as well as display that value in an edit box.
I want the value as soon as user moves and places the slider. How to get the value at runtime?
Please suggest me something.
Posted

1 solution

Just handle the OnHScroll() or OnVScroll() message as described in Slider notification messages[^]. When the user moves the slider and releases the mouse button, a TB_THUMBPOSITION notification is send (nSBCode parameter of the scroll handler). The position is passed in the nPos parameter and a pointer to the slider control in the pScrollBar parameter (this must be casted to CSliderCtrl*).

[UPDATE: Example code]
If not done so far, add the WM_HSCROLL handler to your dialog and optional a member variable for the slider:
C++
void CMyDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    if (IDC_SLIDER == pScrollBar->GetDlgCtrlID())
    {
        // When there is no member variable and we need to access the control
//      CSliderCtrl* pSlider = reinterpret_cast<CSliderCtrl*>(pScrollBar);
        // Handle event here
        switch (nSBCode)
        {
            case TB_LINEUP:
            case TB_LINEDOWN:
            case TB_PAGEUP:
            case TB_PAGEDOWN:
            case TB_THUMBPOSITION:
            case TB_TOP:
            case TB_BOTTOM:
            case TB_THUMBTRACK:
            case TB_ENDTRACK:
            default:
                break;
        }
        return;
    }
    // Default handling if not a slider control.
    CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
 
Share this answer
 
v2
Comments
Fresher16 20-May-13 0:19am    
Can you please explain me in detail?
I tried with the OnHScroll() but nothing happened....
Jochen Arndt 20-May-13 4:10am    
See my updated solution.
louisejackie 20-Jul-13 0:52am    
Hi Jochen , I have problem in dealing with CSliderCtrl in my project. Supposing CSliderCtrl* pSlider points to a slider,use pSlider->SetRange(0,120) to set the range;now I wanna set the slider position in a way that its position will be 20's multiple when I click on the left/right of the slider. e.g., the current position is 25,when I click on the right, its position changes to 40, or 20 when I click on the left. I've been pondering over it for days and still cannot come up with a solution.I'll be very grateful if you kindly give me a hand.thanks in advance.
Fresher16 21-May-13 0:31am    
Thanks Jochen... My project is working now...!!!!

Can you tell me how to show the value of the slider at run time? I mean the value should change as the slider position is changed. I want to display the value above the slider control at run time?
Jochen Arndt 21-May-13 2:58am    
You can add a static control to your dialog and update the text from within the scroll handler.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900