العودة إلى الحوار الأول
-
30-09-2019 - |
سؤال
لديّ winform يتم استخدامه كمربع حوار مع OK
و Cancel
أزرار. لذا:
Dim sr As New SlideRangeDialog
Dim dr As Windows.Forms.DialogResult
dr = sr.ShowDialog
لدي if/ثم لمعرفة ما إذا كان المستخدم يضغط موافق. إذا ضغطوا على موافق وكان هناك خطأ في التحقق من الصحة ، فأنا بحاجة إليهم للعودة إلى مربع الحوار وإصلاحه.
If dr = Windows.Forms.DialogResult.OK Then
Dim mr As Windows.Forms.DialogResult
mr = MsgBox("Click Yes to fix, No to not fix or Cancel to go " + vbCrLf + _
" back to the dialog to fix.", MsgBoxStyle.YesNoCancel)
Select Case mr
Case Windows.Forms.DialogResult.Yes
''# something
Case Windows.Forms.DialogResult.No
''# something more
Case Windows.Forms.DialogResult.Cancel
''# RIGHT HERE is where I'm having the problem.
''# I just want "Cancel" to return to the first dialog.
sr.DialogResult = Windows.Forms.DialogResult.None
End Select
Else
''#other thing
End If
ماذا سأضع Case Windows.Forms.DialogResult.Cancel
ليأخذني مباشرة إلى الحوار الأول sr.DialogResult = Windows.Forms.DialogResult.None
لا يبدو أنه يعمل؟
لقد حاولت رفع الحدث الفرعي مرة أخرى (إنه نقرة من عنصر قائمة) ، لكن هذا لا يعمل مع التكنولوجيا التي أستخدمها (VSTO RIBBON).
المحلول
حاول نقل منطق التحقق من الصحة إما إلى مربع الحوار نفسه ، أو إلى معالج حدث إغلاق لحوار الحوار. قد يكون الأخير أسهل. مهارات VB.NET الخاصة بي غير موجودة عمليا ، لذا سامحني إذا كان هذا خارج العلامة:
Dim sr As New SlideRangeDialog
Dim dr As Windows.Forms.DialogResult
AddHandler dr.Closing, AddressOf SlideRangeDialog_Closing
dr = sr.ShowDialog
ثم في وقت لاحق:
Public Sub SlideRangeDialog_Closing(Sender As Object, e As CancelEventArgs)
' cast Sender as a SlideRangeDialog and check its
' DialogResult property to see if they clicked OK.
' Your validation goes in here.
' If anything fails validation, set e.Cancel to True and the
' dialog won't close.
End Sub
نصائح أخرى
أعتقد أنك بحاجة إلى التعامل مع موافق (أو نعم لا إلخ) والإلغاء في النموذج نفسه وعدم إغلاقه في حالة فشل التحقق من الصحة.
في النموذج الذي يتم استخدامه كمربع حوار ، تحتاج إلى التعامل مع أحداث النقر من الأزرار. ثم قم بإجراء اختبار في معالج الأحداث لتحديد ما إذا كان ينبغي إغلاق النموذج. إذا كان الأمر كذلك ، فيمكنك ضبط Dialogresult على كل ما تحتاجه وتشغيله. close ()
نرى هنا على سبيل المثال
Private Sub OKCmd_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles OKCmd.Click
If Not ____do_your_test_here____ Then
MsgBox("Cannot press OK because of blah blah blah . Try again.", MsgBoxStyle.Exclamation)
Else
Me.DialogResult = DialogResult.OK
Me.Close()
End If
End Sub
نظرًا لأن VSTO الخاص بها وستواجه مشكلات لاستخدام الوسائل "العادية" للتواصل بين الشاشات ، فلماذا لا تحاول استخدام Pinvoke بدلاً من ذلك: بالنظر إلى معرفة كيفية الحصول على hwnds من Windows ، استخدم هذا الرمز:
[DllImport("user32.dll")]
public static extern bool SetFocus(IntPtr hwnd);
كما اقترح Preet ، من الواضح أن IMHO أكثر وضوحًا للتعامل مع بيان "Me.Close" من أحداث الأزرار.
يمكنك أيضًا القيام بذلك عن طريق إزالته من معالج أحداث الزر وإغلاق النموذج في شجرة المنطق الخاصة بك لاحقًا. في المثال لدي شكلين. يحتوي Form1 على زر لفتح Form2 وأن أحدهما يحتوي على زرين مع أزرار موافق وإلغاء. لذلك في النموذج الثاني يمكنك استخدام ما يلي:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.DialogResult = Windows.Forms.DialogResult.OK
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.DialogResult = Windows.Forms.DialogResult.Cancel
Me.Dispose()
End Sub
يجب أن تضع في اعتبارك أنه عند استخدام showdialog ، لا يتم التخلص منه أبدًا إلا إذا كنت تتصل به على وجه التحديد. نعم/لا مثال بسيط لسؤال ما إذا كان المستخدم يريد الإغلاق بالتأكيد.
If Form2.ShowDialog = Windows.Forms.DialogResult.OK Then
Select Case MessageBox.Show("Sure to close?", "Warning", MessageBoxButtons.YesNo)
Case Windows.Forms.DialogResult.Yes
Form2.Dispose()
Case Windows.Forms.DialogResult.No
Button2_Click(sender, e)
End Select
End If
يتم استدعاء button2_click sub بشكل متكرر للاستمرار في العرض طالما يتم اختيار موافق في النموذج الثاني وليس في مربع الرسائل التالي. آمل أن يساعد هذا.