限制TextBox只能輸入整數(shù),而且整數(shù)的數(shù)值范圍為0-100。如果輸入101后,彈窗提示輸入超限
MainWindow.xaml文章來源:http://www.zghlxwxcb.cn/news/detail-855989.html
<TextBox x:Name="textBox1" TextWrapping="Wrap" Text="TextBox" Width="120" Height="50" LostFocus="myTextBox_LostFocus"/>
MainWindow.xaml.cs文章來源地址http://www.zghlxwxcb.cn/news/detail-855989.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 NumRange2
{
/// <summary>
/// MainWindow.xaml 的交互邏輯
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void myTextBox_LostFocus(object sender, RoutedEventArgs e)
{
// 在這里添加你的處理代碼
string textVal = textBox1.Text;
try
{
int numVal = int.Parse(textVal);
if (numVal > 100 || numVal < 0)
{
MessageBox.Show("輸入超限");
}
}
catch(Exception ex)
{
MessageBox.Show("請輸入一個(gè)整數(shù)");
}
}
}
}
到了這里,關(guān)于WPF中TextBox失去焦點(diǎn)事件的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!