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
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Code Samples & Tips Share your recent discoveries and ideas with other users.

Do It Bitwisely: Manage Several Controls With Just A Single Number

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-04-2012, 09:49 PM
Junior Member
 
Join Date: Mar 2010
Posts: 21
Default Do It Bitwisely: Manage Several Controls With Just A Single Number

Bits are not for rocket scientists only. They come in handy in pretty common programming tasks, for instance, when dealing with a complex system of controls. Here's a snippet demonstrating how to get and set checked states for 4 check-boxes with a single number (0 to 15, naturally) in Basic4PPC. Perhaps, this is too simple for the present company, however, I believe the more examples the better.

Code:
Sub Globals 'First add a Bitwise object named bit.
Box=0 'Variable for a decimal value
End Sub
Sub App_Start 'ADDING CONTROLS TO THE FORM
AddNumUpDown("frmMain","numBitCount",80,70,40'Adds a decimal counter with Value_Changed event
AddEvent("numBitCount",ValueChanged,"numBitCount_ValueChanged")
numBitCount.Maximum = 
15
AddLabel (
"frmMain","lblDisplay",130,72,40,16,"0000"' Adds a binary display
lblDisplay.Color = Rgb(220,220,220)
For i = 0 To 3 'Adds 4 checkboxes with Click events
    AddCheckBox ("frmMain","chkTrigger"&i,30+(i*50),160,40,30,""&i)
    AddEvent(
"chkTrigger"&i,Click,"Trigger_Click")
    
Button("chkTrigger"&i).Color = Rgb(220,220,220)
Next i
bit.New1 'Constructs a bitwise object
    frmMain.Show
End Sub
Sub numBitCount_ValueChanged ' Sets checked values of the checkboxes
Box = numBitCount.Value
    
For i = 0 To 3
        
Button("chkTrigger"&i).Checked = bit.GetBit(Box,i)
    
Next
lblDisplay.Text=Format(
bit.DecToBin(Box),"D4"'Binary representation of a decimal value
End Sub
Sub Trigger_Click 'Sets a corresponding bit to 1/0 if a checkbox is checked/unchecked
If Sender.Checked = True Then Box = bit.SetBit(Box,Sender.Text) Else Box = bit.ClearBit(Box,Sender.Text) 
numBitCount.Value = Box
End Sub

Last edited by Sir Isaac : 02-05-2012 at 02:35 AM.
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
How to manage code & GUI? dkjorgi Basic4android Updates and Questions 6 04-19-2012 11:21 AM
how to manage usb port albgreco Questions (Windows Mobile) 3 08-28-2009 06:02 PM
Manage many sprites serge Questions (Windows Mobile) 3 07-31-2008 09:29 AM
Pick up the single column single row value to a textbox. mozaharul Questions (Windows Mobile) 4 05-07-2008 05:24 AM
is a single character string a number as well? Stellaferox Questions (Windows Mobile) 16 06-08-2007 11:01 AM


All times are GMT. The time now is 09:36 PM.


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