2013-10-06 171 views
1

快速提示 - 我对c#非常陌生,所以如果这很愚蠢,我很抱歉。用另一个字符替换字符串中的字符并切换回

我很难尝试在书中完成一个简单的c#任务。

Pseudocode-

文本框中的文本=用户输入

,如果一个按钮被点击 有星号

人换掉在文本框中全部为大写字母,如果两个按钮被点击 更换星号与他们的原始字符(回到正常)

这是我到目前为止

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Text.RegularExpressions; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 

     public Form1() 
     { 
      InitializeComponent(); 
      button1.Click += new System.EventHandler(ClickedButton); 
      button2.Click += new System.EventHandler(ClickedButton); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     public void ClickedButton(object sender, EventArgs e) 
     { 


      string orignalText = textBox1.Text; 


      if (sender == button1) 

      { 
       string replaced = Regex.Replace(orignalText, @"[A-Z]", "*"); 
       textBox1.Text = (replaced); 

      } 

      else if (sender == button2) 

      { 
       textBox1.Text = (orignalText); 
      } 


     } 


    } 
} 

问题是button2显示带星号的文本。它应该显示(我希望它显示)原始字符。

回答

3

originalText应该是一个类字段而不是局部变量。此外,如果有人点击button2,则不应存储文本框的值。尝试用它来取代您的ClickedButton方法:

string orignalText; 

    public void ClickedButton(object sender, EventArgs e) 
    { 
     if (sender == button1) 
     { 
      orignalText = textBox1.Text; 

      string replaced = Regex.Replace(orignalText, @"[A-Z]", "*"); 
      textBox1.Text = replaced; 
     } 
     else if (sender == button2) 
     { 
      textBox1.Text = orignalText; 
     } 
    } 
+0

谢谢亚历山大! –

1

您有2个问题。首先,您在知道按下哪个按钮之前设置了原始文本。其次,originalText是一个局部变量,所以当你想要替换它时,它将不再包含原始文本。

1

你只需要全球化originaltext变量和招行

string orignalText = textBox1.Text; 

进入第一if检查。

1

试试这个:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Text.RegularExpressions; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 

    public Form1() 
    { 
     InitializeComponent(); 
     button1.Click += new System.EventHandler(ClickedButton); 
     button2.Click += new System.EventHandler(ClickedButton); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 

    string orignalText = null; //you should define this var outside of ClickedButton method to keep its value during button1/2 clicks 

    public void ClickedButton(object sender, EventArgs e) 
    { 

     if (sender == button1) 
     { 
      orignalText = textBox1.Text; 
      string replaced = Regex.Replace(orignalText, @"[A-Z]", "*"); 
      textBox1.Text = replaced; 
     } 
     else if (sender == button2) 
     { 
      if (orignalText != null) textBox1.Text = orignalText; 
     } 

    } 

    } 
} 
相关问题