Unwanted Click Event

Zenerdiode

Active Member
Licensed User
Please allow me to introduce myself. I stumbled across B4P as I've been trying to learn languages such as Visual C++ etc. I learn best by having a full syntax of commands and some good examples where I can substitute my own code.

My previous experience of coding is Psion OPL, HD6303X assembler (for PSION Org2) and various other 'Basic' type languages.

I have fully registered for B4P and I'd welcome some help for some code as attached. I'm asking a series of questions - six in this case - and want the user to select their answer with a radio button, then move on to the next question. Before submitting, I'd like them to be able to review their answers and change. You'll see it populates an array Answer(). Going forward works fine, going back works fine too, until the transition between Q2 and Q1. Q1 takes on the answer from Q2, but only if Q2 has been answered. By using breakpoints and adding some tracing code, I see for some reason when hitting 'Prev' when on Q2 wishing to go to Q1, the Radio_Click Sub is invoked, populating Q1 with Q2s answer. But only if Q2 has been answered.

I've stripped out all my other code to keep it clear.
 

Erel

Administrator
Staff member
Licensed User
I'm not sure if it is a bug in the .Net Framework or an intended behavior, but when a RadioBtn is focused it also gets selected (or clicked).
When you disable prevBtn the focus moves to the RadioBtn.
You should move the focus to nextBtn and it will be fixed:
B4X:
Sub ShowData
    If AnsRec=0 Then
        [B]NextBtn.Focus[/B]
        PrevBtn.Enabled=False
        NextBtn.Enabled=True
    Else If AnsRec=5 Then
 

klaus

Expert
Licensed User
Erel,
This behaviour looks strange.

I renamed PrevBtn to PrevBtn1
I added a new button named it PrevBtn
Deleted button PrevBtn1
Renamed Sub PrevBtn1 back to Sub PrevBtn
And it works normally. Source QuestionExampleNew
in this code when PrevBtn.Enabled=False is executed,
the Radio_Click routine is not called.

I also tried the following code
in this code when PrevBtn.Enabled=False is executed,
the Sender in the Radio_Click routine is RadioButton1

B4X:
Sub ShowData
  Label1.Text="Q"&(AnsRec+1)&". "
  If Answer(AnsRec)=0 Then
    For i=1 To 5
      Control("RadioBtn"&i).Checked=False
    Next
  Else
  Control("RadioBtn"&Answer(AnsRec)).Checked=True
  End If
  If AnsRec=0 Then
    PrevBtn.Enabled=False
    NextBtn.Enabled=True
  Else If AnsRec=5 Then
    PrevBtn.Enabled=True
    NextBtn.Enabled=False
  Else
    PrevBtn.Enabled=True
    NextBtn.Enabled=True
  End If
End Sub
instead of

B4X:
Sub ShowData
  If AnsRec=0 Then
    PrevBtn.Enabled=False
    NextBtn.Enabled=True
  Else If AnsRec=5 Then
    PrevBtn.Enabled=True
    NextBtn.Enabled=False
  Else
    PrevBtn.Enabled=True
    NextBtn.Enabled=True
  End If
  Label1.Text="Q"&(AnsRec+1)&". "
  If Answer(AnsRec)=0 Then
    For i=1 To 5
      Control("RadioBtn"&i).Checked=False
    Next
  Else
  Control("RadioBtn"&Answer(AnsRec)).Checked=True
  End If
End Sub
in this code when PrevBtn.Enabled=False is executed,
the Sender in the Radio_Click routine is RadioButton2 which gives the wrong information.

Best regards
 

Zenerdiode

Active Member
Licensed User
Many thanks Erel, that has enabled that Sub to execute as I had intended.

Klaus, from my understanding, the Radio_Click event is inadvertantly triggered when focus is shifted to the radio button. A bit like on desktop applications, when you press the <TAB> key; there is an order to which each control is focussed with the <TAB> (In visual C++ the programmer may define this sequence.) In my case, when the 'Prev' button was disabled, the next control in order was the Radio. This then generated the Radio_Click and wrongly populated the array. In your attached code, because you have moved things around, the unwanted event moves to the end; i.e. when you click the final 'Next' the last question is populated from the unwanted event. Again this is cured by shifting focus to the 'Prev' button before disabling 'Next'

:sign0104: (I'm learning quickly...)
 

agraham

Expert
Licensed User
It works fine for me if you move the button enabling/disabling code after the radio button setting but I'm not sure why.:confused:

B4X:
Sub ShowData
   Label1.Text="Q"&(AnsRec+1)&". "
   If Answer(AnsRec)=0 Then
      For i=1 To 5
         Control("RadioBtn"&i).Checked=False
      Next
   Else
      Control("RadioBtn"&Answer(AnsRec)).Checked=True
   End If
   If AnsRec=0 Then
      PrevBtn.Enabled=False
      NextBtn.Enabled=True
   Else If AnsRec=5 Then
      PrevBtn.Enabled=True
      NextBtn.Enabled=False
   Else
      PrevBtn.Enabled=True
      NextBtn.Enabled=True
   End If   
End Sub
EDIT : Got it! I didn't originally get the full import of Erel's explanation. Disabling the Prev button causes the focus to move from the Prev button back to the Radio Button. However AnsRec has already been decremented so Radio_Click thinks this extra click it is a new value for Q1 and stores what was the value for Q2 as Q1's value overwriting the previous value. Putting the disabling later ensures that the correct Radio Button for Q1 has already been selected so that it still overwrites the answer, but with the correct one this time!
 
Last edited:

klaus

Expert
Licensed User
My post was not really clear, I didn't mention that for me the code with the Enabled=false at the end works OK also.
But for me it is still strange that the last RadioButton that had the focus is triggered when the PrevBtn.Enabled=false and this behaviour depends on the order we enter the buttons. If the buttons are entered in a different order the program has a 'normal' behaviour ! ?

Best regards
 
Top