Visual Baic 2010.如何重新加载表单以启动其原始状态? [英] Visual Baic 2010. How to re-load form one to start in its original state?

查看:73
本文介绍了Visual Baic 2010.如何重新加载表单以启动其原始状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在研究了这个项目之后,我发现我并不完全理解TryCast(发件人,标签)的含义或含义。

然后我想用另一组随机字符重新启动程序但是我不能像在Button1_Click1中那样做任何其他方式。这里我使用Shell命令在项目Bin / Debug文件夹中运行.exe文件。我知道这是一种无用的方法来做到这一点但我找不到另一种重新启动程序的方法,包括所有新的图标位置。



任何帮助都会非常感激。

请查看代码如下。



After studying this project, I found I did not understand exactly what "TryCast(sender, Label) means or does.
I then wanted to re-start the program with another set of random characters but I can''t do it any other way than as in Button1_Click1. Here I use the Shell command to run the .exe file in the project Bin/Debug folder. I know this is a useless way to do this but I cannot find another way to re-start the program with all new positions for icons.

Any help would be truly appreciated.
Please check out the code below.

Public Class Form1
    
    Private firstClicked As Label = Nothing
    
    Private secondClicked As Label = Nothing

    Private random As New Random

    Private icons = New List(Of String) From {"!", "!", "b", "b", "$", "$", "%", "%", "~", "~", "j", "j", "J", "J", "Z", "Z",
                                              "f", "f", "h", "h", "m", "m", "o", "o", "p", "p", "u", "u", "s", "s", "-", "-", "U", "U", "O", "O"}

    Public Sub New()

        '' This call is required by Windows Form Designer but I do not know why?????
        InitializeComponent()
        '' Add any initialization after the InitializeComponent() call
        AssignIconsToSquares()
    End Sub

    Private Sub AssignIconsToSquares()
        
        For Each control In TableLayoutPanel1.Controls       
            Dim iconLabel = TryCast(control, Label)   ''What does this mean and do?

            If iconLabel IsNot Nothing Then                  
                Dim randomNumber = random.Next(icons.Count)
                iconLabel.Text = icons(randomNumber)
                iconLabel.ForeColor = iconLabel.BackColor   
                icons.RemoveAt(randomNumber)                 
            End If                                           
        Next                                               
    End Sub
    
    Dim Trys As Integer
    Private Sub Label_click(sender As System.Object, e As System.EventArgs) Handles Label9.Click, Label8.Click, Label7.Click, Label6.Click, Label5.Click, Label4.Click, Label30.Click, Label3.Click, Label29.Click, Label28.Click, Label27.Click, Label26.Click, Label25.Click, Label24.Click, Label23.Click, Label22.Click, Label21.Click, Label20.Click, Label2.Click, Label19.Click, Label18.Click, Label17.Click, Label16.Click, Label15.Click, Label14.Click, Label13.Click, Label12.Click, Label11.Click, Label10.Click, Label1.Click, Label31.Click, Label32.Click, Label33.Click, Label34.Click, Label35.Click, Label36.Click
        Trys = Trys + 1
        Me.Text = "You have used   " & Trys & "   Trys"

        Dim clickedLabel = TryCast(sender, Label) ''I do not understand this??

        If clickedLabel IsNot Nothing Then

            If clickedLabel.ForeColor = Color.Black Then Exit Sub 

            clickedLabel.ForeColor = Color.Black
            If firstClicked Is Nothing Then
                firstClicked = clickedLabel
                firstClicked.ForeColor = Color.Black 
                Exit Sub                                 
            End If
            secondClicked = clickedLabel 
            secondClicked.ForeColor = Color.Black
            CheckForWinner()
            
            If firstClicked.Text = secondClicked.Text Then 
                firstClicked = Nothing 
                secondClicked = Nothing
                Exit Sub                                   
            End If

            Timer1.Start()

        End If
    End Sub

    Private Sub CheckForWinner()
       
        For Each control In TableLayoutPanel1.Controls
            Dim iconLabel = TryCast(control, Label)                    ''mmmmmmmm
            If iconLabel IsNot Nothing AndAlso
               iconLabel.ForeColor = iconLabel.BackColor Then Exit Sub
        Next

        '' If the loop didn''t return, it didn''t find  any unmatched icons, the user won. Show a message and close the form
        
        Me.Text = "Congratulations You have found all Pairs. You have used   " & Trys & "   Trys"
        Button1.Visible = True

    End Sub

    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
       
        Timer1.Stop()
      
        firstClicked.ForeColor = firstClicked.BackColor
        secondClicked.ForeColor = secondClicked.BackColor
     
        firstClicked = Nothing
        secondClicked = Nothing

    End Sub

    Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Me.Close()
        Button1.Visible = False
       
        Trys = 0
        Me.Text = "You have used   " & Trys & "   Trys"

        ''Here, I need to re-start the game with a fresh list of Random symbols but I have no idea how.
        ''So I used the following code to re-run the project
        Shell("C:\Users\Rapid\Documents\Visual Studio 2010\Projects\QuickMatch\QuickMatch\bin\Debug\QuickMatch.exe")
        ''The outcome is spot on! The program randomly places icons on every label contained within the TableLayoutPanel

    End Sub

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

        WindowState = vbNormal

    End Sub

