Android Question Need your help! simple problem of beginner

FloVB

Member
Hi everyone,
I will need a bit of your time.
I didn’t do visual basic for at least 10 years, so it’s a bit difficult to restart.
You will laugh when you will read my problem.

The little programme I want to do is very simple: I enter numbers, and according to the result of the calculation, the program tell me if it’s significant or not. As I told you nothing complicate.
The only trouble is the program won't let me the time to enter the two numbers, it goes directly to the next stage.

What annoys me when you consider the little programme given on the beginner guide, is that the programme waits for a number to go to the next stage. Of course, I tried to do the same kind of structure but it didn’t work.

Could you give me some advice to do this simple step?

Thanks a lot for your help!
 

sorex

Expert
Licensed User
Longtime User
are you sure you put your code behind a button _Click event?

without seeing any code it's kind of hard to tell you what you did wrong ;)
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
It would be much more efficient if you posted your project, so we can see what exactly you have done.
To post a project:
Make a zip file of the project from the IDE:
upload_2019-6-18_8-50-7.png


And upload this file to the forum with the Upload a File button.
 
Upvote 0

FloVB

Member
Thanks for your answers. Your right I'm going to show you more.
But as I told you,before, I wish to understand why in the tutorial programme "My first program" there isn't any (to all appearances) special instruction to wait an input.

My first program:
.
.
.
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Main")
New
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub btnAction_Click
If btnAction.Text = "O K" Then
If lblResultat.Text = "" Then
lblCommentaire.Text = "No result entered" & CRLF & "Enter a new result" & CRLF & "and click OK."
Else
CheckResult
End If
Else
New
btnAction.Text = "O K"
End If
End Sub

Private Sub New
Number1 = Rnd(1, 10)
Number2 = Rnd(1, 10)
lblNumber1.Text = Number1
lblNumber2.Text = Number2
lblComments.Text = "Enter a result" & CRLF & "and click OK."
lblComments.Color = Colors.RGB(255,235,128)
lblResult.Text = ""
btn0.Visible = False
End Sub

Private Sub CheckResult
If lblResult.Text = Number1 + Number2 Then
lblComments.Text = "GOOD result." & CRLF & "Enter a new result"
lblComments.Color = Colors.RGB(128,255,128)
btnAction.Text = "New"
Else
lblComments.Text = "WRONG result." & CRLF & "Enter a new result" & CRLF & "and click O K."
lblComments.Color = Colors.RGB(255,128,128) '
End If
End Sub

Private Sub btnEvent_Click
Private btnSender As Button

btnSender = Sender

Select btnSender.Tag
Case "BS"
If lblResult.Text.Length > 0 Then
lblResult.Text = lblResult.Text.SubString2(0, lblResult.Text.Length - 1)
End If
Case Else
lblResult.Text = lblResult.Text & btnSender.Text
End Select

If lblResult.Text.Length = 0 Then
btn0.Visible = False
Else
btn0.Visible = True
End If
End Sub

So if you got any idea why this simple program works...


And there is what I tried to do:
.
.
.
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("main")

TestResult

CalculDT_DL

Significant


End Sub


Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub TestResult
lblEditBgd.Text = 0
lblEditRaw.Text = 0
Confidence = 0
lblComments.Text = "Enter Measured Bgd"
lblComments.Color = Colors.RGB(255,235,128) 'couleur jaune



If lblEditBgd.Text > 0 Then
lblComments.Text = "Enter Measured Raw"
Else
Wait For edtRaw_TextChanged (Old As Label, New As Label)
'Wait For btnOK_Click

If Raw > 0 Then
lblComments.Text = "Select a confidence interval"
Wait For checkbox1_CheckedChange(Checked As CheckBox)
Wait For btnOK_Click
End If
End If

End Sub


Sub CheckBox_CheckedChange(Checked As Boolean)

If checkbox1.Checked = True Then
K=1.645
Confidence=checkbox1.Tag
End If

If checkbox2.Checked = True Then
K=1.96
Confidence=checkbox2.Tag
End If

If checkbox3.Checked = True Then
K=2.1
Confidence=checkbox3.Tag
Else

