1

outputLabel を、モールス符号が出力されるラベル コントロール ボックスにしようとしています。モールス信号の最初の文字だけが表示され、残りのコードが表示されない理由がわかりません。(「cat」と入力すると、outlabel の最初のモールス符号文字のみが取得されます)

 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;

namespace MorseCode

{
    public partial class morseCodeForm : Form
    {
        public morseCodeForm()
        {
            InitializeComponent();
        }
        //Anthony Rosamilia
        //Make a key,value system to translate the user text. 
        //Make sure usertext appears only in lower case to minimise errors.
        private void convertButton_Click(object sender, EventArgs e)
        {
            Dictionary<char, String> morseCode = new Dictionary<char, String>()
            {
                {'a' , ".-"},{'b' , "-..."},{'c' , "-.-."}, //alpha
                {'d' , "-.."},{'e' , "."},{'f' , "..-."},
                {'g' , "--."},{'h' , "...."},{'i' , ".."},
                {'j' , ".---"},{'k' , "-.-"},{'l' , ".-.."},
                {'m' , "--"},{'n' , "-."},{'o' , "---"},
                {'p' , ".--."},{'q' , "--.-"},{'r' , ".-."},
                {'s' , ".-."},{'t' , "-"},{'u' , "..-"},
                {'v' , "...-"},{'w' , ".--"},{'x' , "-..-"},
                {'y' , "-.--"},{'z' , "--.."},
                //Numbers 
                {'0' , "-----"},{'1' , ".----"},{'2' , "..----"},{'3' , "...--"},
                {'4' , "....-"},{'5' , "....."},{'6' , "-...."},{'7' , "--..."},
                {'8' , "---.."},{'9' , "----."},
            };
            string userText = inputTextBox.Text;
            userText = userText.ToLower();
            for (int index = 0; index < userText.Length; index++)
            {
               /* if (index > 0)
                {
                    outLabel.Text = ('/').ToString();
                }
                */char t = userText[index];
                if (morseCode.ContainsKey(t))
                {
                    outLabel.Text = (morseCode[t]);
                }
            }
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            inputTextBox.Text = "";
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
4

1 に答える 1

3
outLabel.Text = (morseCode[t]);

Text プロパティを追加ではなく、まったく新しい値に設定しています。その代入がすでにあるものに文字列を追加したとしたら、奇妙ではありませんか?

古い値を保持する必要があります。

outLabel.Text += morseCode[t];

ただし、追加するたびに新しい文字列が作成されます。より良い解決策; StringBuilder最初の文字列、つまり変更可能な文字列で文字列を構築します。

var sb = new StringBuilder();
for (int index = 0; index < userText.Length; index++)
{
    var t = userText[index].ToLower();
    string morseValue;

    // no need for ContainsKey/[], use a single lookup
    if (morseCode.TryGetValue(t, out morseValue))
    {
        sb.Append(morseValue);
    }
}

outLabel.Text = sb.ToString();
于 2013-10-05T01:37:26.810 に答える