消息框关闭表格? [英] message box closes form?

查看:73
本文介绍了消息框关闭表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是vb的新手,正在尝试错误捕获.我有一个基本上执行以下操作的代码示例:

*已经打开了对话框对话框,允许用户进行选择和设置参数*

Hi,

I''m new to vb and trying out error trapping. I have a sample of code that does basically the following:

*already have open dialog form up to allow user to make selections and set parameters*

Private Sub btnExecute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExecute.Click
        Dim queryExecuted As Boolean = False
        Dim dtbresults As New DataTable
        Try
            'try to run a query and if successful then save results to datatable and queryExecuted = True
        Catch ex As Exception
                MessageBox.Show("Query failed")
                queryExecuted = False
        End Try
       If queryExecuted Then
            'do some stuff including close this dialog form
       Else
            'want this dialog box to remain open with same selections shown
       End If
End Sub



每当我的消息框出现时,我的表单就会关闭.我可以做的唯一的另一件事就是只是创建一个新表单来显示查询是否失败,但是然后我丢失了选择.我做错了什么?

感谢您的时间.



Everytime my message box comes up, my form closes. The only other thing I can make work is to just create a new form to show if the query failed, but then I lose my selections. What am I doing wrong?

Thanks for your time.

推荐答案

尝试一下,它可以简化事情.将您的进程放在Try and Catch之间将运行无错误的进程.如果有错误,catch部分将停止它并相应地执行.

Try this and it should simplify things. Placing your process between the Try and Catch will run the error free process. If there is an error, the catch section will stop it and perform accordingly.

Private Sub btnExecute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExecute.Click
    Try
        Dim dtbresults As New DataTable
        ' do your stuff here
    Catch ex As Exception
        MessageBox.Show("Query failed")
        Exit Sub
    End Try
End Sub


在您调用另一个对话框,尝试在第一个对话框中设置DialogResult = DialogResult.None,我遇到了类似的问题,该问题已为我解决.
After you call the other dialog, try setting DialogResult = DialogResult.None in your first dialog, I had a similar problem and that fixed it for me.


这篇关于消息框关闭表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