2015-09-01 57 views
0

我很難解釋我正在嘗試做什麼,所以谷歌搜索我的答案不工作..裸露在我身邊。使用單個窗體來處理幾個任務

我有一個Windows窗體,我用作一個msgbox。我有10個按鈕,我想要全部轉到Shutdownbox窗體,但10個按鈕中的每個按鈕需要更改一個Command字符串。我能想到的只有10種不同的形式......但必須有更好的方式。

示例: ShutdownButton1單擊,將REMOTEPC1.Text放入字段。或者如果按鈕2,做remotePC2等。

代碼:

Public Class ShutdownBox 

Private Sub shutdown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles shutdown.Click 
    Dim command As String = "/C ""net use \\" & REMOTEPC1.Text & "\IPC$ PASSWORD /USER:DOMAIN\USERNAME& shutdown /s /f /t 01 /m \\" & REMOTEPC1.Text & "\""" 

    Dim result As Integer = MessageBox.Show("Are you sure you want to Shutdown?", "Power Down", MessageBoxButtons.YesNo) 
    If result = DialogResult.No Then 
     Exit Sub 
     Me.Close() 
    ElseIf result = DialogResult.Yes Then 
     Process.Start("cmd", command) 
     Me.Close() 
    End If 
End Sub 
End Class 

再次,抱歉,它很難解釋。

編輯: 圖片,試圖幫助 enter image description here

+0

讓按鈕的單擊事件處理程序設置變量。不太可能你應該只用按鈕btw,你必須提供反饋給用戶,當你有很多人。除非他們是RadioButton。拋出一些標籤,它在代碼中也變得簡單。 –

+0

@HansPassant你能舉個例子說明它如何爲它設置一個變量嗎? – dwb

+0

當我點擊其中一個關機按鈕它會執行ShutdownBox.show() – dwb

回答

1

第一種形式......

Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click 
    If TextBox1.Text = "" Then 
     TextBox1.BackColor = Color.Tomato 
    Else 
     TextBox1.BackColor = Color.White 
     Form2.Text = TextBox1.Text 
     Form2.Show() 
    End If 
End Sub 

Private Sub PictureBox6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox6.Click 
    If TextBox2.Text = "" Then 
     TextBox2.BackColor = Color.Tomato 
    Else 
     TextBox2.BackColor = Color.White 
     Form2.Text = TextBox2.Text 
     Form2.Show() 
    End If 
End Sub 

Private Sub PictureBox9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox9.Click 
    If TextBox3.Text = "" Then 
     TextBox3.BackColor = Color.Tomato 
    Else 
     TextBox3.BackColor = Color.White 
     Form2.Text = TextBox3.Text 
     Form2.Show() 
    End If 
End Sub 

Private Sub PictureBox12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox12.Click 
    If TextBox4.Text = "" Then 
     TextBox4.BackColor = Color.Tomato 
    Else 
     TextBox4.BackColor = Color.White 
     Form2.Text = TextBox4.Text 
     Form2.Show() 
    End If 
End Sub 

Private Sub PictureBox15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox15.Click 
    If TextBox5.Text = "" Then 
     TextBox5.BackColor = Color.Tomato 
    Else 
     TextBox5.BackColor = Color.White 
     Form2.Text = TextBox5.Text 
     Form2.Show() 
    End If 
End Sub 

Private Sub PictureBox18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox18.Click 
    If TextBox6.Text = "" Then 
     TextBox6.BackColor = Color.Tomato 
    Else 
     TextBox6.BackColor = Color.White 
     Form2.Text = TextBox6.Text 
     Form2.Show() 
    End If 
End Sub 

