Uncheck radio button?

Malky

Active Member
Licensed User
Longtime User
Hi, at the moment I only have two Radiobuttons on a single panel.

After selecting one, I save the index value to my database.

If I restart and select an option to return to defaults, this means I have to check the first one and uncheck the second one. The result is they both end up checked which I thought shouldn't happen?

B4X:
Sub CreateSettingsPanel
   Price = SQL1.ExecQuerySingleResult("SELECT value FROM system where var = 'price'")
   prop_type = SQL1.ExecQuerySingleResult("SELECT value FROM system where var = 'prop_type'")
   Dim HeightTop
   Activity.AddView(pnlSettings, 0, 0, 100%x, 100%y)
   Dim sqlRowCount As Int
   Dim Cursor1 As Cursor
   Cursor1 = SQL1.ExecQuery("SELECT id, " & language & ", prop_type FROM settings order by prop_type")
   Log("count = " & Cursor1.RowCount)
   sqlRowCount= Cursor1.RowCount
   Dim btndefaultsettings, btnSaveSettings,btnCancelSettings As Button
   Dim radButtons(sqlRowCount) As RadioButton
   btndefaultsettings.Initialize("btnDefaultSettings")
   btnSaveSettings.Initialize("btnSaveSettings")
   btnCancelSettings.Initialize("btnCancelSettings")
   edtPrice.Initialize("edtPrice")
   edtPrice.Hint = DictionList(5)
   edtPrice.InputType =edtPrice.INPUT_TYPE_NUMBERS
   edtPrice.ForceDoneButton = True
   edtPrice.HintColor = Colors.gray
   HeightTop = 50dip
   pnlSettings.AddView(btndefaultsettings, 0dip, 0dip, 100%x, HeightTop) 'Height 50
   Log("Settings count = " & sqlRowCount)
   For i = 0 To sqlRowCount - 1
   Cursor1.Position = i
   
      Dim Rad As RadioButton
      
      Rad.Initialize("RadButton")
      Rad.Text = Cursor1.getstring(language)
      Rad.Tag = Cursor1.GetString("prop_type")
      If i = prop_type Then
         Log("Rad true = " & i)
         Rad.Checked = True
         'DoEvents
      Else
         Log("Rad false = " & i)
         Rad.Checked = False
      End If
      
      radButtons(i) = Rad
      pnlSettings.AddView(Rad,5dip, HeightTop + 5, pnlSettings.Width, 50dip)
      HeightTop = HeightTop + 55dip
      
   Next
   If Price = 1000000 OR Price =0 Then
      edtPrice.Text = ""
   Else
      edtPrice.Text = Price
   End If
   pnlSettings.AddView(edtPrice, 0dip, HeightTop + 5dip, 48%x, 50dip) 'Height 50
   HeightTop = HeightTop + 55dip
   pnlSettings.AddView(btnSaveSettings, 0dip, HeightTop + 5dip, 48%x, 50dip) 'Height 50
   pnlSettings.AddView(btnCancelSettings, 49%x, HeightTop + 5dip, 48%x, 50dip) 'Height 50
   btnCancelSettings.Text = DictionList(20)
   btnSaveSettings.Text = DictionList(19)
   btndefaultsettings.Text = DictionList(4)
   'pnlSettings.Visible = True
End Sub

Any ideas?

Malky
 

Malky

Active Member
Licensed User
Longtime User
Hi Erel, yes you are right, although the number of radiobuttons can change on update, so the whole panel is recreated just in case. There could be 5 or 6 instead of just 2.

i.e. if another product is available on my web server, a new radiobutton would automatically be created.

I'll move the initialization etc from the panel creation subroutine up to one of the Globals sections and try to manipulate the existing buttons without recreating it when it's not needed. (It shouldn't happen too often anyway).

Cheers,

Malky
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
I'm still struggling with this.

If I create the actual panel view and initialize the buttons, I still have to create the radiobuttons which I can't do until the app has been started and the language selected.

Then I need to create the list of possible radio buttons in the phone database which comes from a call to my own server before I can even get a count of the radio buttons needed. This means I can't do this in globals or process globals, so how do I change a single radio button to checked after a 'Reset Defaults' button is set in the defaultsbutton_click event when I can't see what radio button it was as it's not global?

Or am I so confused, I should pack up for the night?

Cheers,

Malky
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
How, where and how often do you call the CreateSettingsPanel routine.
I don't really undersatd what exactly you want to do and what you expect?
If you call CreateSettingsPanel several times you initialize each time a new panel with new Radiobuttons. What about the previous ones.
But without seeing more code it's difficult to give concrete answers because we mest guess what you coud have done and where.
Couldn't you post a small project that shows the problem ?

Best regards.
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
Hi Klaus, you've made my night! I've confused someone else :)

It's a big app and I will attach it if needed, but basically after I create the SettingsViewpanel, I add a few buttons.

I don't know how many radio buttons I need until I have started the app and selected a language.

After this I have to load the possible active radio buttons into my phone db and then I can start my loop to add the radio buttons, be it 2 or 5?

I have a top button that when clicked, resets all the (existing) radio buttons check value.

Regardless of what radio button is checked, I want to set it to the default one, normally the first option.

When I do this, if radio button 2 was originally selected and I change (in the buttonclick event) radio button 1 to true, then I get both checked?

Having a bad time here, so I hope that is a 'little' clearer?

Cheers,

Malky
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I still don't understand exactly what you expect because you didn't answer my questions.
You cannot have two RadioButtons belonging to the same Panel checked at the same time, that's the purpose of RadioButtons !
If you click on one RadioButton the previous checked one is unchecked by the system !
But you can have all unchecked.

Attached a small test program showing what I understood and how I would do it.
There are two Panels:
- a main panel for the screen
- and second Panel for the RadioButtons.
With this configuration you can easily remove the current RadioButtons and add new ones.
You can also easily get each RadioButton in the Panel with it's index.

Normal you should have provided a small program like this and we could modify it.

Best regards.
 

Attachments

  • RadioButtons.zip
    7.3 KB · Views: 420
Upvote 0

Malky

Active Member
Licensed User
Longtime User
Hi Klaus, thanks for that.

I will get a better look tonight after work, but the btnReset_Click sub looks interesting using the pnl.GetView option?

Cheers,

Malky
 
Upvote 0

gregwa

Member
Licensed User
Longtime User
Malky,

As Klaus mentioned, radiobuttons are a '1 of N' solution and checkboxes are a 'X of N' solution. I might not have understood your question correctly, but you might want to look at checkboxes instead.
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
Cheers gregwa.

I initially intended to use checkboxes, but discovered I couldn't group them as I only want one selected at any given time, (select one, the rest are unselected).

I swapped to using radiobuttons for this reason.

I will have more time this evening, (wife permitting) to have a better look at the whole scenario.

Thanks,

Malky
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
Ok, update. It's no as simple Klaus as that as I have to calculate the height for the number of radiobuttons I may have. There are two buttons which appear below this and I need to know the height for them

I have it working, but.... I have an update option which will pull any new data from my server and also any new radiobutton options.

This works, but I use the pnlRads.RemoveView for the existing panel holding the radiobuttons and then recreate it with all possible radiobuttons (to include any new ones). This appears to work, but the panel is invisible after updating although the height seems ok?

The radionbuttons panel (pnlRads) is added to a settings panel.

B4X:
Sub CreateSettingsRadios
   'HeightTop = 50dip
   Price = SQL1.ExecQuerySingleResult("SELECT value FROM system where var = 'price'")
   prop_type = SQL1.ExecQuerySingleResult("SELECT value FROM system where var = 'prop_type'")
   
   Dim sqlRowCount As Int
   Dim Cursor1 As Cursor
   Cursor1 = SQL1.ExecQuery("SELECT id, " & language & ", prop_type FROM settings order by prop_type")
   Log("count = " & Cursor1.RowCount)
   sqlRowCount= Cursor1.RowCount
   
   Dim pRadsHeight As Int
   pRadsHeight = 0dip
   'pnlRads.RemoveView
   pnlSettings.AddView(pnlRads, 0dip, 55dip, 100%x,sqlRowCount * 50dip ) 'Height 50
   Log("Prop_type before buttons = " & prop_type)
   For i = 0 To sqlRowCount - 1
   Cursor1.Position = i
   
      Dim Rad As RadioButton
      
      Rad.Initialize("RadButton")
      Rad.Text = Cursor1.getstring(language)
      Rad.Tag = Cursor1.GetString("prop_type")
      If i = prop_type Then
         Log("Rad true = " & i)
         Rad.Checked = True
         
      Else
         Log("Rad false = " & i)
         Rad.Checked = False
      End If
      
      'radButtons(i) = Rad
      pnlRads.AddView(Rad,0dip, pRadsHeight, pnlSettings.Width, 50dip)
      pRadsHeight = pRadsHeight + 50dip
      SettingsHeightTop = SettingsHeightTop + 55dip
      Log("adding Radio button " & i)
   Next
   DoEvents
   If Price = 1000000 OR Price = 0 Then
      edtPrice.Text = ""
   Else
      edtPrice.Text = Price
   End If
   pnlRads.Visible = True
End Sub

If it is recreated why can't I see it? The height is ok and the rest of the settings panel is fine?

Cheers,

Malky
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
Sorry but:
- You almost never answer condcrete questions.
- You almost never post any code or you post just small snippets insufficient to have any chance to help you.
So, sorry, but if you don't post your project as a zip file that we can test, and with a precise explanation on what you want to do I stop helping here.

Best regards.
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
Hi Klaus, sorry but I have felt the project would be too big?

I have no problem in attaching it, but as you will see it is difficult to create a 'small' example.

I create the settings panel, but I can't create the rest of the panel until the language and the database tables have been created.

I have it working ok, but if a user wants to update the data, then I may have fewer or more radiobuttons to display.

As I said it works, but when I try the remove.view and recreate it in real time, I get nothing visible with the pnlRads panel although the bottom buttons are at the correct height.

Malk
 

Attachments

  • homer.zip
    68.1 KB · Views: 255
Upvote 0

klaus

Expert
Licensed User
Longtime User
Unfortunately your project doesn't run correctly.
First run, I get a selection for the lanuage, I select 'English' get an error 'file not found '!
Second run black screen.

I looked at your code especially CreateSettingsRadios routine.
- you don't remove the previous views from pnlRads
- you never set pnlRads.Height according to the number of RadioButtons
- you should adjust the views below pnlRads according to it's height.

Best regards.
 
Upvote 0

Malky

Active Member
Licensed User
Longtime User
Sh*t, sorry Klaus I was trying to make the file smaller.

I do actually have this code before calling the sub
B4X:
Sub Update 
Log("Update")
'diction.Initialize
   scvMenu.Visible = False
   CreateDictionTable
   CreateSettingsTable
   CreateMenuTable
   CreateAreasTable
   CreateVillagesTable
   CreatePropertiesTable
   CreateLangTable
   SQL1.TransactionSuccessful
   FetchData(DICTION_LIST)
   'PopulateDiction(language)
   
   FetchData(MENU_LIST)
   
   FetchData(AREA_LIST)
   
   FetchData(VILLAGE_LIST)
   
   FetchData(PROPERTY_LIST)
   
   FetchData(LANG_LIST)
   Log("from update 1")
   UpdateMode = 0
   Started = 1
   'SQL1.ExecNonQuery("UPDATE system SET value = 0 WHERE var='UpdateMode'")
   SQL1.ExecNonQuery("UPDATE system SET value = 1 WHERE var='appActivated'")
   ProgressDialogHide
   FillMenuScrollView
   SQL1.ExecNonQuery("UPDATE system SET value = 0 WHERE var='LanguageSelected'")
   SQL1.ExecNonQuery("UPDATE system SET value = 0 WHERE var='UpdateMode'")
   Log("REMOVING RADS!!!!!!!!!!!!!")
   pnlRads.RemoveView
   CreateSettingsRadios
   scvMenu.Visible = True
End Sub

The file not found error is because I haven't added the files as such to the project. (I keep thinking if it works here then it works anywhere, but that is obviously after compiling).

Malky
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Still doesn't work.
File missing menu_sms_sm.png !

But try to modify the project on your own with the suggestions I gave in the previous post and those below.
You have a panel with:
- views on top
- a panel with a variable height depending on the number of RadioButtons.
- views below the previous one, these could also be on a panel.

So when you need to update the RadioButton panel you must:
- remove all views from the RadioButton panel
- add the new RadioButtons
- adjust the new RadioButton panel height
- move the views below according to the new RadioButton panel height, if these are on a panel just set the Panel.Top property adjusted to the new RadioButton panel height.

It's really not difficult.

Best regards.
 
Last edited:
Upvote 0

Malky

Active Member
Licensed User
Longtime User
Hi Klaus 3rd time lucky as they say? File attached again.

As I said the code to remove the view comes in the Update sub.

The bottom buttons appear to be where they should be if there's two radiobuttons but till check this.

Many thanks for your patience,

Malky
 

Attachments

  • homer3.zip
    85.7 KB · Views: 275
Upvote 0

klaus

Expert
Licensed User
Longtime User
OK, this time the program runs the first time.
How can we reproduce the problem?
On a second run the screen is black.

I've seen in your code that you remove pnlRads !?
You should add all views once onto pnlSettings.
And when you update it :
- only remove the child views from pnlRads.RemoveAllViews.
- add the RadioButtons
- adjust pnlRads.Height
- adjust the positions of the views below pnlRads according to the new height.

I have also seen that you initilze some panels in as strange way.
pnlRads.Initialize(500) ??
should be either
pnlRads.Initialize("") no event routines
pnlRads.Initialize("pnlRads") with event routines

Best regards.
 
Last edited:
Upvote 0

Malky

Active Member
Licensed User
Longtime User
Hi Klaus, thanks very much for that.

The app runs ok second time here? As long as it was exited using the menu option.

Basically the only error I get is when I click on Update from the menu.

Then I go into settings and you can see the problem.

Once again, many thanks and I'll try this in the afternoon as my time has been allotted to garden work this morning apparently!

Cheers,

Malky
 
Upvote 0
Top