Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4ppc (Windows Mobile) > Code Samples & Tips > Tutorials
Documentation Wiki Register Members List B4P 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, 02:18 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 13,162
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
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:
<font color="#0000ff">Sub </font>App_Start
 Form1.Show
<font color=
"#0000ff"For</font> i = <font color="#800080">1 </font><font color="#0000ff">To </font><font color="#800080">10</font>
<font color=
"#0000ff">  AddButton</font>(<font color="#800000">"Form1"</font>,<font color="#800000">"Btn"</font> & i, <font color="#800080">100</font>,<font color="#800080">25</font> * i,<font color="#800080">50</font>,<font color="#800080">20</font>,i)
<font color=
"#0000ff">  AddEvent</font>(<font color="#800000">"Btn"</font> & i,Click, <font color="#800000">"Btn_Click"</font>)
<font color=
"#0000ff"Next</font>
<font color=
"#0000ff">End Sub</font>
 
<font color=
"#0000ff">Sub </font>Btn_Click
<font color=
"#0000ff"Sender</font>.Color = cRed
<font color=
"#0000ff">End Sub</font>
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:
<font color="#0000ff">Sub </font>App_Start
 Form1.Show
<font color=
"#0000ff"For</font> i = <font color="#800080">1</font><font color="#0000ff">To</font><font color="#800080">10</font>
<font color=
"#0000ff">  AddButton</font>(<font color="#800000">"Form1"</font>,<font color="#800000">"Btn"</font> & i, <font color="#800080">100</font>,25 * i,<font color="#800080">50</font>,<font color="#800080">20</font>,i)
<font color=
"#0000ff">  AddEvent</font>(<font color="#800000">"Btn"</font> & i,Click, <font color="#800000">"Btn_Click"</font>)
<font color=
"#0000ff"Next</font>
 Btn1.Color = cBlue
 Btn2.Color = cBlue
 ...
<font color=
"#0000ff">End Sub</font>
Instead we could use the Control keyword:
Code:
<font color="#0000ff">Sub </font>App_Start
 Form1.Show
<font color=
"#0000ff"For</font> i = <font color="#800080">1</font><font color="#0000ff">To</font><font color="#800080">10</font>
<font color=
"#0000ff">  AddButton</font>(<font color="#800000">"Form1"</font>,<font color="#800000">"Btn"</font> & i, <font color="#800080">100</font>,<font color="#800080">25</font> * i,<font color="#800080">50</font>,<font color="#800080">20</font>,i)
<font color=
"#0000ff">  AddEvent</font>(<font color="#800000">"Btn"</font> & i,Click, <font color="#800000">"Btn_Click"</font>)
<font color=
"#0000ff">  Control</font>(<font color="#800000">"Btn"</font> & i).Color = cBlue
<font color=
"#0000ff"Next</font>
<font color=
"#0000ff">End Sub</font>
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:

Code:
<font color="#0000ff">Sub </font>Globals
<font color=
"#0000ff"Dim</font> controls(<font color="#800080">0</font>) <font color="#008000">'Creates an empty array.</font>
<font color="#0000ff">End Sub</font>
 
<font color=
"#0000ff">Sub </font>App_Start
 Form1.Show
<font color=
"#0000ff"For</font> i = <font color="#800080">1</font><font color="#0000ff">To</font><font color="#800080">11</font>
<font color=
"#0000ff">  AddEvent</font>(<font color="#800000">"Button"</font> & i,Click,<font color="#800000">"RegularButton_Click"</font>)
<font color=
"#0000ff"Next</font>
<font color=
"#0000ff">End Sub</font>
 
<font color=
"#0000ff">Sub </font>RegularButton_Click
 TextBox1.Text = TextBox1.Text & <font color=
"#0000ff">Sender</font>.Text <font color="#008000">'Sender.Text is equivalent to Control(Sender).Text</font>
<font color="#0000ff">End Sub</font>
 
<font color=
"#0000ff">Sub </font>chkEnabled_Click
 controls() = <font color=
"#0000ff">GetControls</font>(<font color="#800000">"Form1"</font>)
<font color=
"#0000ff"For</font> i = <font color="#800080">0 </font><font color="#0000ff">To </font><font color="#0000ff">ArrayLen</font>(controls())-<font color="#800080">1</font>
<font color=
"#0000ff">  If </font><font color="#0000ff">ControlType</font>(controls(i)) = <font color="#800000">"Button" </font><font color="#0000ff">Then</font>
<font color=
"#0000ff">   Control</font>(controls(i)).Enabled = chkEnabled.Checked
<font color=
"#0000ff">  End </font><font color="#0000ff">If</font>
<font color=
"#0000ff"Next</font>
<font color=
"#0000ff">End Sub</font>
 
<font color=
"#0000ff">Sub </font>btnClear_Click
 TextBox1.Text = <font color=
"#800000">""</font>
<font color=
"#0000ff">End Sub</font>
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, 360 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 Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting color of controls on runtime RGS Questions (Windows Mobile) 5 09-08-2008 06:31 PM
Removing controls in runtime Stanl3yCZ Questions (Windows Mobile) 1 02-18-2008 03:51 PM
Getting value from other other application in runtime Rioven Questions (Windows Mobile) 19 09-20-2007 10:13 AM
Recordset or equivalent Implementation for database manipulation markbarrett_1 Basic4ppc Wishlist 4 08-30-2007 08:23 AM
Runtime control manipulation Erel Code Samples & Tips 6 05-11-2007 02:19 PM


All times are GMT. The time now is 01:04 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0