2016-06-22 61 views
0

我目前正在爲我製作的一些vbs腳本製作一個簡單的UI(用戶界面)。我剛剛在一開始就添加了一個要求用戶名的部分。我的問題是每次使用這個「返回」選項轉到先前的「選項卡」時,它會重複詢問你的名字。我已經尋找了許多不同的方式來使它起作用,但我不能。我無法找到Loop/Exit的工作。我嘗試將名稱腳本分開,以便在開始時運行它,並在返回時轉到主腳本。但那意味着我不會在第二個腳本上知道他們的名字,因爲它沒有變量值。這是劇本!就像是去?

Dim Input 
Dim Input1 
Dim Input2 
Dim Input3 
Dim InputName 
InputName = Inputbox("Please Enter Your Name", "Name") 
Input = Inputbox("Weclome " & InputName & "" & vbNewLine & "Choose One Of The Options!" & vbNewLine & "" & vbNewLine & "Information" & vbNewLine & "Cool" & vbNewLine & "Hacks" & vbNewLine & "Quit", "Solus") 
If Input = "Information" Then 
    Input1 = Inputbox("Here's The Information!" & vbNewLine & "" & vbNewLine & "Info" & vbNewLine & "Speech" & vbNewLine & "Back" & vbNewLine & "Quit", "Information") 
    If Input1 = "Info" Then 
     Set WshShell = WScript.CreateObject("WScript.Shell") 
     WshShell.Run "C:\Users\Owner\Desktop\Solus\Information\Info.txt" 
     Set WshShell = WScript.CreateObject("WScript.Shell") 
     WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
    ElseIf Input1 = "Speech" Then 
     Set WshShell = WScript.CreateObject("WScript.Shell") 
     WshShell.Run "C:\Users\Owner\Desktop\Solus\Information\Speech.vbs" 
     Set WshShell = WScript.CreateObject("WScript.Shell") 
     WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
    ElseIf Input1 = "Back" Then 
     Set WshShell = WScript.CreateObject("WScript.Shell") 
     WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
    ElseIf Input1 = "Quit" Then 
     Wscript.Quit 
    Else 
     msgbox("Please Enter One Of The Options Next Time!") 
     Set WshShell = WScript.CreateObject("WScript.Shell") 
     WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
    End If 
ElseIf Input = "Cool" Then 
    Input2 = Inputbox("Here's Some Scripts!" & vbNewLine & "" & vbNewLine & "Abracadabra" & vbNewLine & "Calculator" & vbNewLine & "Matrix" & vbNewLine & "Back" & vbNewLine & "Quit", "Cool") 
    If Input2 = "Abracadabra" Then 
     Set WshShell = WScript.CreateObject("WScript.Shell") 
     WshShell.Run "C:\Users\Owner\Desktop\Solus\Cool\Abracadabra.vbs" 
    ElseIf Input2 = "Calculator" Then 
     Set WshShell = WScript.CreateObject("WScript.Shell") 
     WshShell.Run "C:\Users\Owner\Desktop\Solus\Cool\Calculator.vbs" 
    ElseIf Input2 = "Matrix" Then 
     Set WshShell = WScript.CreateObject("WScript.Shell") 
     WshShell.Run "C:\Users\Owner\Desktop\Solus\Cool\Matrix.bat" 
    ElseIf Input2 = "Back" Then 
     Set WshShell = WScript.CreateObject("WScript.Shell") 
     WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
    ElseIf Input2 = "Quit" Then 
     Wscript.Quit 
    Else 
     msgbox("Please Enter One Of The Options Next Time!") 
     Set WshShell = WScript.CreateObject("WScript.Shell") 
     WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
    End If 