If checkbox4.Checked = True Then
K=3
Confidence=checkbox4.Tag

End If
End If

End Sub


Private Sub CalculDT_DL

DT=K*Sqrt(2*(Bgd+1))
DL=(K*K)+(2*DT)
NET=Raw-Bgd
End Sub


Private Sub Significant

If NET>DT Then
lblComments.Text = "Significant: Net>DT @" + Confidence
lblComments.Color = Colors.RGB(255,0,0)
Else
lblComments.Text = "Not Significant: Net<DL @" + Confidence
lblComments.Color = Colors.RGB(0,255,0)
End If
End Sub

Sub btnOK_Click


End Sub


So there it is! don't hesitate if you got any question.
Thanks by advance for your advices.
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
FloVB....

That's a lot of code to digest without indent or formatting. Please use the 'Code' tags in the editor so we can actually read it, or export the project as a zip so other users can look at it in the IDE.

B4X:
Private Sub Significant

   If NET>DT Then
      lblComments.Text = "Significant: Net>DT @" + Confidence
      lblComments.Color = Colors.RGB(255,0,0)
   Else
      lblComments.Text = "Not Significant: Net<DL @" + Confidence
      lblComments.Color = Colors.RGB(0,255,0)
   End If

End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Why haven't you posted your project?
Or can't you add a file to the forum as an unlicensed user?
I am afraid that you don't really understand how Android does work.
Android is event driven, the program reacts to user actions.
You are trying to use Wait For, but this is the wrong way.
As you have the code of MyFirstProgram, I suppose that you have downloaded the B4X Booklets.
You might read chapter 3 Program flow / Process life cycle in the B4X Basic Language booklet.

When your program starts you:
- load the layout, that's OK
- then you call TestResults, but you don't have any data yet. And you try to wait for user input, which is wrong.
- then you call CalculDT_DL and Significant which are also in the wrong place.

You should put these calls in the Sub btnOK_Click routine.
Then when the progam starts you load the layout.
And the program waits for the user input and actions.
The user can enter his data.
And then, when the he clicks on tke OK button, the btnOK_Click event routine is called by the operating system executing the code inside.

Anyway, post your project, this would be much easier for us to help you.
It is much more difficult to help without the project.
For example you hace the Sub CheckBox_CheckedChange(Checked As Boolean) routine.
And in this routine you look at different checkboxes, and as we don't have the layout, we don't know how you have implemented all this.
 
Upvote 0

FloVB

Member
Sure no problem, I'll post the project.
I'll take a closer look at your advice!
And yes once again your right, I don't know much about android...
 
Upvote 0

FloVB

Member
Hi there!

Please find below my full program.
In a few words, it's a classical statistic calculation for detection threshold and detection limit.

I have tried to update with the advice you gave me.
I'm conscious it's clumsy because I'm a bit confused: I was more use to program with assembly language and basic...
So don't hesitate if you have any question, remark advice, etc.

Thanks a lot!


B4X:
DT-DL:
#Region  Project Attributes
            #ApplicationLabel: Detection Threshold - Detection Limit
            #VersionCode: 2
            #VersionName:
            'SupportedOrientations possible values: unspecified, landscape or portrait.
            #SupportedOrientations: unspecified
            #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
            #FullScreen: False
            #IncludeTitle: True
#End Region


Sub Process_Globals
            'These global variables will be declared once when the application starts.
            'These variables can be accessed from all modules.

End Sub
[FONT=Akkurat Std][COLOR=rgb(28, 30, 41)][/COLOR][/FONT]

Sub Globals
            'These global variables will be redeclared each time the activity is created.
            'These variables can only be accessed from this module.

            Private checkbox1, checkbox2, checkbox3, checkbox4 As CheckBox
            
            Public ResultBgd, ResultRaw, K, Confidence, DT, DL, NET, Bgd, Raw As Int
            
            Private lblCommentaire, lblConfidence, lblBgd, lblCS1, lblCS2, lblPercent1, lblPercent2, lblPercent3, lblPercent4, lblRaw, lblEditBgd, lblEditRaw As Label
            
            Private btnOK As Button
            
