我的If Else声明VB.Net的奇怪行为

时间:2014-02-10 02:38:41

标签: vb.net winforms forms

我有这个程序,当我在表单上键入相同的文本和另一个表单时,会在我的上一个表单的标签上添加一个(+1)..没有错误但是当我写了正确的答案时文本框它只给了我1号而不是10号。请帮助我们。在此先感谢:)

这是我的代码:

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    frm4 = New Form4()
    frm1 = New Form1()
    Dim Textbox1 As New TextBox
    Dim Label3 As New Label
    If Textbox1.Text = frm1.TextBox2.Text Then
        frm4.Label3.Text = (frm4.Label3.Text) + 1
        If TextBox2.Text = frm1.TextBox4.Text Then
            frm4.Label3.Text = (frm4.Label3.Text) + 1
            If TextBox3.Text = frm1.TextBox6.Text Then
                frm4.Label3.Text = (frm4.Label3.Text) + 1
                If TextBox4.Text = frm1.TextBox8.Text Then
                    frm4.Label3.Text = (frm4.Label3.Text) + 1
                    If TextBox5.Text = frm1.TextBox10.Text Then
                        frm4.Label3.Text = (frm4.Label3.Text) + 1
                        If TextBox6.Text = frm1.TextBox12.Text Then
                            frm4.Label3.Text = (frm4.Label3.Text) + 1
                            If TextBox7.Text = frm1.TextBox14.Text Then
                                frm4.Label3.Text = (frm4.Label3.Text) + 1
                                If TextBox8.Text = frm1.TextBox16.Text Then
                                    frm4.Label3.Text = (frm4.Label3.Text) + 1
                                    If TextBox9.Text = frm1.TextBox18.Text Then
                                        frm4.Label3.Text = (frm4.Label3.Text) + 1
                                        If TextBox10.Text = frm1.TextBox20.Text Then
                                            frm4.Label3.Text = (frm4.Label3.Text) + 1
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
            End If






        End If
        frm4.Show()

    Else
        frm4.Label3.Text = frm4.Label3.Text

        frm4.Show()
    End If

End Sub

表格2代码:

 Public Class Form2
Private frm1 As Form1 ' remove the new here because it creates a new instance of Form1'
Private frm3 As Form3

Public lbl As New Label ' not needed?'

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.Controls.Add(lbl)

End Sub
Public Sub New(ByVal callerInstance As Form1)

    ' Call required if you add your constructor manually
    InitializeComponent()

    ' save the instance of the Me variable passed to this constructor
    frm1 = callerInstance
End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    ' Dim Label21 As Label = New Label          ' this label is not needed'
    ' Dim Textbox1 As TextBox = New TextBox     ' this  textbox is not needed'

    ' Create a new instance of Form3, do not use the automatic Form3 instance
    ' automatically created by VB.NET 
    frm3 = New Form3()

    ' now you are referring to the caller instance of Form1 '
    ' where there is the textbox filled with your text '
    frm3.Label21.Text = frm1.TextBox1.Text
    frm3.Label22.Text = frm1.TextBox3.Text
    frm3.Label23.Text = frm1.TextBox5.Text
    frm3.Label24.Text = frm1.TextBox7.Text
    frm3.Label25.Text = frm1.TextBox9.Text
    frm3.Label26.Text = frm1.TextBox11.Text
    frm3.Label27.Text = frm1.TextBox13.Text
    frm3.Label28.Text = frm1.TextBox15.Text
    frm3.Label29.Text = frm1.TextBox17.Text
    frm3.Label30.Text = frm1.TextBox19.Text






    frm3.Show()

End Sub
End Class

Form1代码:

 Public Class Form1