ElseIf Input = "Hacks" Then 
    Input3 = Inputbox("Here's Some Hacks!" & vbNewLine & "" & vbNewLine & "Failure" & vbNewLine & "Haxs" & vbNewLine & "Rekt" & vbNewLine & "Spam" & vbNewLine & "TheOnly" & vbNewLine & "Back" & vbNewLine & "Quit", "Hacks") 
    If Input3 = "Failure" Then 
     Set WshShell = WScript.CreateObject("WScript.Shell") 
     WshShell.Run "C:\Users\Owner\Desktop\Solus\Hacks\Failure.vbs" 
     Set Wsh Shell = WScript.CreateObject("WScript.Shell") 
     WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
    ElseIf Input3 = "Hacks" Then 
     Set WshShell = WScript.CreateObject("WScript.Shell") 
     WshShell.Run "C:\Users\Owner\Desktop\Solus\Hacks\Haxs.vbs" 
     Set WshShell = WScript.CreateObject("WScript.Shell") 
     WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
    ElseIf Input3 = "Rekt" Then 
     Set WshShell = WScript.CreateObject("WScript.Shell") 
     WshShell.Run "C:\Users\Owner\Desktop\Solus\Hacks\Rekt.vbs" 
     Set WshShell = WScript.CreateObject("WScript.Shell") 
     WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
    ElseIf Input3 = "Back" Then 
     Set WshShell = WScript.CreateObject("WScript.Shell") 
     WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
    ElseIf Input3 = "Spam" Then 
     Set WshShell = WScript.CreateObject("WScript.Shell") 
     WshShell.Run "C:\Users\Owner\Desktop\Solus\Hacks\Spam.bat" 
     Set WshShell = WScript.CreateObject("WScript.Shell") 
     WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
    ElseIf Input3 = "TheOnly" Then 
     Set WshShell = WScript.CreateObject("WScript.Shell") 
     WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
     Set WshShell = WScript.CreateObject("WScript.Shell") 
     WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
    ElseIf Input3 = "Quit" Then 
     Wscript.Quit 
    Else 
     msgbox("Please Enter One Of The Options Next Time!") 
     Set WshShell = WScript.CreateObject("WScript.Shell") 
     WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
    End If 
ElseIf Input = "Quit" Then 
    Wscript.Quit 
Else 
    msgbox("Please Enter One Of The Options Next Time!") 
    Set WshShell = WScript.CreateObject("WScript.Shell") 
    WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
End If 

我如何使「返回」選項工作是通過重新運行該程序,以便它將從頭再次開始。通過在主腳本中添加「返回」,它將首先運行,並且每次返回時都會發生。我不能擁有。如果我決定將名稱分開,所以它不會運行,主腳本會丟失名稱變量,不能使用它,這將是無用的。如果我犯了一些錯誤,我很抱歉。 在此先感謝! BTH

+2

此代碼可以真正使用一些空格,縮進和註釋。這是一個很難理解代碼的牆。 – JNevill

+0

@JNevill,我已經照顧過縮進。它正在進行同行評審。 –

+0

我注意到你正在爲每個If語句創建一個新的WScript.Shell對象。如果你每次都必須這樣做,它不是條件的一部分,所以你可以將它移到頂端。你只需要一個'Shell'對象來運行你的代碼。其次,如果您需要評估那麼多條件,請考慮'Select Case'。最後但並非最不重要的是,請對「Do..While ... Loop」進行一些研究,以照顧您的編碼。不幸的是,我現在無法給出實際答案,因爲我在這裏遇到了其他問題,但如果您以後沒有收到任何答覆,我會試一試。 –

回答

1

還有就是Goto聲明可用,但它通常被認爲是因爲它把你的代碼放到麪條壞習慣使用它。或者,循環將是一個很好的方法。在收集名稱之前,您將繼續循環,直到該人在外部對話框中輸入「退出」:

Dim Input 
Dim Input1 
Dim Input2 
Dim Input3 
Dim InputName 

