簡體   English   中英

c#驗證輸入字符串並使用線程替換為TitleCase

[英]c# Validating input string and replace to TitleCase using Threading

我想檢查用戶在文本框中寫的內容,並從winform中包含的文本框中獲取此輸入字符串,然后將其轉換為將單詞的每個字母都大寫。

使用此輸入,我首先需要驗證他們是否將文本框留空,為Null。 第二步是使用Title大小寫檢查他們是否形成了連貫的句子“換句話說,確保他們沒有大寫鎖定或反之亦然”。

我一直在研究有關驗證的Threading和Regex。 使用線程我已經使用了IsLower()方法,然后使用了ToLower()方法。 這可能是轉換輸入的相當長的方法,至少可以將每個單詞的所有首字母都轉換為大寫,而將所有中位數轉換為小寫。

如果我還沒有很好地解釋我的問題,請問一下,並樂意為您提供任何其他信息,請在下面找到我的源代碼:

        string myText = tbProductId.Text;

        //// Check for null values
        if (myText.Equals(""))
        {    //// display Error prompt
            MessageBox.Show("Please enter somthing");
            //// Get length of entered string
        }

        else
        {

            for (int i = 0; i < myText.Length; i++)
            {
                //// check if lower case
                if (char.IsLower(myText[i]))
                {
                    string asTitleCase = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.
                    ToTitleCase(myText.ToLower());
                    MessageBox.Show("Please enter non Captilised strings");

                }
            }
            for (int i = 0; i < myText.Length; i++)
            {
                if (char.IsUpper(myText[i]))
                {
                    string asTitleCase = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.
                   ToTitleCase(myText.ToUpper());
                    MessageBox.Show("Please enter strings in Title Case");
                }
            }

        }
    }

感謝您提供所有反饋和幫助,請在下面找到@ WiktorStribiżew提供的解決方案。 但是,還有一個問題,當按住shift鍵時,它是不活動的,並且不起作用,為什么在大寫鎖定時不起作用,而對shift鍵卻不起作用?

   string myText = tbProductId.Text;
        if (myText.Equals(""))
        {
            MessageBox.Show("Please enter somthing");
        }
        else
        {
            tbProductId.Text = System.Threading.Thread.CurrentThread.CurrentCulture.TextInf‌​o.ToTitleCase(tbProd‌​uctId.Text);

            tbProductId.Focus();
            tbProductId.Select(tbProductId.Text.Length, 0);
            //Move Cursor to location of where error

        }

暫無
暫無

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

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