1. How to combine a slider and an edit control in win32lib
- Posted by penpal0andrew May 05, 2009
- 826 views
I have a program which has a slider (scroll control) and an edit control. And when I move the slider it adjusts the value shown in the read only edit control. However, I would like it so I can ALSO edit the edit control, and have the slider change. But when I attempted to do them both, it made some sort of infinite event loop. I assume that is what happened.
I am asking this as a general idea, rather than asking someone to fix some code that I have written.
Andy
2. Re: How to combine a slider and an edit control in win32lib
- Posted by DerekParnell (admin) May 05, 2009
- 798 views
I have a program which has a slider (scroll control) and an edit control. And when I move the slider it adjusts the value shown in the read only edit control. However, I would like it so I can ALSO edit the edit control, and have the slider change. But when I attempted to do them both, it made some sort of infinite event loop. I assume that is what happened.
I am asking this as a general idea, rather than asking someone to fix some code that I have written.
Andy
Assuming Win32lib ...
global procedure Keypress_EB( integer self, integer event, sequence parms ) object val sequence range if parms[1] = VK_RETURN and parms[2] = 0 then -- Get the number typed in to the EditBox (EB) val = getNumber(self) -- Get the current range allowed in the scrollbar range = getScrollRange(HSBar) -- Make sure the entered value is in the allowed range. if val >= range[1] and val <= range[2] then -- Set the new position of the 'thumb' setScrollPos(HSBar, val) else -- Change the edit box to the current thumb position. setText(self, getScrollPos(HSBar)) end if end if end procedure setHandler(EB, w32HKeyPress, routine_id("Keypress_EB"))
3. Re: How to combine a slider and an edit control in win32lib
- Posted by penpal0andrew May 06, 2009
- 803 views
- Last edited May 07, 2009
This code works as advertised. Thank you. However, it is such a pain to click on the edit box (which is rather small), and select the text, and type a new number, and then press Enter (RETURN). My scroll range is from 10 to 70 and it is not so hard to find the number I want. There may be users who prefer to type rather than use the mouse. However, the big issue with this code is that if one does not press Enter, an incorrect value can be left in the edit box! And I want the slider and edit to always be in agreement with one another.
If anyone wants to take a try at code which does not wait for the Enter key to be pressed, I would appreciate it. But if not, that is okay. I think most people are like me, and use Euphoria as a hobby.
Andy
4. Re: How to combine a slider and an edit control in win32lib
- Posted by DerekParnell (admin) May 06, 2009
- 770 views
- Last edited May 07, 2009
If anyone wants to take a try at code which does not wait for the Enter key to be pressed, I would appreciate it.
Assuming Win32lib ...
setUserProperty(EB, "flag", 0) global procedure KeyUp_EB( integer self, integer event, sequence parms ) object val sequence range -- Get the number typed in to the EditBox (EB) val = getNumber(self) -- Check if the user blanked out the whole field. if val = 0 then range = getScrollRange(HSBar) val = range[1] -- use the lowest allowed. end if -- Only change the slider if the editbox value is different from the current position. if val != getScrollPos(HSBar) then -- Get the current range allowed in the scrollbar range = getScrollRange(HSBar) -- Make sure the entered value is in the allowed range. if val >= range[1] and val <= range[2] then -- Set the new position of the 'thumb' setUserProperty(EB, "flag", 1) -- Indicate that the slider is being changed by me setScrollPos(HSBar, val) setUserProperty(EB, "flag", 0) -- Finshed changing the slider. end if end if end procedure setHandler(EB, w32HKeyUp, routine_id("KeyUp_EB"))
And in the paint handler you code something like ...
if equal(getUserProperty(EB, "flag"), {0}) then setText(EB, getScrollPos(HSBar)) end if
The main changes are I now trap the KeyUp event rather than the KeyPress and don't bother checking for VK_RETURN to set the slider. Now setting the slider triggers the paint event, which used to always change the text in the edit box. Now the paint event only changes the editbox text if the paint was not triggered by the editbox setting the slider value.
5. Re: How to combine a slider and an edit control in win32lib
- Posted by penpal0andrew May 07, 2009
- 837 views
One detail turned out to be important. And that is that the edit box is always going to have a value between 10 and 99. The actual range is 10 to 70. But this makes it easier to do the user interface. The idea of controls having private values is a good one, but not needed here, and also a global variable would have worked as well. The paint event is the wrong place to place code, since it caused the control to not get painted! The most complicated thing about windows programming is that actions done in processing one event can cause other events to happen. Is there a good source which discusses this?
Here is code which I am happy with now:
procedure SHIFTLEVER_onScroll (integer self, integer event, sequence params)--params is ( int pos ) -- must place value of shift lever into the shift lever number setText (SHIFTLEVER_E, getScrollPos( SHIFTLEVER )) end procedure setHandler( SHIFTLEVER, w32HScroll, routine_id("SHIFTLEVER_onScroll")) procedure SHIFTLEVER_E_onKeyDown (integer self, integer event, sequence params)--params is ( atom scanCode, atom shift ) -- clear the field if full - event is done before the key is added sequence textval -- Get the number typed in to the EditBox textval = getText(self) --This is needed since I am only part way to entering the field if length(textval) < 2 then return end if setText(self,"") end procedure setHandler( SHIFTLEVER_E, w32HKeyDown, routine_id("SHIFTLEVER_E_onKeyDown")) procedure SHIFTLEVER_E_onKeyUp (integer self, integer event, sequence params)--params is ( int scanCode, int shift ) object val sequence textval sequence range -- Get the number typed in to the EditBox val = getNumber(self) textval = getText(self) --This is needed since I am only part way to entering the field if length(textval) < 2 then return end if range = getScrollRange(SHIFTLEVER) if val < range[1] then val = range[1] -- use the lowest allowed. end if if val > range[2] then val = range[2] -- use the highest allowed. end if -- Set the new position of the 'thumb' - does not hurt to always do it setScrollPos(SHIFTLEVER, val) end procedure setHandler( SHIFTLEVER_E, w32HKeyUp, routine_id("SHIFTLEVER_E_onKeyUp")) procedure SHIFTLEVER_E_onLostFocus (integer self, integer event, sequence params)--params is () -- make sure editbox has valid number setText(self, getScrollPos(SHIFTLEVER)) end procedure setHandler( SHIFTLEVER_E, w32HLostFocus, routine_id("SHIFTLEVER_E_onLostFocus"))
The last one is needed for the case when the user only entered none or one digit and went away from the edit box.
Andy