C# Tutorials - Herong's Tutorial Examples - v3.32, by Herong Yang
WPF Button Click Test
A tutorial example is provided on how to test on System.Windows.Controls.Button class with Click event handler functions.
To confirm what I learned about System.Windows.Controls classes, I wrote the following WPF button click test program:
// WpfButtonTest.cs // Copyright (c) 2016 HerongYang.com. All Rights Reserved. using System.Windows; using System.Windows.Controls; public class MyWindow : Window { private Label myLabel; public MyWindow() { Width = 200; Height = 200; Title = "WPF Button Test"; Grid myGrid = new Grid(); Content = myGrid; Button yesButton = new Button(); yesButton.Content = "Yes"; yesButton.Margin = new Thickness(50, 10, 50, 0); yesButton.Height = 30; yesButton.VerticalAlignment = System.Windows.VerticalAlignment.Top; yesButton.Click += new RoutedEventHandler(yesButton_Click); myGrid.Children.Add(yesButton); Button noButton = new Button(); noButton.Content = "No"; noButton.Margin = new Thickness(50, 50, 50, 0); noButton.Height = 30; noButton.VerticalAlignment = System.Windows.VerticalAlignment.Top; noButton.Click += new RoutedEventHandler(noButton_Click); myGrid.Children.Add(noButton); myLabel = new Label(); myLabel.Margin = new Thickness(50,90,50,0); myGrid.Children.Add(myLabel); } void yesButton_Click(object sender, RoutedEventArgs e) { myLabel.Content = "Yes clicked"; } void noButton_Click(object sender, RoutedEventArgs e) { myLabel.Content = "No clicked"; } [System.STAThread] public static void Main() { Application app = new Application(); app.Run(new MyWindow()); } }
Some interesting notes on this example program:
Now let's compile and execute WpfButtonTest.cs with .NET Framework 4.6.1 SDK.
C:\herong>%NET%\csc /reference:"%REF%\PresentationFramework.dll"; "%REF%\PresentationCore.dll";"%REF%\System.Xaml.dll"; "%REF%\WindowsBase.dll" WpfButtonTest.cs Microsoft (R) Visual C# Compiler version 4.6.1055.0 C:\herong>WpfButtonTest.exe
A new window shows up on the screen with 2 buttons: Yes and No, displayed. When you click the "Yes" button, the text "Yes clicked" will be displayed on the text label. When you click the "No" button, the text "No clicked" will be displayed on the text label.
Table of Contents
Logical Expressions and Conditional Statements
Visual C# 2010 Express Edition
C# Compiler and Intermediate Language
Compiling C# Source Code Files
MSBuild - Microsoft Build Engine
System.Diagnostics.FileVersionInfo Class
►WPF - Windows Presentation Foundation
System.Windows.Application Class
HelloWPF.cs - First WPF Application
System.Windows.Controls Namespace
Compiling WPF Applications with MSBuild