背景:私はVBで新しく、javascriptから来ています。VB:イベントを手動で呼び出したり遅延させる方法は?
私の割り当ては、txtBox.LostFocusイベントを使用して検証を行うことです。問題は、ユーザが3つのボタンのうちの2つを押すことを意図している場合、検証ステップをキャンセルする必要があることです。
イラスト:私は入力ボックスのLostFocusイベントハンドラに2つのボタンのGotFocusイベントを追加しようとしましたが、イベントの発火とのバグがありました
Dim lblInputBox As Label
lblInputBox.Text = "Name:"
Dim txtInputBox As TextBox
Dim btnClear As Button
btnClear.Text = "Clear"
Dim btnSayName As Button
btnSayName.Text = "Say Name"
Dim btnExit As Button
btnExit.Text = "Exit"
' Some boolean to determine what the next action is
Dim UserIntentDetected = False
' When the user moves focus away from the textbox
Private Sub txtInputBox_LostFocus(sender As Object, e As EventArgs) _
Handles txtIputBox.LostFocus
' I want to be able to detect the next focus, but this is where I'm going wrong
If btnExit.GotFocus Or btnClear.GotFocus Then
UserIntentDetected = True
Else
UserIntentDetected = False
End If
' Only call validate if there is no intent to quit or clear
If Not UserIntentDetected Then
Call validate()
End If
' Reset the intent boolean
UserIntentDetected = False
End Sub
' Validate subroutine
Private Sub validate()
' **Fixed description**
' User moved focus away from txtbox and doesn't intend to clear or exit
Console.WriteLine("You're NOT INTENDING to clear or exit")
End Sub
コードループで複数回
例:
Private Sub txtInputBox_LostFocus(sender As Object, e As EventArgs) _
Handles txtIputBox.LostFocus, btnExit.GotFocus, _
btnClear.GotFocus
... (code follows)
これらの試みは完全に間違っているが、JavaScript背景から、また完全に間違っているが、私はこのような何かをハックでした。..
... (assume same element structure from vb)
var eventQueue = [];
var userIntentDetected = false;
txtInputBox.addEventListener("blur", function(event){
// Set a timeout to manually trigger the last event's click method in the eventQueue
setTimeout(function(){
if (eventQueue.length > 0 && userIntetDetected)
runIntendedHandler(eventQueue[eventQueue.length -1]);
}, 500);
})
// Both event listeners listen for click and stop default actions,
// set intent boolean to true, and add it's event object to a queue
btnExit.addEventListener("click", function(event){
event.preventDefault();
userIntentDetected = true;
eventQueue.push(event);
});
btn.addEventListener("click", function(event){
event.preventDefault();
userIntentDetected = true;
eventQueue.push(event);
});
// Validation should occur only if the user moves focus to an element
// that IS NOT either the btnExit or btnClear
function runIntendedHandler(event){
if (event.target.id = "btnExit")
// run exit functions
code...
else if (event.target.id = "btnClear")
// run clear functions
code..
userIntentDetected = false;
}
適切な方法は何ですかVBでイベントを処理するには、アクションをトリガする前に、キュー内の次のイベントを検出する方法はありますか?
RaiseEventステートメントのヘルプはありますか?
UPDATE 3:答えは、私が思ったよりもずっと簡単でした。どうやら、btn.Focusedプロパティを使用して、txtInputBox.LostFocusイベントハンドラ内から要素の次のフォーカスを確認できます。
UPDATE 2:正確に何が必要なのかについて多くの混乱がありました。その多くは、検証サブルーチンを記述する際の私の責任でした。私はいくつかの要素名を変更し、インストラクターによって私に与えられたすべての情報をまとめたイメージを追加しました。
UPDATE 1:@TnTinMnは、マイナーな変更で使用できる最も近い動作可能な回答を提供しています。 例:
Private LastActiveControl As Control = Me ' initialize on form creation
Protected Overrides Sub UpdateDefaultButton()
' Just added an IsNot condition to stay inline with the requirements
If (LastActiveControl Is txtNumberOfDrinks) AndAlso
((ActiveControl Is btnClear) OrElse (ActiveControl Is btnExit)) Then
Console.WriteLine("Your intent to press either btnClear or btnExit has been heard...")
' Validation happens only if the user didn't intend to click btnClear or btnExit
ElseIf (LastActiveControl Is txtNumberOfDrinks) AndAlso
((ActiveControl IsNot btnClear) OrElse (ActiveControl IsNot btnExit)) Then
Console.WriteLine("You didn't press either btnClear or btnExit.. moving to validation")
validateForm()
End If
LastActiveControl = ActiveControl ' Store this for the next time Focus changes
End Sub
ありがとうございます!
winformsの土地では、通常、検証をキャンセルしません。ユーザーが遅れないようにすばやく実行するか、ユーザーが実際に操作を試みるまで検証しないでください。意図は関係ありません。 –
私はこの任務の性質がそのようなものなのかどうかはわかりませんが、指示がはっきりしているので、これは私が掘ったウサギの穴です。 – 4UmNinja
上の宣言で、3つのボタンをまだStringとして割り当てようとしています - Option Strictをonにして、すぐにエラーを見てください。典型的にはこれらはデザイナーに組み込まれるべきです。 – OneFineDay