我很难解释我想要做什么,因此谷歌搜索我的答案是行不通的。
我有一个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
再一次,抱歉,很难解释。
答案 0 :(得分:3)
我个人会使用My.Settings ..需要发送所有命令并从click事件处理程序中调用它们...我很快写了这个,我希望它有所帮助。
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
答案 1 :(得分: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
这次不需要在应用程序的设置中保存字符串......