Content update - Handle events in Visual Basic (user story 1878475) (#1320)

* Add article, toc, and redirects

* Add code snippets

* Fix a note in a prior event article

* Add tip and fix lnk

* Simplify snippet
This commit is contained in:
Tris Shores
2022-03-04 10:35:13 -08:00
committed by GitHub
parent a606313314
commit d9125bf6ea
11 changed files with 205 additions and 1 deletions
@@ -0,0 +1,9 @@
<Application x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CodeSampleVb"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
@@ -0,0 +1,6 @@
Class Application
' Application-level events, such as Startup, Exit, and DispatcherUnhandledException
' can be handled in this file.
End Class
@@ -0,0 +1,11 @@
Imports System.Windows
'The ThemeInfo attribute describes where any theme specific and generic resource dictionaries can be found.
'1st parameter: where theme specific resource dictionaries are located
'(used if a resource is not found in the page,
' or application resource dictionaries)
'2nd parameter: where the generic resource dictionary is located
'(used if a resource is not found in the page,
'app, and any theme specific resource dictionaries)
<Assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)>
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<RootNamespace>CodeSampleVb</RootNamespace>
<NeutralLanguage>en-us</NeutralLanguage>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<Import Include="System.Windows" />
<Import Include="System.Windows.Controls" />
<Import Include="System.Windows.Data" />
<Import Include="System.Windows.Documents" />
<Import Include="System.Windows.Input" />
<Import Include="System.Windows.Media" />
<Import Include="System.Windows.Media.Imaging" />
<Import Include="System.Windows.Navigation" />
<Import Include="System.Windows.Shapes" />
</ItemGroup>
</Project>
@@ -0,0 +1,15 @@
<Window x:Class="CodeSampleVb.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Visual Basic and WPF event handling" Height="100" Width="400">
<!--<ButtonCreatedByXaml>-->
<StackPanel Name="StackPanel1">
<Button
Name="XamlButton"
Content="Create a new button with an event handler"
Background="LightGray">
</Button>
</StackPanel>
<!--</ButtonCreatedByXaml>-->
</Window>
@@ -0,0 +1,61 @@
Imports System.Windows.Controls.Primitives
Namespace CodeSampleVb
' <summary>
' Interaction logic for MainWindow.xaml.
' </summary>
Partial Public Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
' Add the new button to the StackPanel.
StackPanel1.Children.Add(CodeButton)
' Remove a handler.
' RemoveHandler XamlButton.Click, AddressOf XamlButton_Click
End Sub
'<Window_Loaded>
' Loaded event handler attached to the XAML page root using Handles.
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
' Handler logic.
Debug.WriteLine($"Loaded event handler attached to Window using Handles.")
End Sub
'</Window_Loaded>
'<CodeButton_Click>
' Declare a new button using WithEvents.
Dim WithEvents CodeButton As New Button With {
.Content = "New button",
.Background = Brushes.Yellow
}
' Click event handler attached to CodeButton using Handles.
Private Sub CodeButton_Click(sender As Object, e As RoutedEventArgs) Handles CodeButton.Click
' Handler logic.
Debug.WriteLine($"Click event handler attached to CodeButton using Handles.")
End Sub
'</CodeButton_Click>
'<XamlButton_Click>
' Click event handler attached to XamlButton using Handles.
Private Sub XamlButton_Click(sender As Object, e As RoutedEventArgs) Handles XamlButton.Click
' Handler logic.
Debug.WriteLine($"Click event handler attached to XamlButton using Handles.")
End Sub
'</XamlButton_Click>
End Class
End Namespace