From c953bfc5bd1eaffbb090cffa9dd51f79262beff2 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 15 Sep 2025 20:42:41 +0000 Subject: [PATCH 1/6] Initial plan From 36307a14a94fbc94dc4c23925f64a55ee16f3046 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 15 Sep 2025 20:49:52 +0000 Subject: [PATCH 2/6] Replace problematic DoEvents example with console-based timer example Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> --- .../statements/raiseevent-statement.md | 15 ++----- .../VbVbalrEvents/VB/Class1.vb | 39 +++++++------------ 2 files changed, 19 insertions(+), 35 deletions(-) diff --git a/docs/visual-basic/language-reference/statements/raiseevent-statement.md b/docs/visual-basic/language-reference/statements/raiseevent-statement.md index e385743bd2e2e..4164b14dd0ad5 100644 --- a/docs/visual-basic/language-reference/statements/raiseevent-statement.md +++ b/docs/visual-basic/language-reference/statements/raiseevent-statement.md @@ -54,26 +54,19 @@ RaiseEvent eventname[( argumentlist )] The class that raises an event is the event source, and the methods that process the event are the event handlers. An event source can have multiple handlers for the events it generates. When the class raises the event, that event is raised on every class that has elected to handle events for that instance of the object. - The example also uses a form (`Form1`) with a button (`Button1`) and a text box (`TextBox1`). When you click the button, the first text box displays a countdown from 10 to 0 seconds. When the full time (10 seconds) has elapsed, the first text box displays "Done". + The example demonstrates a simple timer that counts down from 10 to 0 seconds and displays the progress to the console. When the countdown finishes, it displays "Done". - The code for `Form1` specifies the initial and terminal states of the form. It also contains the code executed when events are raised. - - To use this example, open a new Windows Application project, add a button named `Button1` and a text box named `TextBox1` to the main form, named `Form1`. Then right-click the form and click **View Code** to open the Code Editor. - - Add a `WithEvents` variable to the declarations section of the `Form1` class. + Add a `WithEvents` variable to your class declarations. [!code-vb[VbVbalrEvents#14](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb#14)] ## Example 2 - Add the following code to the code for `Form1`. Replace any duplicate procedures that may exist, such as `Form_Load`, or `Button_Click`. + Add the following code to implement the event handlers and timer logic. This example shows how to use the `RaiseEvent` statement to notify event handlers when the timer updates or completes. [!code-vb[VbVbalrEvents#15](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb#15)] - Press F5 to run the preceding example, and click the button labeled **Start**. The first text box starts to count down the seconds. When the full time (10 seconds) has elapsed, the first text box displays "Done". - -> [!NOTE] -> The `My.Application.DoEvents` method does not process events in exactly the same way as the form does. To allow the form to handle the events directly, you can use multithreading. For more information, see [Managed Threading](../../../standard/threading/managed-threading-basics.md). + When you run the preceding example, it starts counting down the seconds from 10 to 0, displaying the progress to the console. When the full time (10 seconds) has elapsed, it displays "Done". ## See also diff --git a/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb b/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb index 1100b9e5787df..360ea95d984e8 100644 --- a/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb +++ b/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb @@ -163,28 +163,24 @@ Class Class306ff8ed74dd4b6abd2fe91b17474042 Private WithEvents TextBox1 As New TextBox ' - Private WithEvents mText As TimerState + Private WithEvents mTimer As TimerState ' ' - Private Sub Form1_Load() Handles MyBase.Load - Button1.Text = "Start" - mText = New TimerState - End Sub - Private Sub Button1_Click() Handles Button1.Click - mText.StartCountdown(10.0, 0.1) - End Sub + Private WithEvents mTimer As TimerState - Private Sub mText_ChangeText() Handles mText.Finished - TextBox1.Text = "Done" + Private Sub StartCountdownExample() + Console.WriteLine("Starting countdown from 10 seconds...") + mTimer = New TimerState() + mTimer.StartCountdown(10.0, 1.0) End Sub - Private Sub mText_UpdateTime(ByVal Countdown As Double - ) Handles mText.UpdateTime + Private Sub mTimer_UpdateTime(ByVal Countdown As Double) Handles mTimer.UpdateTime + Console.WriteLine($"Time remaining: {Countdown:0.0} seconds") + End Sub - TextBox1.Text = Format(Countdown, "##0.0") - ' Use DoEvents to allow the display to refresh. - My.Application.DoEvents() + Private Sub mTimer_Finished() Handles mTimer.Finished + Console.WriteLine("Done") End Sub Class TimerState @@ -192,16 +188,11 @@ Class Class306ff8ed74dd4b6abd2fe91b17474042 Public Event Finished() Public Sub StartCountdown(ByVal Duration As Double, ByVal Increment As Double) - Dim Start As Double = DateAndTime.Timer - Dim ElapsedTime As Double = 0 - Dim SoFar As Double = 0 - Do While ElapsedTime < Duration - If ElapsedTime > SoFar + Increment Then - SoFar += Increment - RaiseEvent UpdateTime(Duration - SoFar) - End If - ElapsedTime = DateAndTime.Timer - Start + Do While SoFar < Duration + System.Threading.Thread.Sleep(CInt(Increment * 1000)) ' Sleep for increment seconds + SoFar += Increment + RaiseEvent UpdateTime(Duration - SoFar) Loop RaiseEvent Finished() End Sub From 7ed91932249dd8d2d20e6a5b45e776f9f3984e40 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 16 Sep 2025 13:31:47 +0000 Subject: [PATCH 3/6] Fix VB.NET syntax errors in RaiseEvent example - remove C# string interpolation, move to standalone classes, use proper VB syntax Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> --- .../statements/raiseevent-statement.md | 2 +- .../VbVbalrEvents/VB/Class1.vb | 51 +++++++++---------- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/docs/visual-basic/language-reference/statements/raiseevent-statement.md b/docs/visual-basic/language-reference/statements/raiseevent-statement.md index 4164b14dd0ad5..f973202d413be 100644 --- a/docs/visual-basic/language-reference/statements/raiseevent-statement.md +++ b/docs/visual-basic/language-reference/statements/raiseevent-statement.md @@ -56,7 +56,7 @@ RaiseEvent eventname[( argumentlist )] The example demonstrates a simple timer that counts down from 10 to 0 seconds and displays the progress to the console. When the countdown finishes, it displays "Done". - Add a `WithEvents` variable to your class declarations. + Declare a `WithEvents` variable in your class to handle events from the timer: [!code-vb[VbVbalrEvents#14](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb#14)] diff --git a/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb b/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb index 360ea95d984e8..3b30377dfd090 100644 --- a/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb +++ b/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb @@ -157,47 +157,46 @@ Class Class306ff8ed74dd4b6abd2fe91b17474042 End Class ' - Class Form1 - Inherits Form - Private WithEvents Button1 As New Button - Private WithEvents TextBox1 As New TextBox - - ' + ' + Public Class TimerExample Private WithEvents mTimer As TimerState ' ' - Private WithEvents mTimer As TimerState - - Private Sub StartCountdownExample() - Console.WriteLine("Starting countdown from 10 seconds...") + Public Sub StartCountdownExample() mTimer = New TimerState() mTimer.StartCountdown(10.0, 1.0) End Sub Private Sub mTimer_UpdateTime(ByVal Countdown As Double) Handles mTimer.UpdateTime - Console.WriteLine($"Time remaining: {Countdown:0.0} seconds") + Console.WriteLine("Time remaining: " & Format(Countdown, "##0.0") & " seconds") End Sub Private Sub mTimer_Finished() Handles mTimer.Finished Console.WriteLine("Done") End Sub + End Class - Class TimerState - Public Event UpdateTime(ByVal Countdown As Double) - Public Event Finished() - Public Sub StartCountdown(ByVal Duration As Double, - ByVal Increment As Double) - Dim SoFar As Double = 0 - Do While SoFar < Duration - System.Threading.Thread.Sleep(CInt(Increment * 1000)) ' Sleep for increment seconds - SoFar += Increment - RaiseEvent UpdateTime(Duration - SoFar) - Loop - RaiseEvent Finished() - End Sub - End Class - ' + Public Class TimerState + Public Event UpdateTime(ByVal Countdown As Double) + Public Event Finished() + Public Sub StartCountdown(ByVal Duration As Double, + ByVal Increment As Double) + Dim SoFar As Double = 0 + Do While SoFar < Duration + System.Threading.Thread.Sleep(CInt(Increment * 1000)) + SoFar += Increment + RaiseEvent UpdateTime(Duration - SoFar) + Loop + RaiseEvent Finished() + End Sub + End Class + ' + + Class Form1 + Inherits Form + Private WithEvents Button1 As New Button + Private WithEvents TextBox1 As New TextBox End Class End Class From 23d1f28886588f823e39d34265a5c111558f912a Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 16 Sep 2025 10:05:25 -0400 Subject: [PATCH 4/6] Update docs/visual-basic/language-reference/statements/raiseevent-statement.md --- .../language-reference/statements/raiseevent-statement.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/visual-basic/language-reference/statements/raiseevent-statement.md b/docs/visual-basic/language-reference/statements/raiseevent-statement.md index f973202d413be..a84f9af896120 100644 --- a/docs/visual-basic/language-reference/statements/raiseevent-statement.md +++ b/docs/visual-basic/language-reference/statements/raiseevent-statement.md @@ -54,7 +54,7 @@ RaiseEvent eventname[( argumentlist )] The class that raises an event is the event source, and the methods that process the event are the event handlers. An event source can have multiple handlers for the events it generates. When the class raises the event, that event is raised on every class that has elected to handle events for that instance of the object. - The example demonstrates a simple timer that counts down from 10 to 0 seconds and displays the progress to the console. When the countdown finishes, it displays "Done". + The example demonstrates a timer that counts down from 10 to 0 seconds and displays the progress to the console. When the countdown finishes, it displays "Done". Declare a `WithEvents` variable in your class to handle events from the timer: From 1e673a5d7d4c52483fde7975ea33f80b6ddf5c9f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 16 Sep 2025 14:10:12 +0000 Subject: [PATCH 5/6] Add link to RaiseEvent statement in example description Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> --- .../language-reference/statements/raiseevent-statement.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/visual-basic/language-reference/statements/raiseevent-statement.md b/docs/visual-basic/language-reference/statements/raiseevent-statement.md index a84f9af896120..6b7f2efee12e9 100644 --- a/docs/visual-basic/language-reference/statements/raiseevent-statement.md +++ b/docs/visual-basic/language-reference/statements/raiseevent-statement.md @@ -62,7 +62,7 @@ RaiseEvent eventname[( argumentlist )] ## Example 2 - Add the following code to implement the event handlers and timer logic. This example shows how to use the `RaiseEvent` statement to notify event handlers when the timer updates or completes. + Add the following code to implement the event handlers and timer logic. This example shows how to use the [RaiseEvent](raiseevent-statement.md) statement to notify event handlers when the timer updates or completes. [!code-vb[VbVbalrEvents#15](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb#15)] From 1e37ec41f97c750892313f5fd438ce4bd370a38d Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 16 Sep 2025 10:12:51 -0400 Subject: [PATCH 6/6] Apply suggestions from code review --- .../language-reference/statements/raiseevent-statement.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/visual-basic/language-reference/statements/raiseevent-statement.md b/docs/visual-basic/language-reference/statements/raiseevent-statement.md index 6b7f2efee12e9..a84f9af896120 100644 --- a/docs/visual-basic/language-reference/statements/raiseevent-statement.md +++ b/docs/visual-basic/language-reference/statements/raiseevent-statement.md @@ -62,7 +62,7 @@ RaiseEvent eventname[( argumentlist )] ## Example 2 - Add the following code to implement the event handlers and timer logic. This example shows how to use the [RaiseEvent](raiseevent-statement.md) statement to notify event handlers when the timer updates or completes. + Add the following code to implement the event handlers and timer logic. This example shows how to use the `RaiseEvent` statement to notify event handlers when the timer updates or completes. [!code-vb[VbVbalrEvents#15](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb#15)]