【问题标题】:VB.Net How to close random choosed formVB.Net 如何关闭随机选择的表单
【发布时间】:2013-10-29 16:07:33
【问题描述】:

我正在做一些测验。我已经为问题制作了随机数。因此,当弹出一个问题(表单)并单击正确答案时,会弹出新表单,您可以在其中继续使用一个标签和按钮。我已经做到了,当我单击继续按钮时会弹出下一个问题(表单),但我希望前一个表单也关闭,但我不知道如何命名或做什么。

Dim rn As New Random 
TextBox1.Text = rn.Next(1, 4) 

If TextBox1.Text = 1 Then
   Form4.Show() 
   Form4.Timer1.Start() 
End If 
If TextBox1.Text = 2 Then 
   Form7.Show() 
   Form7.Timer1.Start() 
End If 

If TextBox1.Text = 3 Then 
     Form8.Show() 
     Form8.Timer1.Start() 
End If 
If TextBox1.Text = 4 Then 
    Form12.Show() 
    Form12.Timer1.Start() 
End If

【问题讨论】:

    标签: vb.net random


    【解决方案1】:

    将问题的引用提交给结果表。然后您可以在打开下一个问题之前对上一个问题调用 Close() 方法。

    【讨论】:

    • 为按钮单击事件添加一个事件处理程序,并在之前的表单上调用 Close()。
    【解决方案2】:

    在打开新表单之前关闭所有表单的例程怎么样?

    这不是最好的方法 - 因为您告诉表单即使没有打开也要关闭。 但它会满足你的要求。

    这是一个例子:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim rn As New Random
        Dim randomForm As UInteger = rn.Next(1, 3) 'Random Form Number
        TextBox1.Text = randomForm 'Store the Random Number here
    
        closeQuestionForms() 'Routine called before opening others
    
        Select Case randomForm
            Case 1
                Form2.Show()
                'Do something else here
            Case 2
                Form3.Show()
        End Select
    
    End Sub
    
    
    Private Sub closeQuestionForms() 'Closes the forms
        Form2.Close()
        Form3.Close()
        MessageBox.Show("Closed Question Forms")
    End Sub
    

    同样,其他答案可能会向您展示可能有更好的方法。但是这种方式应该可以正常工作。

    【讨论】:

    • 我做了其他事情。所以我总是打开开始菜单然后提问。我在开始菜单中添加了复选框(使它们不可见)并编写了 if 代码。我添加了继续按钮: If Form1.CheckBox2.Checked = True Then Form4.Close() End If If Form1.CheckBox3.Checked = True Then Form7.Close() End If If Form1.CheckBox4.Checked = True Then Form8.Close () End If Form1.CheckBox2.Checked = False Form1.CheckBox3.Checked = False Form1.CheckBox4.Checked = False 但是还是感谢您的帮助...
    猜你喜欢
    • 2012-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多