简体   繁体   English

使用WPF在C#中进行用户输入的字母验证

[英]Letter-only validation for user input in C# with WPF

I'm fairly new to using C# and I've been working on a small game to help me get used to the language. 我很擅长使用C#而且我一直在开发一款小游戏来帮助我习惯这种语言。 I want players to be able to input a letters-only name to be used throughout the course of the game; 我希望玩家能够在整个游戏过程中输入一个仅用于字母的名称; however, I've run into several issues trying to get the validation to work. 但是,我遇到了几个试图让验证工作的问题。

Here is the XAML code: 这是XAML代码:

<Window x:Class="COMP4_Project.InputWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="New Game" Height="120" Width="250" ResizeMode="CanMinimize">
<Grid Margin="10">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />

    </Grid.RowDefinitions>
    <Label>Enter name:</Label>
    <TextBox x:Name="userInput" Grid.Column="2" Margin="0,0,0,0"></TextBox>
    <Button x:Name="Confirm" Click="Confirm_Click" Grid.Row="2" Width="80" Height="25" Content="Confirm" Margin="0,10,0,41" />
    <Button x:Name="Cancel" Click="Cancel_Click" Grid.Row="2" Grid.Column="1" Width="80" Height="25" Content="Cancel" HorizontalAlignment="Right" Margin="54,10,0,41" />
</Grid>

And here is my codebehind: 这是我的代码隐藏:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
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.Shapes;

namespace COMP4_Project
{
/// <summary>
/// Interaction logic for InputWindow.xaml
/// </summary>
public partial class InputWindow : Window
{
    public InputWindow()
    {
        InitializeComponent();
    }

    private void Confirm_Click(object sender, RoutedEventArgs e)
    {
        string playerName;
        string userInput = "";

        Regex r = new Regex("^[a-zA-Z]*$");
        if (r.IsMatch(userInput))
        {
            MessageBox.Show("onlyletter");
            playerName = userInput;
            Window win = new GameWindow();
            win.Owner = this;
            win.ShowDialog();
        } 
        else
        {
            MessageBox.Show("Only letters permitted. Try again!");
        }
    }

    private void Cancel_Click(object sender, RoutedEventArgs e)
    {
        Close();

    }
}
}

I've tried several methods of getting the validation to work; 我已经尝试了几种让验证工作的方法; this is as close as I've gotten so far without breaking the application. 这和我到目前为止一样接近,没有破坏应用程序。 It seems to accept any input despite the use of validation, including input with numbers and input where nothing has been typed at all (I tried changing the * in regex to a + to solve this, but when I did it wouldn't accept any input). 它似乎接受任何输入,尽管使用验证,包括输入数字和输入根本没有输入任何东西(我尝试将正则表达式中的*更改为+来解决这个问题,但是当我这样做时它不会接受任何输入输入)。

I feel like this issue has something to do with how I declared the variable for userInput, but I'm not sure how to work around it without more errors popping up. 我觉得这个问题与我为userInput声明变量的方式有关,但我不知道如何解决它而不会出现更多错误。

You're defining userInput as an empty string inside the Click event. 您将userInput定义为Click事件中的空字符串。

private void Confirm_Click(object sender, RoutedEventArgs e)
{
    string playerName;
    string userInput = "";  // An empty string is a match for your pattern

    Regex r = new Regex("^[a-zA-Z]*$");
    if (r.IsMatch(userInput))
    {
       ...

Set userInput to the value in the TextBox where they're entering their name. userInput设置为TextBox中输入其名称的值。

private void Confirm_Click(object sender, RoutedEventArgs e)
{
    string playerName;
    string userInput = userInput.Text;

    Regex r = new Regex("^[a-zA-Z]*$");
    if (r.IsMatch(userInput))
    {
       ...

You are doing this: 你这样做:

string userInput = "";

instead of: 代替:

string user = userInput.Text;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM