请帮助我在vb.net中实现这一目标。
答案 0 :(得分:0)
一个基本的想法是检查文本框选择长度是否等于文本框中文本框的文本长度,然后有一个布尔变量,如果没有设置阻止表单关闭,就像这样。
Public Class MyDialog
Dim closeValid As Boolean
Private Sub ExitButton_Click(sender As Object, e As EventArgs) Handles btnOk.Click
If TextBox1.SelectionLength = TextBox1.Text.Length Then
closeValid = True
Close()
End If
End Sub
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
closeValid = True
Close()
End Sub
Private Sub MyDialog_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If Not closeValid Then e.Cancel = True
End Sub
End Class
实际上,更好的方法是拦截Dialogs FormClosing事件中的结束原因并根据UserClosing关闭原因对其进行测试,这样如果应用程序关闭或系统关闭您的应用程序将不会挂起。然后,您可以向DialogResult提供适当的CloseReason,并在您的表单从ShowDialog调用返回时进行测试。
<强> Form1中强>
Public Class Form1
Private Sub Button_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Dim form2 As New MyDialog
Dim dr As DialogResult = form2.ShowDialog()
If dr = Windows.Forms.DialogResult.OK Then
'Process your OK
ElseIf dr = Windows.Forms.DialogResult.Cancel Then
'Process your Cancel
Else
'Do Something
End If
End Sub
End Class
<强>窗体2 强>
Public Class MyDialog
Private Sub MyDialog_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If e.CloseReason = CloseReason.UserClosing Then e.Cancel = True 'This is true if user clicks the X
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
DialogResult = Windows.Forms.DialogResult.Cancel
End Sub
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
If TextBox1.SelectionLength = TextBox1.Text.Length Then
DialogResult = Windows.Forms.DialogResult.OK
End If
End Sub
End Class
答案 1 :(得分:0)
您应该使用ShowDialog打开第二个表单。在第二个表单上,单击按钮上的DialogResult。像这样:
Dim frm2 = new Form2()
If frm2.ShowDialog()==DialogResult.OK Then
'do something because the user clicked OK
End If
答案 2 :(得分:0)
1)创建一个Global函数(最好在模块中,这样你只需要声明一次)
Imports System.Runtime.InteropServices ' required imports
Public intInputBoxCancel as integer ' public variable
Public Function StrPtr(ByVal obj As Object) As Integer
Dim Handle As GCHandle = GCHandle.Alloc(obj, GCHandleType.Pinned)
StrPtr = Handle.AddrOfPinnedObject.ToInt32
Handle.Free()
End Function
在表单load事件中放入此(使变量intInputBoxCancel = cancel事件)
intInputBoxCancel = StrPtr(String.Empty)
现在,您可以在表单中的任何位置使用(如果StrPtr在模块中声明为全局,则可以使用项目)
dim ans as string = inputbox("prompt") ' default data up to you
if StrPtr(ans) = intInputBoxCancel then
' cancel was clicked
else
' ok was clicked (blank input box will still be shown here)
endif