这是我得到的代码:
Public Class FormNew
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim writer As New StreamWriter(SaveFileDialog1.FileName)
writer.Write(TextBox1.Text)
writer.Close()
End If
End Sub
End Class
Public Class Form1
Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click
FormNew.Button1.Click()
End Sub
End Class
不起作用的行是FormNew.Button1.click
。我该如何解决?我只想按下表单1上的按钮,并希望它认为我按下FormNew上的按钮。这样我点击" Save"就可以保存我在FormNew上输入的文字。在Form1上,而不是单击formNew。
答案 0 :(得分:1)
替换
FormNew.Button1.Click()
与
FormNew.Button1.PerformClick()
然后它会起作用。
答案 1 :(得分:0)
您可以通过在模块
中创建函数来使用以下内容 public Sub Button1_Click()
If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim writer As New StreamWriter(SaveFileDialog1.FileName)
writer.Write(TextBox1.Text)
writer.Close()
End If
End Sub
然后你可以从应用程序的任何地方调用该函数。该调用将类似于以下
Public Class Form1
Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click
Button1_Click()
End Sub
End Class
in formnew
Public Class FormNew
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Button1_Click()
End Sub
End Class