Private frm2 As Form2

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    If frm2 Is Nothing Then
        frm2 = New Form2(Me)
        AddHandler frm2.FormClosed, AddressOf Me.Form2HasBeenClosed

        Dim Label21 As Label = New Label
        frm2.Label21.Text = TextBox1.Text
        frm2.Label21.ForeColor = Color.Black


        Dim Label22 As Label = New Label
        frm2.Label22.Text = TextBox2.Text
        frm2.Label22.ForeColor = Color.Black


        Dim Label23 As Label = New Label
        frm2.Label23.Text = TextBox3.Text
        frm2.Label23.ForeColor = Color.Black

        Dim Label24 As Label = New Label
        frm2.Label24.Text = TextBox4.Text
        frm2.Label24.ForeColor = Color.Black


        Dim Label25 As Label = New Label
        frm2.Label25.Text = TextBox5.Text
        frm2.Label25.ForeColor = Color.Black


        Dim Label26 As Label = New Label
        frm2.Label26.Text = TextBox6.Text
        frm2.Label26.ForeColor = Color.Black


        Dim Label27 As Label = New Label
        frm2.Label27.Text = TextBox7.Text
        frm2.Label27.ForeColor = Color.Black


        Dim Label28 As Label = New Label
        frm2.Label28.Text = TextBox8.Text
        frm2.Label28.ForeColor = Color.Black


        Dim Label29 As Label = New Label
        frm2.Label29.Text = TextBox9.Text
        frm2.Label29.ForeColor = Color.Black

        Dim Label30 As Label = New Label
        frm2.Label30.Text = TextBox10.Text
        frm2.Label30.ForeColor = Color.Black


        Dim Label31 As Label = New Label
        frm2.Label31.Text = TextBox11.Text
        frm2.Label31.ForeColor = Color.Black


        Dim Label32 As Label = New Label
        frm2.Label32.Text = TextBox12.Text
        frm2.Label32.ForeColor = Color.Black


        Dim Label33 As Label = New Label
        frm2.Label33.Text = TextBox13.Text
        frm2.Label33.ForeColor = Color.Black


        Dim Label34 As Label = New Label
        frm2.Label34.Text = TextBox14.Text
        frm2.Label34.ForeColor = Color.Black


        Dim Label35 As Label = New Label
        frm2.Label35.Text = TextBox15.Text
        frm2.Label35.ForeColor = Color.Black

        Dim Label36 As Label = New Label
        frm2.Label36.Text = TextBox16.Text
        frm2.Label36.ForeColor = Color.Black


        Dim Label37 As Label = New Label
        frm2.Label37.Text = TextBox17.Text
        frm2.Label37.ForeColor = Color.Black


        Dim Label38 As Label = New Label
        frm2.Label38.Text = TextBox18.Text
        frm2.Label38.ForeColor = Color.Black


        Dim Label39 As Label = New Label
        frm2.Label39.Text = TextBox19.Text
        frm2.Label39.ForeColor = Color.Black


        Dim Label40 As Label = New Label
        frm2.Label40.Text = TextBox20.Text
        frm2.Label40.ForeColor = Color.Black


    End If

    If frm2 IsNot Nothing Then
        frm2.Show(Me) 'Show Second Form  
        Me.Hide()
    End If

End Sub
Sub Form2HasBeenClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)
    frm2 = Nothing
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
End Class

表单3代码:(实例调用form1)

Public Class Form3
Private frm3 As Form3
Private frm1 As Form1
Private frm4 As Form4

Public Sub New1(ByVal callerInstance As Form1)

    ' Call required if you add your constructor manually
    InitializeComponent()

    ' save the instance of the Me variable passed to this constructor
    frm1 = callerInstance
End Sub

结束班

2 个答案:

答案 0 :(得分:1)

如果您希望在正确答案的标签号中显示,那么     您需要将所有用户答案与正确的答案进行比较     并计算答案相同的时间:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
frm4 = New Form4()
frm1 = New Form1()
Dim Textbox1 As New TextBox
Dim Label3 As New Label
Dim sameQnt As Int32 = 0

If Textbox1.Text = frm1.TextBox2.Text Then sameQnt += 1
If Textbox2.Text = frm1.TextBox4.Text Then sameQnt += 1
If Textbox3.Text = frm1.TextBox6.Text Then sameQnt += 1
If Textbox4.Text = frm1.TextBox8.Text Then sameQnt += 1
If Textbox5.Text = frm1.TextBox10.Text Then sameQnt += 1
If Textbox6.Text = frm1.TextBox12.Text Then sameQnt += 1
If Textbox7.Text = frm1.TextBox14.Text Then sameQnt += 1
If Textbox8.Text = frm1.TextBox16.Text Then sameQnt += 1
If Textbox9.Text = frm1.TextBox18.Text Then sameQnt += 1
If Textbox10.Text = frm1.TextBox20.Text Then sameQnt += 1

'Print reslut
frm4.Label3.Text = sameQnt.ToString()
frm4.Show()
End Sub

答案 1 :(得分:0)

这是我的新答案

Public Class MainForm '<----Your main form class name

    Dim frm4 = New Form4()'<----declare it here
    Dim frm1 = New Form1()'<----declare it here

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim Textbox1 As New TextBox
        Dim Label3 As New Label

        Dim i As Integer = 0
        If Textbox1.Text = frm1.TextBox2.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
        If TextBox2.Text = frm1.TextBox4.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
        If TextBox3.Text = frm1.TextBox6.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
        If TextBox4.Text = frm1.TextBox8.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
        If TextBox5.Text = frm1.TextBox10.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
        If TextBox6.Text = frm1.TextBox12.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
        If TextBox7.Text = frm1.TextBox14.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
        If TextBox8.Text = frm1.TextBox16.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
        If TextBox9.Text = frm1.TextBox18.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
        If TextBox10.Text = frm1.TextBox20.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1

        frm4.Label3.Text = i.ToString()

        frm4.Show()

    End Sub

End Class