2013-10-06 4 views
1

빠른 메모 - 나는 매우 어리 석다.문자열의 문자를 다른 문자로 바꾸고 다시 전환하십시오.

책에서 간단한 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

두 가지 문제가 있습니다. 먼저, 어떤 버튼을 눌렀는지 알기 전에 원래의 텍스트를 설정하고 있습니다. 둘째, originalText는 지역 변수이므로 다시 대체 할 때 원래 텍스트가 더 이상 포함되지 않습니다.

1

당신은 originaltext 변수를 세계화와 제 if 검사에 선

string orignalText = textBox1.Text; 

를 이동해야합니다.

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

    } 

    } 
} 
관련 문제