C# 用另一个字符替换字符串中的字符并切换回

C# 用另一个字符替换字符串中的字符并切换回,c#,visual-studio,C#,Visual Studio,速记-我对c#很陌生,所以如果这很简单,我很抱歉 我很难在一本书中完成一个简单的c#任务 伪码 文本框Text=用户输入 如果单击按钮1 将文本框中的所有大写字母替换为星号 否则,如果单击按钮2 将星号替换为其原始字符(恢复正常) 这是我到目前为止所拥有的 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; usi

速记-我对c#很陌生,所以如果这很简单,我很抱歉

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

伪码

文本框Text=用户输入

如果单击按钮1 将文本框中的所有大写字母替换为星号

否则,如果单击按钮2 将星号替换为其原始字符(恢复正常)

这是我到目前为止所拥有的

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);
            }


        }


    }
}

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

您有两个问题。首先,您要先设置原始文本,然后才能知道按下了哪个按钮。其次,originalText是一个局部变量,因此当您要将其替换回时,它将不再包含原始文本。

您有两个问题。首先,您要先设置原始文本,然后才能知道按下了哪个按钮。其次,originalText是一个局部变量,因此当您要将其替换回时,它将不再包含原始文本。originalText应该是一个类字段,而不是局部变量。此外,您不应存储文本框的值,以防有人单击
按钮2
。尝试用以下方法替换您的
clicked按钮
方法:

    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;
        }
    }

originalText
应该是类字段而不是局部变量。此外,您不应存储文本框的值,以防有人单击
按钮2
。尝试用以下方法替换您的
clicked按钮
方法:

    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;
        }
    }

您只需要全球化
originaltext
变量并移动行

string orignalText = textBox1.Text;

进入第一个
if
检查。

您只需全球化
originaltext
变量并移动行即可

string orignalText = textBox1.Text;
进入第一个
如果
检查。

尝试以下操作:

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;
        }

    }

  }
}
试试这个:

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;
        }

    }

  }
}