InputName = Inputbox("Please Enter Your Name", "Name") 
Do While Input <> "Quit" 
    Input = Inputbox("Weclome " & InputName & "" & vbNewLine & "Choose One Of The Options!" & vbNewLine & "" & vbNewLine & "Information" & vbNewLine & "Cool" & vbNewLine & "Hacks" & vbNewLine & "Quit", "Solus") 

    If Input = "Information" Then 

     Input1 = Inputbox("Here's The Information!" & vbNewLine & "" & vbNewLine & "Info" & vbNewLine & "Speech" & vbNewLine & "Back" & vbNewLine & "Quit", "Information") 

     If Input1 = "Info" Then 
      Set WshShell = WScript.CreateObject("WScript.Shell") 
      WshShell.Run "C:\Users\Owner\Desktop\Solus\Information\Info.txt" 
      Set WshShell = WScript.CreateObject("WScript.Shell") 
      WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
     ElseIf Input1 = "Speech" Then 
      Set WshShell = WScript.CreateObject("WScript.Shell") 
      WshShell.Run "C:\Users\Owner\Desktop\Solus\Information\Speech.vbs" 
      Set WshShell = WScript.CreateObject("WScript.Shell") 
      WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
     ElseIf Input1 = "Back" Then 
      'do nothing 
     ElseIf Input1 = "Quit" Then 
      Wscript.Quit 
     Else 
      msgbox("Please Enter One Of The Options Next Time!") 
      Set WshShell = WScript.CreateObject("WScript.Shell") 
      WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
     End If 

    ElseIf Input = "Cool" Then 

     Input2 = Inputbox("Here's Some Scripts!" & vbNewLine & "" & vbNewLine & "Abracadabra" & vbNewLine & "Calculator" & vbNewLine & "Matrix" & vbNewLine & "Back" & vbNewLine & "Quit", "Cool") 

     If Input2 = "Abracadabra" Then 
      Set WshShell = WScript.CreateObject("WScript.Shell") 
      WshShell.Run "C:\Users\Owner\Desktop\Solus\Cool\Abracadabra.vbs" 
     ElseIf Input2 = "Calculator" Then 
      Set WshShell = WScript.CreateObject("WScript.Shell") 
      WshShell.Run "C:\Users\Owner\Desktop\Solus\Cool\Calculator.vbs" 
     ElseIf Input2 = "Matrix" Then 
      Set WshShell = WScript.CreateObject("WScript.Shell") 
      WshShell.Run "C:\Users\Owner\Desktop\Solus\Cool\Matrix.bat" 
     ElseIf Input1 = "Back" Then 
      'do nothing 
     ElseIf Input2 = "Quit" Then 
      Wscript.Quit 
     Else 
      msgbox("Please Enter One Of The Options Next Time!") 
      Set WshShell = WScript.CreateObject("WScript.Shell") 
      WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
     End If 

    ElseIf Input = "Hacks" Then 

     Input3 = Inputbox("Here's Some Hacks!" & vbNewLine & "" & vbNewLine & "Failure" & vbNewLine & "Haxs" & vbNewLine & "Rekt" & vbNewLine & "Spam" & vbNewLine & "TheOnly" & vbNewLine & "Back" & vbNewLine & "Quit", "Hacks") 

     If Input3 = "Failure" Then 
      Set WshShell = WScript.CreateObject("WScript.Shell") 
      WshShell.Run "C:\Users\Owner\Desktop\Solus\Hacks\Failure.vbs" 
      Set WshShell = WScript.CreateObject("WScript.Shell") 
      WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
     ElseIf Input3 = "Hacks" Then 
      Set WshShell = WScript.CreateObject("WScript.Shell") 
      WshShell.Run "C:\Users\Owner\Desktop\Solus\Hacks\Haxs.vbs" 
      Set WshShell = WScript.CreateObject("WScript.Shell") 
      WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
     ElseIf Input3 = "Rekt" Then 
      Set WshShell = WScript.CreateObject("WScript.Shell") 
      WshShell.Run "C:\Users\Owner\Desktop\Solus\Hacks\Rekt.vbs" 
      Set WshShell = WScript.CreateObject("WScript.Shell") 
      WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
     ElseIf Input1 = "Back" Then 
      'do nothing 
     ElseIf Input3 = "Spam" Then 
      Set WshShell = WScript.CreateObject("WScript.Shell") 
      WshShell.Run "C:\Users\Owner\Desktop\Solus\Hacks\Spam.bat" 
      Set WshShell = WScript.CreateObject("WScript.Shell") 
      WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
     ElseIf Input3 = "TheOnly" Then 
      Set WshShell = WScript.CreateObject("WScript.Shell") 
      WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
      Set WshShell = WScript.CreateObject("WScript.Shell") 
      WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
     ElseIf Input3 = "Quit" Then 
      Wscript.Quit 
     Else 
      msgbox("Please Enter One Of The Options Next Time!") 
      Set WshShell = WScript.CreateObject("WScript.Shell") 
      WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
     End If 
    Elseif Input = "Quit" Then 
     'do nothing. While loop will exit 
    Else 
     msgbox("Please Enter One Of The Options Next Time!") 
     Set WshShell = WScript.CreateObject("WScript.Shell") 
     WshShell.Run "C:\Users\Owner\Desktop\Solus\Solus.vbs" 
    End If 
Loop 
+1

另一個好主意是將代碼分解成* functions *。使用更多的常量不會是一個瘋狂的想法,或者減少所有這些路徑的重複。如果你像鈍器一樣使用它,GoTo *可以將你的代碼轉換成意大利麪條,但它並不像它得到的說唱那樣糟糕。真的,一個循環只是GoTo的語法糖! –

+0

夫婦的事情。主要感謝您的幫助!我試着運行腳本,它有一些錯誤。當我運行它並嘗試使用「返回」時,它將其作爲未知條目和內容讀取。 Quit沒有工作,最後我用3個循環的腳本結束了。我的主要目的是讓「後退」功能跳過名稱功能。抱歉一塌糊塗。我會解決它。 – BackToHack

+0

我感謝你的全力幫助。我知道這是我的錯。我真的需要了解更多關於潛艇和功能的知識! – BackToHack