[Gambas-user] Trapping events from dynamically created controls....

timothy timothy.marshal-nichols at ...247...
Tue Feb 20 10:20:42 CET 2007


> -----Original Message-----
> From: gambas-user-bounces at lists.sourceforge.net [mailto:gambas-user-
> bounces at lists.sourceforge.net] On Behalf Of sbungay
> Sent: Tuesday, 20 February 2007 04:01 AM
> To: gambas-user at lists.sourceforge.net
> Subject: [Gambas-user] Trapping events from dynamically created
> controls....
> 
>    How can one capture events from controls that are created on the
fly?
> In my case I am creating buttons and want to capture the mousemove
event
> or click event of the control that was just created.
> I think if I was able to add each programatically created control to a
> special group then that group could capture and process the events. Is
> this possible?
> 
> Steve.
> 


Here is an example that creates a number of buttons and places them on a
form. We create a new button, the ME parameter to NEW tells the button
which container it should live in. You could use any other container
e.g. a ScrollView. The AS "KeyButton" gives us the name to use when
handling the buttons events. We also set some other properties of the
button.

We have set the Form.Arrangement property to make the buttons look a bit
nicer for this example.

The button click event is handled by the KeyButton_Click procedure -
this must match the AS "KeyButton" statement. We get the button object
using the LAST keyword. Here we just display the Name and Tag using a
Message box.

***************

PUBLIC SUB Form_Open()
  DIM b AS Button
  DIM i AS Integer
  ' Fill the form with buttons for
  ' ASCII codes 33 to 126
  FOR i = 33 TO 126
    b = NEW Button(ME) AS "KeyButton"
    b.Font.Size = 32
    b.Width = 56
    b.Height = 56
    IF Chr(i) LIKE "&" THEN
      b.Text = "&&"
    ELSE
      b.Text = Chr(i)
    END IF
    b.Tag = i
    b.ToolTip = "ASCII: 0x" & Hex(i, 2)
  NEXT
  ' Set the form arrangement so the buttons
  ' fill the form area right to left. Note thet
  ' when you resize the form the button fill the
  ' available space
  ME.Arrangement = Arrange.LeftRight
END

' Handle button click event
PUBLIC SUB KeyButton_Click()
  Message.Info("Button Tag: " & LAST.Tag & "\nButton Text: " &
LAST.Text)
END

******************

From: http://www.linuxbasic.net/index.php?topic=117.0

Thanks

8-{)} Timothy Marshal-Nichols
<mailto: timothy.marshal-nichols at ...247...>

 








More information about the User mailing list