Basic4ppc - Windows Mobile Development  

Go Back   Basic4ppc - Windows Mobile Development > Main Category > Code Samples & Tips > Tutorials
Home Register FAQ Members List Search Today's Posts Mark Forums Read

Tutorials Basic4ppc tutorials


Runtime controls manipulation


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-26-2007, 01:18 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 2,560
Default Runtime controls manipulation

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
In this code we are adding 10 buttons to the form.
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
Instead we could use the Control keyword:
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
Again you should be aware that Control, AddEvent and Sender keywords could be used with controls (and objects) that were added with the designer.
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
Using the visual designer we've created the following form:


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.
Attached Files
File Type: sbp RuntimeControls.sbp (1.8 KB, 148 views)
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
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
Tab Simulation @ Runtime anansath Questions & Help Needed 1 05-19-2007 02:55 PM
Runtime control manipulation Erel Code Samples & Tips 6 05-11-2007 01:19 PM


All times are GMT. The time now is 09:55 AM.


Powered by vBulletin® Version 3.6.10
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0