2017-05-06 10 views
0

Visual StudioでWindowsフォームアプリケーションを作成しようとしています。私がしようとしているのは、ユーザがRichTextBoxに何かを入力したときに、あなたが入力したものを削除し、プリセットされた文字に置き換えるときです。私が今までに持っているものは次のとおりです。入力テキストをプリセットテキストに置き換える方法c#

private void richTextBox1_TextChanged(object sender, EventArgs e) 
{ 
    string text = richTextBox1.Text; 
    richTextBox1.Text = text.Remove(text.Length - 1, 1); 
} 

したがって、1文字を入力すると、それを消去します。それ以降、私はプリセットテキストの1文字を追加したいと思っています。だから、テキストThis is a test text that is reasonably longがあるとします。ユーザーが「A」を入力すると、文字「T」が表示されます。ユーザーが別の文字を入力すると、代わりに次の文字「h」が表示され、その後は完全なテキストThis is a test text that is reasonably longが表示され、その後は入力できなくなります。

は、ここで必要に応じてより多くのコードです:

private void button6_Click(object sender, EventArgs e) 
{ 
    webBrowser1.Navigate(textBox1.Text); 
} 

private void button5_Click(object sender, EventArgs e) 
{ 
    webBrowser1.Navigate("www.google.com"); 
} 

private void richTextBox1_TextChanged(object sender, EventArgs e) 
{ 
    string text = richTextBox1.Text; 
    richTextBox1.Text = text.Remove(text.Length - 1, 1); 
} 
+0

richTextBox1.Text = text.Substring(0、richTextBox1.Length); ? – VirCom

答えて

0

あなたはこれに似た何かを行うことができます。

public string longText = "This is a test text that is reasonably long"; 
private void RichTextBox1_TextChanged(object sender, EventArgs e) 
{    
    // I don't know if the user can press enter 
    // and give you an empty string so this checks that 
    // and that richTextBox1.Text is not greater than the 
    // text you want replace with, in this case longText 
    if (richTextBox1.Text.Length <= longText.Length && richTextBox1.Text.Length > 0) 
    { 
     string text = richTextBox1.Text; 
     // removes the last character, Remove returns 
     // a new string so you have to save it somewhere 
     text = richTextBox1.Text.Remove(text.Length - 1, 1); 
     // here you add the character you want to replace 
     text += longText[richTextBox1.Text.Length - 1]; 
     richTextBox1.Text = text; 
    } 
    else if (richTextBox1.Text.Length > longText.Length) 
    { 
     // this case is when the user wants to add another 
     // character but the whole text it's been already replaced 
     richTextBox1.Text = richTextBox1.Text.Remove(richTextBox1.Text.Length - 1, 1); 
     } 
    } 

これが役立つ場合はお知らせください。

+0

ようこそ。質の高い回答を提供するためには、この[how-to-answer](http://stackoverflow.com/help/how-to-answer)をお読みください。 – thewaywewere

+0

"if(enrichedText.Length <= longText.Length)'行に "System.NullReferenceException: 'オブジェクト参照がオブジェクトのインスタンスに設定されていません。'というエラーが表示されていて、" enrichedText wasヌル"。あなたの答えには何かがありますか? – MainframeHacker

+0

申し訳ありません@MainframeHacker、私は私のVSと何かをして、私はそれをテストすることができませんでした。私はすでに答えを編集し、より良いものを与えました、私はheheと思います。よろしく! – santipl

関連する問題