In the previous article "Four user controls of the WPF game series", a list box of items in "My Shop" was created through user controls. This document continues to use the user control to create a bank interface, and verify the validity of the input amount when the user saves money.
1. After creating the control BankElement. xaml, enter the XAML code in it:
<Grid> <Border BorderThickness="3" CornerRadius="5" Background="#CEE4E5" BorderBrush="#0C7D42"> <StackPanel Orientation="Vertical" Margin="5"
HorizontalAlignment="Center"> <Image Height="80" Width="80" Margin="5"
Source="{Binding BankImage}"></Image> <TextBlock Name="bankCash" Margin="5"
Text="{Binding BankCash}"></TextBlock> <TextBlock Name="bankInterest" Margin="5"
Text="{Binding BankInterest}"></TextBlock> <TextBlock Name="transferInfo" Margin="5"></TextBlock> <StackPanel Orientation="Horizontal"> <TextBox Name="cashTransfer" Margin="5" Width="100"></TextBox> <Image Name="transImage" Source="image/trans.png" Cursor="Hand" Width="20" Height="20"></Image> </StackPanel> </StackPanel> </Border></Grid>
Style and:
2. After you enter the amount in TextBox, you must first verify the data in two ways:. whether the input amount is a number, B. whether the input amount exceeds the current total amount, and then process the corresponding results.
Private void saveCashImage_MouseLeftButtonDown (object sender, MouseButtonEventArgs e) {Image transferImage = sender as Image; // search for the input amount of TextBox object findCashTextBox = queryGrid. findName ("saveCash"); TextBox saveCashInput = findCashTextBox as TextBox; // find the TextBlock object findCashTextBlock = queryGrid in the feedback result. findName ("saveCashInfo"); TextBlock saveCashInfo = findCashTextBlock as TextBlock; // The current total amount ($750) int remainAmount = Convert. toInt32 (transferImage. tag. toString ());
// Int transAmount;
// Determine whether saveCashInput is a number. Otherwise, transAmout is 0 int. tryParse (saveCashInput. text, out transAmount); // if transAmout is 0, the input data is incorrect. Of course, the input is 0 if (transAmount = 0) {saveCashInfo. text = "Not vaild number";} else {// if the current total amount is exceeded...
If (transAmount> remainAmount) {saveCashInfo. Text = "No enough cash";} else {// Money can be saved to the bank }}}
3. Error message:
To be continued... ...