Hi good day guys i would just like to seek some help regarding my problem here in VB.NET. I have this program that if the text in the textbox is equal to the text in the label of the previous form it will add 1 for each correct answer but instead it always shows zero (0) (last form) and not showing the results that I want. Here is my code guys I hope you can help me with this problem thanks in advance.

Imports System.Convert
Imports System.IO
Public Class Form3

    Private frm1 As Form1
    Private frm4 As Form4

    Public Sub New1(ByVal callerInstance As Form1)

        InitializeComponent()

        ' save the instance of the Me variable passed to this constructor 
        frm1 = callerInstance
    End Sub
    Public Sub New2(ByVal callerInstance As Form4)

        InitializeComponent()
        ' save the instance of the Me variable passed to this constructor 
        frm4 = callerInstance
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frm1 As Form1 = Form1
        Dim frm4 As Form4 = Form4
        frm1 = New Form1
        frm4 = New Form4

        'program execution proper 

        Dim lbl3 As Integer = CInt(frm4.Label3.Text)
        lbl3 = CInt(frm4.Label3.Text)
        Dim Label22 As New Label
        If frm1.TextBox2.Text = TextBox1.Text Then
            lbl3 = CInt(lbl3) + 0 'if(integer.tryparse, lbl3) then lbl3 += 1 


            Dim Label24 As New Label
            If Not frm1.TextBox4.Text = TextBox2.Text Then
                lbl3 = CInt(lbl3) + 0

                Dim Label26 As New Label
                If Not frm1.TextBox6.Text = TextBox3.Text Then
                    lbl3 = CInt(lbl3) + 0

                    Dim Label28 As New Label
                    If Not frm1.TextBox8.Text = TextBox4.Text Then
                        lbl3 = CInt(lbl3) + 0
                        Dim Label30 As New Label
                        If Not frm1.TextBox10.Text = TextBox5.Text Then
                            lbl3 = CInt(lbl3) + 0
                            Dim Label32 As New Label
                            If Not frm1.TextBox12.Text = TextBox6.Text Then
                                lbl3 = CInt(lbl3) + 0
                                Dim Label34 As New Label
                                If Not frm1.TextBox14.Text = TextBox7.Text Then
                                    lbl3 = CInt(lbl3) + 0
                                    Dim Label36 As New Label
                                    If Not frm1.TextBox16.Text = TextBox8.Text Then
                                        lbl3 = CInt(lbl3) + 0
                                        Dim Label38 As New Label
                                        If Not frm1.TextBox18.Text = TextBox9.Text Then
                                            lbl3 = CInt(lbl3) + 0
                                            Dim Label40 As New Label
                                            If Not frm1.TextBox20.Text = TextBox10.Text Then
                                                lbl3 = CInt(lbl3) + 0 'frm4.Label3.Text = (frm4.Label3.Text) + 1 

                                                frm4.Show()
                                                Me.Hide()
                                            Else
                                                lbl3 = CInt(lbl3) + 1 'Int((lbl3 = Convert.ToInt64(lbl3)) + 1) 

                                                frm4.Show()
                                                Me.Hide()
                                            End If
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If

        If frm4 IsNot Nothing Then
            frm4.Visible = False
            frm4.Show(Me) 'Show Second Form 

            Me.Hide()
        End If
    End Sub


End Class

Edited 2 Years Ago by Reverend Jim: Fixed code formatting

The else statement is only executed if the innermost If statement is false. If any of the others are false the execution will bypass the the addition statement. A quick fix, is an else statement for each of the if statements and don't nest them, have each one separate.

On a side note, refactoring your code will make it more concise and easier to maintain. Using the Controls collection and the methods in there, will allow you to make a collection of controls so that you can iterate through them to check for equal.

Edited 2 Years Ago by tinstaafl

One way to refactor your code is like this:

    For Each c As TextBox In Me.Controls.OfType(Of TextBox).Where(Function(x) x.Name.StartsWith("TextBox"))
        Dim index As Integer = Integer.Parse(c.Name.Split({"TextBox", "."}, 1, StringSplitOptions.None)(0))
        Dim txtbx1 As TextBox = c
        Dim txtbx2 As TextBox = frm1.Controls.OfType(Of TextBox).First(Function(x) x.Name = "TextBox" & (index * 2).ToString)
        Dim lbl As New Label With {.Name = "Label" & ((index + 11) * 2)}
        If txtbx1.Text = txtbx2.Text Then
            lbl3 += 1
        Else
            'lbl.Location = New Point(x coordinate, y coordinate) add your algorithm for the placement of the labels
            'lbl.Text = add the text for the label here
            Me.Controls.Add(lbl)
        End If            
    Next     

One advantage of this way is that, if you need to add or subtract a set of textboxes, as long as the names still conform to the pattern your code will work.

Edited 2 Years Ago by tinstaafl

Public Class Form2
    Dim frm As New Form1
    Dim myScore As Integer = 0
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim lbllist = (From lbl In frm.Controls Where TypeOf (lbl) Is Label Select DirectCast(lbl, Label).Text).ToList()
        For Each lbltext As String In lbllist
            If Not String.IsNullOrEmpty(TextBox1.Text) Then
                If TextBox1.Text.Equals(lbltext) Then
                    myScore = myScore + 1
                End If
            End If
        Next
    End Sub
End Class
This article has been dead for over six months. Start a new discussion instead.