簡體   English   中英

檢查字符串是否為特定字符,並將其限制為一個字符

[英]Check if string is a specific character, and limit it to one character

我正在為我的 C# class 制作計算器。 我不允許對操作數使用按鈕。 有一個操作數文本框,用戶應該在其中輸入“+”表示加法,“-”表示減法,“/”表示除法,或“*”表示乘法。

我試圖找出如何使它只能輸入這四個字符之一,但它也只能讓你一次輸入一個。

我對 C# 很陌生。

感謝“TheGeneral”通過給我這個鏈接幫助我解決這個問題。

我不確定為什么我在 Google 上找不到這個問題。

我是這樣做的:

private void TxtOperator_TextChanged(object sender, EventArgs e)
    {
        Regex regex = new Regex(@"[^+^\-^\/^\*^\(^\)]");
        MatchCollection matches = regex.Matches(txtOperator.Text);
        if (matches.Count > 0)
        {
            MessageBox.Show("Please input an operator.");
            txtOperator.Text = "";
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM