Grouping radio buttons

Caravelle

Active Member
Licensed User
Longtime User
The offline and online help both say that radio buttons can be grouped in a panel and that only one of the group can be clicked at a time. I've tried it, it works, but...

How exactly does the group return the index of the one that is clicked?
I can't find any relevant properties or methods. Do you have to have a loop that reads the checked property of each radio button, called by a separate _click sub for each individual button?

Sorry to ask so many questions :sign0104:

Caravelle
 

Ariel_Z

Active Member
Licensed User
There is no problem at all! This is the purpose of this forum...:sign0089:
Basically, you need a different sub for each button. In Basic4ppc, if you have a control named Cont, and it has an event named Click, the sub "Cont_Click" will be called when the event occurs.

Try this (I have added some extra functionality but it explains the idea you need as well...):

B4X:
Sub Globals
   'Declare the global variables here.
   jump = -10
End Sub

Sub App_Start
   AddForm("Form1", "Form1")
   AddPanel("Form1", "Panel1", 0, Form1.Height, Form1.Width, 100)
   AddTimer("Timer1")
   panel1.color = cRed
   AddButton("Form1", "B2", 0, 0, 50, 50)
   Form1.text = panel1.top
   timer1.interval = 10
   AddButton("Panel1", "B1", 90, 20, 50, 50)
   form1.show
   AddForm("frmMn", "frmMn")
   AddPanel("frmMn", "P1", 10, 10, 100, 100)
   AddRadioBtn("P1", "RB1", 10, 10, 100, 20, "RB1")
   AddRadioBtn("P1", "RB2", 10, 30, 100, 20, "RB2")
End Sub

Sub Timer1_Tick
   panel1.top = panel1.top + jump
   Form1.text = panel1.top

   If panel1.top + panel1.height <= Form1.height Then
      panel1.top = Form1.height - panel1.height
      timer1.enabled = False
   End If
End Sub

Sub b2_Click
   timer1.enabled = True
End Sub
Sub B1_Click
   frmMn.Show
End Sub

Sub rb1_Click
   Msgbox(1)
End Sub

Sub rb2_Click
   Msgbox(2)
End Sub


Best regards,
Ariel
 

Caravelle

Active Member
Licensed User
Longtime User
Thank you. I was looking for something like the "RadioGroup" in other languages

Can I suggest an example in the Help item for Radio Buttons? Something simpler than the one you have given which may seem very straightforward to you but uses the Timer component which the new user may well not have investigated yet (I certainly haven't). It's not true that you can't teach an old dog new tricks, but it's much better to only teach the old dog one new trick at a time :)

I'll work on something that gets the index of the checked button into a simple global variable that can be "read" from anywhere else in the application and post it in this thread.

Caravelle
 

Ariel_Z

Active Member
Licensed User
Thank you, this will be very helpful. I agree the example was too complicated - I saw your post minutes before I had to go but I know you are busy these days trying to achive some progress and so I just changed an existing code (an example I've written for someone else) and sent it to you knowing you could understand it. Now that I have a bit more time I can say that the Timer "ticks" (raises the Tick event) once every [Timer.Interval] milliseconds and hence the poping up panel. It is actually not part of the answer you've asked for - excuse me for this. The radio buttons are for you so you can as well use only the relevant form. I agree an example should be added to the help about radio buttons - the way you use them varies from one language to another. Moreover, it is strongly recommended to add the radio buttons with the Visual Designer - unlike my example - so that you have the AutoComplete feature when typing. I chose this way for code completeness when reading (nothing is outside code).
 
Last edited:

Caravelle

Active Member
Licensed User
Longtime User
No need to keep apologising! I have worked out a simple example which shows the use of RadioButton.Click, RadioButton.Name, and RadioButton.Checked, both getting and setting the value.

I find it very useful that the button's Text attribute both acts as a label and can be used to return a string value.

I am used to a group of radio buttons automatically first appearing with the first one checked, so it may be helpful to tell users that they need to preset one of their buttons before the Form is shown if they want a "default value".

B4X:
Sub Globals
  radiogroupvalue = ""
End Sub

Sub App_Start
  Form1.Show
  ' The form has a panel, containing three radio buttons. Give each
  ' radio button a Name property which will be displayed
  ' next to the button.  When tapped, the name of the selected
  ' radio button will be allocated to a Global variable "radiogroupvalue"
  ' which can be used for many purposes eg program control in a 
  ' Select Case statement, or as data in a data entry system.

' preset button 1 on start
  RadioButton1.Checked = True
' preset the Global variable to match
  radiogroupvalue = RadioButton1.Text  
End Sub

Sub RadioButton1_Click
  RadioGroupValue = RadioButton1.Text
  RadioGroupClick(RadioGroupValue)
End Sub

Sub RadioButton2_Click
  RadioGroupValue = RadioButton2.Text
  RadioGroupClick(RadioGroupValue)
End Sub

Sub RadioButton3_Click
  RadioGroupValue = RadioButton3.Text 
  RadioGroupClick(RadioGroupValue)
End Sub

Sub RadioGroupClick(v)
  Msgbox("Value selected is " & v, "Radio Button group example", cMsgboxOK, cMsgboxHand)
End Sub

I have a version of this running with 6 Radio Buttons, text attributes "1 GB", "2 GB", "4 GB", "8 GB", "16 GB" and "32 GB". The next task is to make the program write the selected value to a tiny .dat file on any USB stick, SD or CF card which does not already have such a file (I don't have anything less than 1 GB). The purpose of all this is to make it possible to calculate the remaining space available after adding up the length of the files on it. Small beginnings :)

Caravelle
 

agraham

Expert
Licensed User
Longtime User
Once you get a bit more used to Basic4ppc you can shorten your code with techniques like this.
B4X:
Sub Globals
  radiogroupvalue = ""
End Sub

Sub App_Start
  For i = 1 To 3
    AddEvent("RadioButton" & i, Click, "RadioGroupClick")
  Next
  RadioButton1.Checked = True
  radiogroupvalue = RadioButton1.Text  
  Form1.Show
End Sub

Sub RadioGroupClick
  RadioGroupValue = Sender.Text 
  Msgbox("Value selected is " & RadioGroupValue, "Radio Button group example", cMsgboxOK, cMsgboxHand)
End Sub
 

Caravelle

Active Member
Licensed User
Longtime User
True and thanks, but I hope you understood that the idea was to give an example of simple usage for beginners in the Help article. I was aiming at the simplest structure and the least use of other keywords. A help article where you have to look up two other help articles to understand the example isn't very encouraging.

I'm new to BASIC4PPC and the constraints of Win Mobile but I've been programming in Delphi for quite some time.

Caravelle
 

Caravelle

Active Member
Licensed User
Longtime User
the library I've just written to return the free space on a drive

Would that have been in response to the message I posted in this thread?

http://www.b4x.com/forum/questions-help-needed/2170-retrieve-free-space.html.

As no answer came I've been working on it myself.

Ah, so you won't want the library

I do appreciate you were only joking, but many readers (and not all have a good grasp of English and what we leave unsaid when we say things) won't get the point. Providing a simple example for the program help on the use of linked Radio Buttons and writing a practical process for retrieving the free space on a drive (including an attached USB drive) are two quite different things.

I'm sure lots of users including me will appreciate your library (and as soon as possible in my case). You can't conclude I won't want it because the code I suggested was better as a help example on working with Radio Buttons than your more elegant version. I've learned quite a lot along the way about reading and writing files to the USB drive and I've used your example in what I've got so far and I'm grateful. The Radio Button problem was just something that arose in working out a quick one-click way of manually telling the device what the size of the drive was. So yes, your library will be very welcome :icon_clap:; but I still don't think your version of my code is appropriate for the Help! :sign0089:

Looking forward to trying out your library.

Thanks

Caravelle
 

Caravelle

Active Member
Licensed User
Longtime User
Well, that will give me a chance to try out my new "add DLL" skills :)

Many thanks. Just what the doctor ordered at this point!

Caravelle
 
Top