2013-05-14 3 views
1

돈으로 계산해야하며 사용자가 입력 할 수있는 텍스트 상자를 몇 개 만들었습니다. (그래서 2 유로, 5 유로 등) 지금은 합계로 계산하고 싶지만, 텍스트 상자가 비어 있다면, 오히려 그것을 계산할 수 없습니다. 몇 가지 방법을 시도해 봤지만 프로그램이 자동으로 모든 입력을 계산하기 때문에 작동하지 않습니다.텍스트 상자로 계산하고 빈 텍스트 상자를 건너 뛰는 방법은 무엇입니까?

짧게 유지하려면 계산 중에 빈 텍스트 상자를 건너 뛰고 싶습니다. 난 당신이 나에게 간단한 솔루션을 제공 할 수 있기를 바랍니다

) 때로는 빈 상자가 있기 때문에,

Int32 aa = Int32.Parse(textBox1.Text); // 5 c 
Int32 ba = Int32.Parse(textBox2.Text); // 10 c 
Int32 ca = Int32.Parse(textBox3.Text); // 20 c 
Int32 da = Int32.Parse(textBox4.Text); // 50 c 
Int32 ea = Int32.Parse(textBox5.Text); // 1 e 
Int32 fa = Int32.Parse(textBox6.Text); // 2 e 
Int32 ga = Int32.Parse(textBox7.Text); // 5 e 
Int32 ha = Int32.Parse(textBox8.Text); // 10 e 
Int32 ia = Int32.Parse(textBox9.Text); // 20 e 
Int32 ja = Int32.Parse(textBox10.Text); // 50 e 
Int32 ka = Int32.Parse(textBox11.Text); // 100 e 
Int32 total = ((aa * 5) + (ba * 10) + (ca * 20) + (da * 50) + (ea * 100) + (fa * 200) + (ga * 500) + (ha * 1000) + (ia * 2000) + (ja * 5000) + (ka * 100000))/100; 
richTextBox1.AppendText("\r\nTotaal: € " + total.ToString()); 

그러나 그것은 작동하지 않습니다

지금 나는이 있습니다.

+1

'총합 '에'decimal'을 사용해야합니다 !! –

답변

2

을 당신이해야한다고 생각 이 approch를 사용하십시오 :

var list = new List<Tuple<TextBox, decimal>>(){ 
      Tuple.Create(textBox1, 0.05m), 
      Tuple.Create(textBox2, 0.1m), 
      Tuple.Create(textBox3, 0.2m), 
      Tuple.Create(textBox4, 0.5m), 
      Tuple.Create(textBox5, 1m), 
      Tuple.Create(textBox6, 2m), 
      Tuple.Create(textBox7, 5m), 
      Tuple.Create(textBox8, 10m), 
      Tuple.Create(textBox9, 20m), 
      Tuple.Create(textBox10, 50m), 
      Tuple.Create(textBox11, 100m), 
     }; 
    decimal sum = list.Sum(tuple =>{ 
     int a = 0; 
     int.TryParse(tuple.Item1.Text, out a); 
     return a * tuple.Item2; 
    }); 
+0

어떻게이 코드와 함께 (모두 함께) 표시합니까? –

+0

합계는'sum'입니다. –

+0

'richTextBox1.AppendText (sum);'내가 richTextBox1.AppendText (sum.ToString);을 시도하면 오류가 10 진수에서 문자열로 변환되지 않고 오류가 발생합니다. 메서드 그룹에서 문자열로 변환 할 수 없습니다. –

1

숫자가 아닌 데이터 (빈 문자열 포함)의 경우 0으로 변수를로드하는 int.TryParse을 사용할 수 있습니다.

int a = 0; 
int.TryParse(textBox1.Text, out a); 
1

당신은 Int32.TryParse 사용할 수 있지만, 당신은 당신의 코드를 정리하기 위해 확장 방법이 결합 할 수 있습니다 :

public static Int32 NumberOrZero(this string input) 
{ 
    Int32 output = 0; 

    if (!string.IsNullOrWhiteSpace(input)) 
    { 
     Int32.TryParse(input, out output); 
    } 

    return output; 
} 

그런 다음 당신이 할 수 있습니다

Int32 aa = textBox1.Text.NumberOrZero(); // 5 c 
Int32 ba = textBox2.Text.NumberOrZero(); // 10 c 
Int32 ca = textBox3.Text.NumberOrZero(); // 20 c 
Int32 da = textBox4.Text.NumberOrZero(); // 50 c 
Int32 ea = textBox5.Text.NumberOrZero(); // 1 e 
Int32 fa = textBox6.Text.NumberOrZero(); // 2 e 
Int32 ga = textBox7.Text.NumberOrZero(); // 5 e 
Int32 ha = textBox8.Text.NumberOrZero(); // 10 e 
Int32 ia = textBox9.Text.NumberOrZero(); // 20 e 
Int32 ja = textBox10.Text.NumberOrZero(); // 50 e 
Int32 ka = textBox11.Text.NumberOrZero(); // 100 e 
+0

참고 : 확장 메서드는 고정되어 있어야하며 비표준 정적 클래스에 정의되어야합니다. 자신이 속한 네임 스페이스가 범위에있을 때 범위 내에 있습니다. http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx –

+0

@JeremyPridemore 아, 그래, 나는 'static'키워드를 놓친 것을 알고 있었다. 감사합니다 – mattytommo

2

많은 유사 변수를 개별적으로 다루는 대신 컬렉션을 활용하도록 코드를 변경했습니다.

을 나타내는 텍스트 상자에 각 통화 값을 매핑하는 사전 만들기 :

var mapping = new Dictionary<decimal, TextBox>() 
{ 
    {.05, textBox1}, 
    {.10, textBox2}, 
    {.20, textBox3}, 
    {.50, textBox4}, 
    {1, textBox5}, 
    {2, textBox6}, 
    //... 
}; 

그런 다음 당신이 그 값 Where 텍스트를 얻을 수있는 유효한 정수 및 Sum 그들이다.

int n; 
decimal total = mapping.Where(pair => int.TryParse(pair.Value.Text, out n)) 
    .Sum(pair => pair.Key * int.Parse(pair.Value.Text)); 
관련 문제