1

Quick note - I am very new to c# so I apologize if this is stupid simple.

I am having a hard time trying to complete a simple c# task in a book.

Pseudocode-

Text box text = user input

if button one is clicked replace all capital letters in the text box with an asterisk

else if button two is clicked replace the asterisks with their original characters (back to normal)

Here is what I have so far

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


        }


    }
}

The problem is that button2 is showing the text with the asterisks. It should be showing (I want it to show) the original characters.

4

4 回答 4

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;
        }
    }
于 2013-10-06T07:37:10.250 回答
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;
        }

    }

  }
}
于 2013-10-06T07:46:05.187 回答
1

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

于 2013-10-06T07:30:56.973 回答
1

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

string orignalText = textBox1.Text;

进入第一次if检查。

于 2013-10-06T07:38:40.000 回答