1

簡単なメモ - 私は c# に非常に慣れていないので、これがばかげて単純である場合は申し訳ありません。

本で簡単な C# タスクを完了するのに苦労しています。

疑似コード-

テキスト ボックスのテキスト = ユーザー入力

ボタン 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);
            }


        }


    }
}

問題は、button2 がアスタリスク付きのテキストを表示していることです。元のキャラクターを表示する必要があります(表示したい)。

4

4 に答える 4

3

originalText、ローカル変数ではなくクラス フィールドである必要があります。また、誰かが をクリックした場合に備えて、テキストボックスの値を保存しないでくださいbutton2ClickedButtonメソッドを次のように置き換えてみてください。

    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

2 つの問題があります。まず、どのボタンが押されたかを知る前に originalText を設定しています。2 つ目は、originalText はローカル変数であるため、元のテキストに置き換えようとすると、元のテキストが含まれなくなります。

于 2013-10-06T07:30:56.973 に答える
1

originaltext変数をグローバル化して行を移動するだけです

string orignalText = textBox1.Text;

最初のifチェックに。

于 2013-10-06T07:38:40.000 に答える