End Class

推荐答案


,%,%,〜,〜,j,j,J, J,Z,Z,
f,f,h,h,m,m,o,o,p ,p,你,你,s,s, - , - ,U,U,O,O}

Public Sub New()

''Windows Form Designer需要此调用,但我不知道为什么?????
InitializeComponent()
''在InitializeComponent()调用之后添加任何初始化
AssignIconsToSquares()
End Sub

Private Sub AssignIconsToSquares()

For Table in TableL ayoutPanel1.Controls
Dim iconLabel = TryCast(control,Label)''这意味着什么?

如果iconLabel IsNot Nothing那么
Dim randomNumber = random.Next(icons.Count)
iconLabel.Text = icons(randomNumber)
iconLabel.ForeColor = iconLabel。 BackColor
icons.RemoveAt(randomNumber)
End if
Next
End Sub

Dim Trys As Integer
Private Sub Label_click(sender As System.Object,e As System.EventArgs)处理Label9.Click,Label8.Click,Label7.Click,Label6.Click,Label5.Click,Label4.Click,Label30.Click,Label3.Click,Label29.Click,Label28.Click ,Label27.Click,Label26.Click,Label25.Click,Label24.Click,Label23.Click,Label22.Click,Label21.Click,Label20.Click,Label2.Click,Label19.Click,Label18.Click,Label17.Click,Label16 .Click,Label15.Click,Label14.Click,Label13.Click,Label12.Clic k,Label11.Click,Label10.Click,Label1Click,Label31.Click,Label32.Click,Label33.Click,Label34.Click,Label35.Click,Label36.Click
Trys = Trys + 1
Me.Text =你用过了&特里斯& Trys

Dim clickedLabel = TryCast(sender,Label)''我不明白这个?

如果clickedLabel IsNot Nothing那么

如果clickedLabel.ForeColor = Color.Black然后退出Sub

clickedLabel.ForeColor = Color.Black
如果firstClicked是Nothing那么
firstClicked = clickedLabel
firstClicked.ForeColor = Color.Black
退出Sub
结束如果
secondClicked = clickedLabel
secondClicked.ForeColor = Color.Black
CheckForWinner()

如果firstClicked.Text = secondClicked.Text则
firstClicked = Nothing
secondClicked = Nothing
退出Sub
结束如果

Timer1.Start()

结束如果
结束子

私人子CheckForWinner()

For each contro l在TableLayoutPanel1.Controls
Dim iconLabel = TryCast(control,Label)''mmmmmmmm
如果iconLabel IsNot Nothing AndAlso
iconLabel.ForeColor = iconLabel.BackColor然后退出Sub
Next

''如果循环没有返回,它没有找到任何不匹配的图标,用户赢了。显示信息并关闭表格

Me.Text =恭喜你找到了所有的对。你曾经使用过&特里斯& Trys
Button1.Visible = True

End Sub

Private Sub Timer1_Tick(sender As System.Object,e As System.EventArgs)处理Timer1.Tick

Timer1.Stop()

firstClicked.ForeColor = firstClicked.BackColor
secondClicked.ForeColor = secondClicked.BackColor

firstClicked = Nothing
secondClicked = Nothing

End Sub

Private Sub Button1_Click_1(sender As System.Object,e As System.EventArgs)Handles Button1.Click
Me .Close()
Button1.Visible = False

Trys = 0
Me.Text =你用过了&特里斯& Trys

''在这里,我需要用一个新的随机符号列表重新开始游戏,但我不知道如何。
''所以我使用以下代码重新运行项目
Shell(C:\ Users \Rapid \Documents\Visual Studio 2010 \Projects \QuickMatch \QuickMatch \bin\Debug \QuickMatch.exe)
''结果是现货!该程序在TableLayoutPanel中包含的每个标签上随机放置图标

End Sub

Public Sub Form1_Load(sender As System.Object,e As System.EventArgs)处理MyBase.Load

WindowState = vbNormal

End Sub

