问题描述
问候大家,
我想对正在使用的VB.NET(3.5)应用程序提供一些帮助.
I would like a little help with a VB.NET(3.5) application i am working on.
主要思想是,我在Load_Form事件中运行一个测试,如果测试失败,我将Dialogresult定义为Abort,从那时起,我希望不执行下一个代码,并且控件必须返回到调用表单.
The main idea is that i am runnign a test in the Load_Form event and if the test fails i define Dialogresult to Abort, from that moment i expect the next code NOT to be executed and the control must return to the calling form.
但是代码将继续执行直到完成,然后窗体消失并且控件返回.
However the code continues until it finishes and than the form disappears and control returns.
我很困惑,这是正确的行为吗?
I am so confused is it the right behaviour or not?
Private Sub editPosteForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Using conn As New SqlConnection
Dim tran As SqlTransaction = Nothing
Try
conn.ConnectionString = Tools.GetMyTools.getConnString()
conn.Open()
tran = conn.BeginTransaction(IsolationLevel.ReadCommitted)
postesR = comptesDB.MyDataAccess.getPostesActifs(conn, tran)
tran.Commit()
Catch ex As Exception
Dim msgb As New StringBuilder
postesR = Nothing
msgb.AppendLine("Erreur lors de la recherche des postes de rattachments de la base de données")
msgb.AppendLine(ex.Message)
Try
tran.Rollback()
Catch exRollBack As Exception
If Not tran Is Nothing Then
msgb.AppendLine("Transacation n'a pas pu etre annulé, veuillez verifiez la consistence de la base de données")
msgb.AppendLine(exRollBack.Message)
End If
End Try
MsgBox(msgb.ToString())
End Try
End Using
If postesR Is Nothing Then
Me.DialogResult = Windows.Forms.DialogResult.Abort 'this is where the code should stop
Else
Try
cbposteR.DataSource = postesR
cbposteR.DisplayMember = "desPst"
cbposteR.ValueMember = "codePst"
Catch ex As Exception
MsgBox("Erreur lors de la liaison de la liste des postes de rattachements :" + ex.Message)
DialogResult = Windows.Forms.DialogResult.Abort
End Try
End If
'但是此代码仍会执行
'However this code still executes
使用conn作为新的SqlConnection Dim tran As SqlTransaction =无 尝试 conn.ConnectionString = Tools.GetMyTools.getConnString() conn.Open() tran = conn.BeginTransaction(IsolationLevel.ReadCommitted) postetoedit = comptesDB.MyDataAccess.getPosteByCode(codePst,conn,tran) tran.Commit() 异常捕获 Dim msgb作为新的StringBuilder postetoedit =没什么 msgb.AppendLine(修正后的修饰符") msgb.AppendLine(ex.Message) 尝试 tran.Rollback() 异常捕获exRollBack 如果不是tran没什么,那么 msgb.AppendLine("Transacation n'a pas pu etreannulé,véillezverifiez laconsistency de la base dedonnées") msgb.AppendLine(exRollBack.Message) 万一 结束尝试 MsgBox(msgb.ToString()) 结束尝试 最终使用 如果postetoedit什么都没有,那么 DialogResult = Windows.Forms.DialogResult.Abort 别的 如果postetoedit.Rows.Count <= 0,则 MsgBox(张贴修饰语") DialogResult = Windows.Forms.DialogResult.Abort 别的 尝试 lbCode.Text = postetoedit(0)("codePst").ToString tbDCreation.Text = postetoedit(0)("dateCreationPst") cbposteR.SelectedValue = postetoedit(0)("codePstRatt") tbDes.Text = postetoedit(0)("desPst") tbTel.Text = postetoedit(0)("telPst") tbFax.Text = postetoedit(0)("faxPst") tbEmail.Text = postetoedit(0)("emailPst") tbAdrs.Text = postetoedit(0)("adrsPst") tbComment.Text = postetoedit(0)("commentPst") 异常捕获 MsgBox(张贴修饰语的Erreur lors de l'affichage" +例如消息) DialogResult = Windows.Forms.DialogResult.Abort 结束尝试 万一 万一 结束子结束班级
Using conn As New SqlConnection Dim tran As SqlTransaction = Nothing Try conn.ConnectionString = Tools.GetMyTools.getConnString() conn.Open() tran = conn.BeginTransaction(IsolationLevel.ReadCommitted) postetoedit = comptesDB.MyDataAccess.getPosteByCode(codePst, conn, tran) tran.Commit() Catch ex As Exception Dim msgb As New StringBuilder postetoedit = Nothing msgb.AppendLine("Erreur lors de la recherche du poste a modifier de la base de données") msgb.AppendLine(ex.Message) Try tran.Rollback() Catch exRollBack As Exception If Not tran Is Nothing Then msgb.AppendLine("Transacation n'a pas pu etre annulé, veuillez verifiez la consistence de la base de données") msgb.AppendLine(exRollBack.Message) End If End Try MsgBox(msgb.ToString()) End Try End Using If postetoedit Is Nothing Then DialogResult = Windows.Forms.DialogResult.Abort Else If postetoedit.Rows.Count <= 0 Then MsgBox("Poste a modifier non trouvé") DialogResult = Windows.Forms.DialogResult.Abort Else Try lbCode.Text = postetoedit(0)("codePst").ToString tbDCreation.Text = postetoedit(0)("dateCreationPst") cbposteR.SelectedValue = postetoedit(0)("codePstRatt") tbDes.Text = postetoedit(0)("desPst") tbTel.Text = postetoedit(0)("telPst") tbFax.Text = postetoedit(0)("faxPst") tbEmail.Text = postetoedit(0)("emailPst") tbAdrs.Text = postetoedit(0)("adrsPst") tbComment.Text = postetoedit(0)("commentPst") Catch ex As Exception MsgBox("Erreur lors de l'affichage du poste a modifier" + ex.Message) DialogResult = Windows.Forms.DialogResult.Abort End Try End If End If End SubEnd Class
推荐答案
您需要在设置DialogResult之后直接放置Close方法,然后调用者检查DialogResult.
You need to place the Close method directly after setting DialogResult then the caller checks for DialogResult.
这篇关于设置Dialogresult不会将控件返回到父窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!