Win32 Buttons and SLE's

Okay, call me the lazy sort, but this chapter just adds a few lines to the previous program. This will show you how to add a simple button to your window, and make it do something. It will also add a tiny SLE ( single line entry ) window, into which a user can type some text. So, what might you do with this text, once you've typed it in ? Here's one tiny example. For the source code below, all the new code lines are shown in red, so you can see whats been added.

-- lists2.exw

-- from Ex06.exw by David Cuny
-- This opens a window with a populated list in it.
-- I previously added onChange[] to show it's use,
-- so lets add a button and an SLE to "fill" the listbox.

include win32lib.ew

constant
    Win   = create( Window, "List ++", 0, Default, Default, 160, 190, 0 ),
--  yes, the Window has to be made larger.                   ^^   ^^
    List1 = create( List, "", Win, 10, 10, 120, 58, LBS_NOINTEGRALHEIGHT ),    
--  ^^ create a listbox window
    Label1 = create( RText, "", Win, 10, 75, 120, 20, 0 ),
--  ^^ create a right-justified display window
    Sle1    = create( EditText, "", Win, 10, 96, 110, 20, 0 ),
--  ^^ create a little single-line-entry window.
    Button1 = create( PushButton, "ADD", Win, 10, 126, 60, 20, 0 )
--  ^^ and last, add a button to do something.

procedure onLoad_Win( integer self, integer event, sequence params )
  -- add these items to the listbox    
  addItem( List1, "one" )
  addItem( List1, "two" )
  addItem( List1, "three" )
  addItem( List1, "four" )
  setIndex(List1,1)  -- point to the first item in the list.
  setFocus(Sle1)
-- ^^ setFocus() makes this cursor blink ! (moving the focus to the SLE)
end procedure

procedure show_it(integer here)
-- show the selected list item in the text window.
  sequence this
  this = getItem(List1,here)
  setText(Label1, this)
end procedure

procedure onChange_List1( integer self, integer event, sequence params )
-- get the latest index from the listbox, then show it.
  atom wwhere
  wwhere = getIndex(List1)
  show_it(where)
end procedure

procedure onClick_Button1( integer self, integer event, sequence params )
-- what to do when the button is "pressed".
  sequence list_entry
  list_entry = getText( Sle1 ) -- just what it says, getText() from Sle1.
  addItem( List1, list_entry ) -- here's where we add it to the list.
  setText(Sle1, {} )           -- and delete what was in the SLE.
  setFocus(Sle1)               -- return to the now empty SLE
end procedure

setHandler( Win, w32HOpen, routine_id( "onLoad_Win" ) )
setHandler( List1, w32HChange, routine_id("onChange_List") )
-- ^^ this event handler is triggered whenever List1 changes state.
setHandler( Button1, w32HClick, routine_id("onClick_Button1") )
-- ^^ this new event handler is activated when you click the button.

WinMain( Win,Normal )

-- end of code --

There are lots of little things that can be done with this example.

(1) First try changing one line so that the list will NOT accept an empty entry.
. currently, the program will crash if you click the button with nothing in the 'EditText' entry box.
. a little clue ! Use an if..then.. end if to do this.
(2) Next, you can figure out a way to point to the latest entry to show it both in the listbox and in the text box below the list. As it is, you never see it unless you scroll to the bottom of the list, ...yes, thats where it went ;-)
(3) It might also be useful to add a setHandler( …, w32HKeyDown, routine_id( “..._onKeyDown” ) ) routine, so that you can just hit the enter key to accept your entry, instead of using the button.

In any case, have fun trying !

..end of lesson.

CONTENTS