End Class
", "%", "%", "~", "~", "j", "j", "J", "J", "Z", "Z", "f", "f", "h", "h", "m", "m", "o", "o", "p", "p", "u", "u", "s", "s", "-", "-", "U", "U", "O", "O"} Public Sub New() '' This call is required by Windows Form Designer but I do not know why????? InitializeComponent() '' Add any initialization after the InitializeComponent() call AssignIconsToSquares() End Sub Private Sub AssignIconsToSquares() For Each control In TableLayoutPanel1.Controls Dim iconLabel = TryCast(control, Label) ''What does this mean and do? If iconLabel IsNot Nothing Then Dim randomNumber = random.Next(icons.Count) iconLabel.Text = icons(randomNumber) iconLabel.ForeColor = iconLabel.BackColor icons.RemoveAt(randomNumber) End If Next End Sub Dim Trys As Integer Private Sub Label_click(sender As System.Object, e As System.EventArgs) Handles Label9.Click, Label8.Click, Label7.Click, Label6.Click, Label5.Click, Label4.Click, Label30.Click, Label3.Click, Label29.Click, Label28.Click, Label27.Click, Label26.Click, Label25.Click, Label24.Click, Label23.Click, Label22.Click, Label21.Click, Label20.Click, Label2.Click, Label19.Click, Label18.Click, Label17.Click, Label16.Click, Label15.Click, Label14.Click, Label13.Click, Label12.Click, Label11.Click, Label10.Click, Label1.Click, Label31.Click, Label32.Click, Label33.Click, Label34.Click, Label35.Click, Label36.Click Trys = Trys + 1 Me.Text = "You have used " & Trys & " Trys" Dim clickedLabel = TryCast(sender, Label) ''I do not understand this?? If clickedLabel IsNot Nothing Then If clickedLabel.ForeColor = Color.Black Then Exit Sub clickedLabel.ForeColor = Color.Black If firstClicked Is Nothing Then firstClicked = clickedLabel firstClicked.ForeColor = Color.Black Exit Sub End If secondClicked = clickedLabel secondClicked.ForeColor = Color.Black CheckForWinner() If firstClicked.Text = secondClicked.Text Then firstClicked = Nothing secondClicked = Nothing Exit Sub End If Timer1.Start() End If End Sub Private Sub CheckForWinner() For Each control In TableLayoutPanel1.Controls Dim iconLabel = TryCast(control, Label) ''mmmmmmmm If iconLabel IsNot Nothing AndAlso iconLabel.ForeColor = iconLabel.BackColor Then Exit Sub Next '' If the loop didn''t return, it didn''t find any unmatched icons, the user won. Show a message and close the form Me.Text = "Congratulations You have found all Pairs. You have used " & Trys & " Trys" Button1.Visible = True End Sub Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick Timer1.Stop() firstClicked.ForeColor = firstClicked.BackColor secondClicked.ForeColor = secondClicked.BackColor firstClicked = Nothing secondClicked = Nothing End Sub Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click Me.Close() Button1.Visible = False Trys = 0 Me.Text = "You have used " & Trys & " Trys" ''Here, I need to re-start the game with a fresh list of Random symbols but I have no idea how. ''So I used the following code to re-run the project Shell("C:\Users\Rapid\Documents\Visual Studio 2010\Projects\QuickMatch\QuickMatch\bin\Debug\QuickMatch.exe") ''The outcome is spot on! The program randomly places icons on every label contained within the TableLayoutPanel End Sub Public Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load WindowState = vbNormal End Sub End Class


那里在.NET中没有像 TryCast 那样愚蠢的想法,这是一个纯粹的VB.NET,几乎相当于 运营商:

http:// msdn.microsoft.com/en-us/library/zyy863x8%28v=vs.110%29.aspx [ ^ ]。



在您的情况下,使用它会尝试将 Control 强制转换为 Label 。由于所有标签都是控件,但并非所有控件都是标签,因此向下投射,这可能是成功与否。不成功的强制转换(当 Control 的实例不是标签时,由Null表示(VB.NET <$ c) $ c>没什么)。很简单,不是吗?



这与原始状态的问题完全无关在.NET中没有这样的工具,如果你考虑它,它甚至不应该存在。只有表单代码的作者知道什么应该被认为是原始状态当你只是向表单添加一些控件时,你创建了一些状态机。它可能不仅仅是控件,还可能是你可能需要添加的任何其他实例成员。你永远不会指出阶段。 state声明为initial;代码add逐个控制并修改它的一些属性。



特别是,我建议你开发一个将所有表单状态转换为某种初始状态的方法。为了保持一致性,您可以在某个时刻调用此方法一次表格首次显示在屏幕上的时间,稍后在需要时调用相同的方法。



-SA
There is no such stupid think as TryCast in .NET, this is a pure VB.NET thingy, pretty much equivalent to the as operator:
http://msdn.microsoft.com/en-us/library/zyy863x8%28v=vs.110%29.aspx[^].

In your case, the use of it tries to cast Control to Label. As all labels are controls but not all controls are labels, this is down-casting, which can be successful or not. The unsuccessful cast (when an instance of Control is not a Label is indicated by Null (VB.NET Nothing). Pretty simple, isn''t it?

And this is totally unrelated to the problem of "original state" of the form. There is no such facility in .NET, and, if you think about it, it should not even exist. Only the author of a form code knows what''s supposed to be considered as the "original" state of a form or anything at all. When you merely add some controls to the form, you create some state machine. It might be not just controls, but also any other instance members you may need to add. You never indicate the phase when the state is to be claimed as "initial"; the code add controls one by one and modifies some of its properties.

In particular, I can suggest that you develop a method which turns all the form state in some "initial" state. For consistency, you can call this method once at some moment of time the form is first shown on screen, and later call the same method when it is needed.

—SA


这篇关于Visual Baic 2010.如何重新加载表单以启动其原始状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