添加第二種形式...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    Process.Start("cmd", "/C ""net use \\" & Me.Text & "\IPC$ PASSWORD /USER:DOMAIN\USERNAME& shutdown /r /f /t 01 /m \\" & Me.Text & "\""") 
    Me.Close() 
End Sub 

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
    Process.Start("cmd", "/C ""net use \\" & Me.Text & "\IPC$ PASSWORD /USER:DOMAIN\USERNAME& shutdown /s /f /t 01 /m \\" & Me.Text & "\""") 
    Me.Close() 
End Sub 

這個時候沒有必要保存字符串在應用的設置中...

+0

啊,我看到你是怎麼做到的,它非常簡單!Form2.text = Textbox.text。 – dwb

+1

這將改變表格2的標題...如果你不希望它改變,你可以使用表格2中的標籤並修改該表格而不是表格標題...並且可以隱藏標籤... – Chris

3

我個人會用My.Settings ..具有所需的所有命令發送和Click事件處理程序叫他們......我趕緊寫了這個,我希望它有幫助。

enter image description here

enter image description here

Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click 
    If TextBox1.Text = "" Then 
     TextBox1.BackColor = Color.Tomato 
    Else 
     TextBox1.BackColor = Color.White 
     Dim result As Integer = MessageBox.Show("Are you sure you want to Shutdown?", "Power Down", MessageBoxButtons.YesNo) 
     If result = DialogResult.No Then 
      Exit Sub 
      Me.Close() 
     ElseIf result = DialogResult.Yes Then 
      Process.Start("cmd", My.Settings.command1) 
      Me.Close() 
     End If 
    End If 
End Sub 

Private Sub PictureBox6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox6.Click 
    If TextBox2.Text = "" Then 
     TextBox2.BackColor = Color.Tomato 
    Else 
     TextBox2.BackColor = Color.White 
     Dim result As Integer = MessageBox.Show("Are you sure you want to Shutdown?", "Power Down", MessageBoxButtons.YesNo) 
     If result = DialogResult.No Then 
      Exit Sub 
      Me.Close() 
     ElseIf result = DialogResult.Yes Then 
      Process.Start("cmd", My.Settings.command2) 
      Me.Close() 
     End If 
    End If 
End Sub 

Private Sub PictureBox9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox9.Click 
    If TextBox3.Text = "" Then 
     TextBox3.BackColor = Color.Tomato 
    Else 
     TextBox3.BackColor = Color.White 
     Dim result As Integer = MessageBox.Show("Are you sure you want to Shutdown?", "Power Down", MessageBoxButtons.YesNo) 
     If result = DialogResult.No Then 
      Exit Sub 
      Me.Close() 
     ElseIf result = DialogResult.Yes Then 
      Process.Start("cmd", My.Settings.command3) 
      Me.Close() 
     End If 
    End If 
End Sub 

Private Sub PictureBox12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox12.Click 
    If TextBox4.Text = "" Then 
     TextBox4.BackColor = Color.Tomato 
    Else 
     TextBox4.BackColor = Color.White 
     Dim result As Integer = MessageBox.Show("Are you sure you want to Shutdown?", "Power Down", MessageBoxButtons.YesNo) 
     If result = DialogResult.No Then 
      Exit Sub 
      Me.Close() 
     ElseIf result = DialogResult.Yes Then 
      Process.Start("cmd", My.Settings.command4) 
      Me.Close() 
     End If 
    End If 
End Sub 

Private Sub PictureBox15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox15.Click 
    If TextBox5.Text = "" Then 
     TextBox5.BackColor = Color.Tomato 
    Else 
     TextBox5.BackColor = Color.White 
     Dim result As Integer = MessageBox.Show("Are you sure you want to Shutdown?", "Power Down", MessageBoxButtons.YesNo) 
     If result = DialogResult.No Then 
      Exit Sub 
      Me.Close() 
     ElseIf result = DialogResult.Yes Then 
      Process.Start("cmd", My.Settings.command5) 
      Me.Close() 
     End If 
    End If 
End Sub 

Private Sub PictureBox18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox18.Click 
    If TextBox6.Text = "" Then 
     TextBox6.BackColor = Color.Tomato 
    Else 
     TextBox6.BackColor = Color.White 
     Dim result As Integer = MessageBox.Show("Are you sure you want to Shutdown?", "Power Down", MessageBoxButtons.YesNo) 
     If result = DialogResult.No Then 
      Exit Sub 
      Me.Close() 
     ElseIf result = DialogResult.Yes Then 
      Process.Start("cmd", My.Settings.command6) 
      Me.Close() 
     End If 
    End If 
End Sub 
+0

感謝您的重播,這幾乎正是我需要的,但我希望重新啓動的選項。任何想法? – dwb

+0

我會讓那個處理所有六個PB的點擊事件的子,然後根據發件人查看命令。 – clweeks

相關問題