-3

問題が発生しました。誰かが私を助けてくれることを願っています:)テキストボックスを取得しました。ユーザーを制限して、\が2つ続くことを許可しないようにします。フォルダに使用しています。例: C\temp\test\ C\temp\test\\ と入力できないようにしたい

この問題を探してみましたが、このようなものは見つかりませんでした。だから私はそれが可能であることを願っています:)

これが私のテキストボックスのコードです

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        try
        {
            Regex regex = new Regex(@"[^C^D^A^E^H^S^T^]");
            MatchCollection matches = regex.Matches(textBox1.Text);
            if (matches.Count > 0)
            {
                MessageBox.Show("Character niet toegestaan!");
                textBox1.Text = "";
            }

            clsOpslagMedium objOpslag;  // definieert type object 
            objOpslag = new clsOpslagMedium();  // creert opject in memory
            objOpslag.DriveLetterString = textBox1.Text;
        }
        catch (Exception variableEx1)
        {
            MessageBox.Show("Foutmelding: " + variableEx1.Message);
        }
    }

誰かがいくつかの例を挙げて、十分な情報を提供してくれることを願っています:)

4

3 に答える 3

3

簡単な方法は、単に文字列を置き換えることです。

private void textBox1_TextChanged(object sender, EventArgs e)
{
    //get the current cursor position so we can reset it 
    int start = textBox1.SelectionStart;

    textBox1.Text = Regex.Replace(textBox1.Text, @"\\\\+", @"\");

    //make sure the cursor does reset to the beginning
    textBox1.Select(start, 0);
}

置換を囲む追加のコードにより、カーソルがテキストボックスの先頭にリセットされないようにします (これは、Textプロパティを設定したときに発生します)。

于 2013-06-19T14:24:16.550 に答える
0

すべての\-sequences ( \\, \\\, \\\\, ...) を見つけて、 で置き換える必要があります\。検索シーケンスに正規表現を使用できます

サンプル:

      string test=@"c:\\\adas\\dasda\\\\\\\ergreg\\gwege";
       Regex regex = new Regex(@"\\*");


       MatchCollection matches = regex.Matches(test);
        foreach (Match match in matches)
        {
            if (match.Value!=string.Empty)
                test = ReplaceFirst(test, match.Value, @"\");
        }
于 2013-06-19T14:27:15.003 に答える