End Sub


Sub Activity_Create(FirstTime As Boolean)
            Activity.LoadLayout("main")           
            
End Sub


Sub Activity_Resume

End Sub


Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub btnOK_Click
        
            btnOK = Sender
            
            
            TestResult

            CalculDT_DL
            
            
            if  btnOK.Text = "O K" Then
            
            Significant
            
            End if
            
End Sub


Sub TestResult
            lblEditBgd.Text = 0
            lblEditRaw.Text = 0
            Confidence = 0
            lblCommentaire.Text = "Enter Measured Bgd"
            lblCommentaire.Color = Colors.RGB(255,235,128)    'yellow     
            
            If lblEditBgd.Text > 0 Then
                        lblCommentaire.Text = "Enter Measured Raw"
                        Else
                        Wait For edtRaw_TextChanged (Old As Label, New As Label)   
                        
                        If Raw > 0 Then
                                    lblCommentaire.Text = "Select a confidence interval"                     
                                  
            End If
            End If
            
End Sub


Sub CheckBox_CheckedChange(Checked As Boolean)
            
            If checkbox1.Checked = True Then                           'checked = True
                        K=1.645
                        Confidence=checkbox1.Tag
            End If
                        
            If checkbox2.Checked = True Then                         
                        K=1.96
                        Confidence=checkbox2.Tag
            End If
                        
            If checkbox3.Checked = True Then                           
                        K=2.1
                        Confidence=checkbox3.Tag
            Else                                                                             
                        
            If checkbox4.Checked = True Then                           
                        K=3
                        Confidence=checkbox4.Tag
                                                                                    
            End If
            End If
            
End Sub


Private Sub CalculDT_DL
                                      
            DT=K*Sqrt(2*(Bgd+1))
            DL=(K*K)+(2*DT)
            NET=Raw-Bgd                                 
End Sub


Private Sub Significant
            
            If NET>DT Then
                        lblCommentaire.Text = "Significant: Net>DT @" + Confidence
                        lblCommentaire.Color = Colors.RGB(255,0,0)     'red
            Else
                        lblCommentaire.Text = "Not Significant: Net<DL @" + Confidence
                        lblCommentaire.Color = Colors.RGB(0,255,0)     'green
            End If
End Sub
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
From looking at the code, there are several issues
I have changed some of the subs to get you a bit closer, it hasn't been tested so may contain a few bugs

As Klaus advised you need to take a look at the Android event model

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")
    lblCommentaire.Text = "Enter Bgd, Raw and confidence interval"
            
End Sub


Sub Activity_Resume

End Sub


Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub btnOK_Click
        
            
    If TestResult Then

      CalculDT_DL
      Significant
    End If       
            
            
End Sub


Sub TestResult As Boolean
' Check each of the input fields for validity

    If Not( lblEditBgd.Text > 0) Then
    
        lblCommentaire.Text = "Enter Measured Bgd"
        lblCommentaire.Color = Colors.RGB(255,235,128)    'yellow
        Return False
    End If
    
    If Not(lblEditRaw.Text > 0) Then
        lblCommentaire.Text = "Enter Measured Raw"
        Return False             
    End If
    
    If Confidence = 0 Then
        lblCommentaire.Text = "Select a confidence interval"
        Return False
    End If
    ' Passed all tests so should  be ok
    Return True
End Sub

' I assume all checkboxes have the same event
Sub CheckBox_CheckedChange(Checked As Boolean)
            
    If checkbox1.Checked = True Then                           'checked = True
        K=1.645
        Confidence=checkbox1.Tag
    else If checkbox2.Checked = True Then
        K=1.96
        Confidence=checkbox2.Tag
    else If checkbox3.Checked = True Then
        K=2.1
        Confidence=checkbox3.Tag
    Else If checkbox4.Checked = True Then
            K=3
            Confidence=checkbox4.Tag
    Else ' Reset the confidence flag if no checkbox is set
        k = 0
        Confidence = 0
    End If
            
End Sub
 
Upvote 0
Top