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) 2015 by Dr. Herong Yang, herongyang.com

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 = 
      yesButton.Click += new RoutedEventHandler(yesButton_Click);

      Button noButton = new Button();
      noButton.Content = "No";
      noButton.Margin = new Thickness(50, 50, 50, 0);
      noButton.Height = 30;
      noButton.VerticalAlignment = 
      noButton.Click += new RoutedEventHandler(noButton_Click);
      myLabel = new Label();
      myLabel.Margin = new Thickness(50,90,50,0);

   void yesButton_Click(object sender, RoutedEventArgs e) {
      myLabel.Content = "Yes clicked";
   void noButton_Click(object sender, RoutedEventArgs e) {
      myLabel.Content = "No clicked";

   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%\WindowsBase.dll" WpfButtonTest.cs
Microsoft (R) Visual C# Compiler version 4.6.1055.0


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.

WPF Button Click Event Test
WPF Button Click Event Test

Last update: 2015.

Table of Contents

 About This Book

 Introduction of C# (C Sharp)

 Data Type and Variables

 Logical Expressions and Conditional Statements

 Arrays and Loop Statements

 Data Type Features

 Floating-Point Data Types

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

 Class Features

 C# Compiler and Intermediate Language

 Compiling C# Source Code Files

 MSBuild - Microsoft Build Engine

 Memory Usages of Processes

 Multithreading in C#

 Async Feature from C# 5

 System.IO.FileInfo Class

 System.Diagnostics.FileVersionInfo Class

WPF - Windows Presentation Foundation

 What Is WPF?

 System.Windows.Application Class

 System.Windows.Window Class

 HelloWPF.cs - First WPF Application

 System.Windows.Controls Namespace

WPF Button Click Test

 Compiling WPF Applications with MSBuild

 What Is XAML?

 Partial Classes and Partial Methods

 Outdated Tutorials


 PDF Printing Version