![]() |
|
|||||||
| Home | Register | FAQ | Members List | Search | Today's Posts | Mark Forums Read |
| Tutorials Basic4ppc tutorials |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
||||
|
Basic4ppc allows you to add controls with the visual designer or at runtime using the AddControls keywords.
Usually it is easier to add the controls with the designer and this also enables the pop-up lists to show when you write the control's name followed by a period. However there are some occasions when adding controls at runtime is useful. For example: Code:
Sub App_Start
Form1.Show
For i = 1 To 10
AddButton("Form1","Btn" & i, 100,25 * i,50,20,i)
AddEvent("Btn" & i,Click, "Btn_Click")
Next
End Sub
Sub Btn_Click
Sender.Color = cRed
End Sub
AddEvent is very important when adding many controls. We could create Sub Btn1_Click, Sub Btn2_Click... and it would have worked. Instead we are using AddEvent which connects the Click event of the buttons to a specific sub (Btn_Click). Note that you can use AddEvent with controls that you create with the designer as well. Another thing you should notice is that the sub signature (number of parameters) must match the signature of the original event. If one sub handles events of many controls, we need a way to find which control raised the event. Sender keyword comes to the rescue. If we just use Sender then it returns the name of the control that raised the event. However Sender can do more than that. You can treat Sender as a control. In our example we wrote Sender.Color = cRed, that will change the color of each pressed button to red. Another important keyword is Control. Using Control keyword you can reference a control with a string. If we want to change the color of all buttons to blue when they were first created, we could have written: Code:
Sub App_Start
Form1.Show
For i = 1To10
AddButton("Form1","Btn" & i, 100,25 * i,50,20,i)
AddEvent("Btn" & i,Click, "Btn_Click")
Next
Btn1.Color = cBlue
Btn2.Color = cBlue
...
End Sub
Code:
Sub App_Start
Form1.Show
For i = 1To10
AddButton("Form1","Btn" & i, 100,25 * i,50,20,i)
AddEvent("Btn" & i,Click, "Btn_Click")
Control("Btn" & i).Color = cBlue
Next
End Sub
Deleting a control is done with each control's Dispose method. Two new related functions were added in version 5.50: GetControls and ControlType. GetControls returns an array holding the names of all the controls or all the controls that belong to a specific form. GetControls("") will return all controls, while GetControls("Form1") will return all the controls that belong to Form1. ControlType returns the type of a control. Combining GetControls and ControlType allows us to handle all the controls which are of the same type. Here is an example of a simple numpad: (See this thread for a fully functional numpad http://basic4ppc.geotrail.no/projects/Numpad.sbp ) Code:
Sub Globals
Dim controls(0) 'Creates an empty array.
End Sub
Sub App_Start
Form1.Show
For i = 1To11
AddEvent("Button" & i,Click,"RegularButton_Click")
Next
End Sub
Sub RegularButton_Click
TextBox1.Text = TextBox1.Text & Sender.Text 'Sender.Text is equivalent to Control(Sender).Text
End Sub
Sub chkEnabled_Click
controls() = GetControls("Form1")
For i = 0 To ArrayLen(controls())-1
If ControlType(controls(i)) = "Button" Then
Control(controls(i)).Enabled = chkEnabled.Checked
End If
Next
End Sub
Sub btnClear_Click
TextBox1.Text = ""
End Sub
![]() All the 'regular buttons' are handled in Sub RegularButtons_Click. This sub just adds the text of the sender button. We enable and disable all the buttons using GetControls and ControlType. The source code of this example is attached. Another runtime keyword which is not covered here is CallSub. CallSub calls a sub using a string as the sub name. |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Removing controls in runtime | Stanl3yCZ | Questions & Help Needed | 1 | 02-18-2008 02:51 PM |
| Getting value from other other application in runtime | Rioven | Questions & Help Needed | 19 | 09-20-2007 09:13 AM |
| addlabel in runtime | tvrman | Questions & Help Needed | 2 | 09-04-2007 02:58 PM |
| Recordset or equivalent Implementation for database manipulation | markbarrett_1 | Basic4ppc Wishlist | 4 | 08-30-2007 07:23 AM |
| Runtime control manipulation | Erel | Code Samples & Tips | 6 | 05-11-2007 01:19 PM |