Monday, November 15, 2010

WPF Form Events

The xaml code
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<RadioButton Height="16" Margin="26,32,132,0" Name="radioButton1" VerticalAlignment="Top" Checked="radioButton1_Checked">Red</RadioButton>
<RadioButton Height="16" Margin="26,55,132,0" Name="radioButton2" VerticalAlignment="Top" Checked="radioButton2_Checked">Green</RadioButton>
<RadioButton Height="16" Margin="30,81,128,0" Name="radioButton3" VerticalAlignment="Top" Checked="radioButton3_Checked">Blue</RadioButton>
<StackPanel Margin="0,18,17,0" Name="stackPanel1" Height="105" HorizontalAlignment="Right" VerticalAlignment="Top" Width="116" MouseEnter="stackPanel1_MouseEnter" />
<Button Height="23" HorizontalAlignment="Left" Margin="30,0,0,92" Name="button1" VerticalAlignment="Bottom" Width="75" Click="button1_Click" MouseEnter="button1_MouseEnter">Button</Button>
<TextBox Height="23" Margin="34,0,124,49"
Name="textBox1" VerticalAlignment="Bottom" LostFocus="textBox1_LostFocus" GotFocus="textBox1_GotFocus"/>
<Button Height="23" HorizontalAlignment="Right" Margin="0,0,58,92" Name="button2" VerticalAlignment="Bottom" Width="75" Click="button2_Click">Random Color</Button>
</Grid>
</Window>

the code for the various events

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
///
/// Interaction logic for Window1.xaml
///

public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}

//these occur whenever the radiobutton is checked
private void radioButton1_Checked(object sender, RoutedEventArgs e)
{
stackPanel1.Background = new SolidColorBrush(Colors.Red);
}

private void radioButton2_Checked(object sender, RoutedEventArgs e)
{
stackPanel1.Background = new SolidColorBrush(Colors.Green);
}

private void radioButton3_Checked(object sender, RoutedEventArgs e)
{
stackPanel1.Background = new SolidColorBrush(Colors.Blue);
}

//this occurs when the button is clicked by a mouse
private void button1_Click(object sender, RoutedEventArgs e)
{
radioButton1.IsChecked = false;
radioButton2.IsChecked = false;
radioButton3.IsChecked = false;
stackPanel1.Background = new SolidColorBrush(Colors.White);
}

//this happens when the mouse enters the area of the button control
private void button1_MouseEnter(object sender, MouseEventArgs e)
{
button1.Background = new SolidColorBrush(Colors.Yellow);
}

//this doesn't work
private void stackPanel1_MouseEnter(object sender, MouseEventArgs e)
{
button1.Background = new SolidColorBrush(Colors.WhiteSmoke);
}

//this happens when you click out of the text box to do something else
private void textBox1_LostFocus(object sender, RoutedEventArgs e)
{
textBox1.Text = sender.ToString();
textBox1.Background = new SolidColorBrush(Colors.White);
MessageBox.Show("Come Back here");
}

//this happens when you click into the text box to type something
private void textBox1_GotFocus(object sender, RoutedEventArgs e)
{
textBox1.Background = new SolidColorBrush(Colors.Thistle);
}

//this randomly assigns a color to the stackpanel
private void button2_Click(object sender, RoutedEventArgs e)
{
//create an array of colors
Color[] myColors = new Color[4];
myColors[0] = Colors.Peru;
myColors[1] = Colors.SaddleBrown;
myColors[2] = Colors.Salmon;
myColors[3] = Colors.SeaShell;

//get a random number
Random rand = new Random();
int x = rand.Next(0, 3);

//set the background color of the stackpanel using the array
stackPanel1.Background = new SolidColorBrush(myColors[x]);
}



}
}

No comments:

Post a Comment