将变量从方法传递到按钮单击事件

时间:2016-08-19 07:47:53

标签: c#

我希望能够将upperEncodedMsg传递到代码底部的msgLabel的text属性中。

namespace ProgrammingAssignmentDecoder
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();


        }

        public void Encode()
        {
            string message = Convert.ToString(messageTxt.Text);
            char[] encodeArray = message.ToCharArray();

            for (int i = 0; i < encodeArray.Length; i++)
            {
                char letter = (encodeArray[i]);

                switch (letter)
                {
                    case 'a':
                    case 'A':
                        encodeArray[i] = 't';
                        break;
                    case 'b':
                    case 'B':
                        encodeArray[i] = 'u';
                         break;
                    case 'c':
                    case 'C':
                        encodeArray[i] = 'v';
                        break;
                    case 'd':
                    case 'D':
                        encodeArray[i] = 'w';
                        break;
                    case 'e':
                    case 'E':
                        encodeArray[i] = 'x';
                        break;
                    case 'f':
                    case 'F':
                        encodeArray[i] = 'y';
                        break;
                    case 'g':
                    case 'G':
                        encodeArray[i] = 'z';
                        break;
                    case 'h':
                    case 'H':
                        encodeArray[i] = 'a';
                        break;
                    case 'i':
                    case 'I':
                        encodeArray[i] = 'b';
                        break;
                    case 'j':
                    case 'J':
                        encodeArray[i] = 'c';
                        break;
                    case 'k':
                    case 'K':
                        encodeArray[i] = 'd';
                        break;
                    case 'l':
                    case 'L':
                        encodeArray[i] = 'e';
                        break;
                    case 'm':
                    case 'M':
                        encodeArray[i] = 'f';
                        break;
                    case 'n':
                    case 'N':
                        encodeArray[i] = 'g';
                        break;
                    case 'o':
                    case 'O':
                        encodeArray[i] = 'h';
                        break;
                    case 'p':
                    case 'P':
                        encodeArray[i] = 'i';
                        break;
                    case 'q':
                    case 'Q':
                        encodeArray[i] = 'j';
                        break;
                    case 'r':
                    case 'R':
                        encodeArray[i] = 'k';
                        break;
                    case 's':
                    case 'S':
                        encodeArray[i] = 'l';
                        break;
                    case 't':
                    case 'T':
                        encodeArray[i] = 'm';
                        break;
                    case 'u':
                    case 'U':
                        encodeArray[i] = 'n';
                        break;
                    case 'v':
                    case 'V':
                        encodeArray[i] = 'o';
                        break;
                    case 'w':
                    case 'W':
                        encodeArray[i] = 'p';
                        break;
                    case 'x':
                    case 'X':
                        encodeArray[i] = 'q';
                        break;
                    case 'y':
                    case 'Y':
                        encodeArray[i] = 'r';
                        break;
                    case 'z':
                    case 'Z':
                        encodeArray[i] = 's';
                        break;



                }




            }

        }

        static string upperEncoded(char[] encodeArray, string upperEncodedMsg)
        {
            string encodedMsg = new string(encodeArray);
            upperEncodedMsg = encodedMsg.ToUpper();
            return upperEncodedMsg;

        }



        private void clearBtn_Click(object sender, EventArgs e)
        {
            messageTxt.Text = string.Empty;
            msgLabel.Text = string.Empty;
            processedMessageLabel.Text = "Processed Message: ";

        }

        private void encodeBtn_Click(object sender, EventArgs e)
        {
            Encode();

            if (messageTxt.TextLength == 0)
            {
                MessageBox.Show("There is no message to Encode");
            }
            else
            {
                processedMessageLabel.Text = "Encoded Message: ";
                msgLabel.Visible = true;
            }

            msgLabel.Text = upperEncodedMsg;

        }
    }

}

2 个答案:

答案 0 :(得分:0)

我试图保持你的结构,有一些事情可以用较少的字符和较简单的方式完成,但是当你学习语言练习是唯一可以提高你的技能:)所以干得好。

namespace ProgrammingAssignmentDecoder
{

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private string upperEncodedMsg = null;

    public void Encode()
    {
        string message = Convert.ToString(messageTxt.Text);
        char[] encodeArray = message.ToCharArray();

        for (int i = 0; i < encodeArray.Length; i++)
        {
            char letter = (encodeArray[i]);

            switch (letter)
            {
                case 'a':
                case 'A':
                    encodeArray[i] = 't';
                    break;
                case 'b':
                case 'B':
                    encodeArray[i] = 'u';
                     break;
                case 'c':
                case 'C':
                    encodeArray[i] = 'v';
                    break;
                case 'd':
                case 'D':
                    encodeArray[i] = 'w';
                    break;
                case 'e':
                case 'E':
                    encodeArray[i] = 'x';
                    break;
                case 'f':
                case 'F':
                    encodeArray[i] = 'y';
                    break;
                case 'g':
                case 'G':
                    encodeArray[i] = 'z';
                    break;
                case 'h':
                case 'H':
                    encodeArray[i] = 'a';
                    break;
                case 'i':
                case 'I':
                    encodeArray[i] = 'b';
                    break;
                case 'j':
                case 'J':
                    encodeArray[i] = 'c';
                    break;
                case 'k':
                case 'K':
                    encodeArray[i] = 'd';
                    break;
                case 'l':
                case 'L':
                    encodeArray[i] = 'e';
                    break;
                case 'm':
                case 'M':
                    encodeArray[i] = 'f';
                    break;
                case 'n':
                case 'N':
                    encodeArray[i] = 'g';
                    break;
                case 'o':
                case 'O':
                    encodeArray[i] = 'h';
                    break;
                case 'p':
                case 'P':
                    encodeArray[i] = 'i';
                    break;
                case 'q':
                case 'Q':
                    encodeArray[i] = 'j';
                    break;
                case 'r':
                case 'R':
                    encodeArray[i] = 'k';
                    break;
                case 's':
                case 'S':
                    encodeArray[i] = 'l';
                    break;
                case 't':
                case 'T':
                    encodeArray[i] = 'm';
                    break;
                case 'u':
                case 'U':
                    encodeArray[i] = 'n';
                    break;
                case 'v':
                case 'V':
                    encodeArray[i] = 'o';
                    break;
                case 'w':
                case 'W':
                    encodeArray[i] = 'p';
                    break;
                case 'x':
                case 'X':
                    encodeArray[i] = 'q';
                    break;
                case 'y':
                case 'Y':
                    encodeArray[i] = 'r';
                    break;
                case 'z':
                case 'Z':
                    encodeArray[i] = 's';
                    break;
            }
        }
       foreach (char eachChar in encodeArray) {
           upperEncodedMsg += eachChar;
       }

    }

   public void upperEncoded()

   {
       if (upperEncodedMsg != null)
       { upperEncodedMsg = upperEncodedMsg.ToUpper(); }

   }



    private void clearBtn_Click(object sender, EventArgs e)
    {
        messageTxt.Text = string.Empty;
        msgLabel.Text = string.Empty;
        processedMessageLabel.Text = "Processed Message: ";

    }

    private void encodeBtn_Click(object sender, EventArgs e)
    {
        if (messageTxt.TextLength == 0)
        {
            MessageBox.Show("There is no message to Encode");
        }
        else
        {
            Encode();
            upperEncoded();
            processedMessageLabel.Text = "Encoded Message: ";
            msgLabel.Visible = true;
        }

        msgLabel.Text = upperEncodedMsg;

    }
}

}

答案 1 :(得分:0)

我沿着使用Dictionary<char, char>创建映射的路线(参见here for details on Dictionary),然后在encodeArray中为每个字符查找字典中的键值。使用StringBuilder而不仅仅是字符串来存储编码消息也可能更好。

创建地图

//Add Dictionary at the top of the class along with upperEncodedMsg string.
private Dictionary<char, char> charMapping;
private StringBuilder upperEncodedMsg;

//Create mappings. Use uppercase values here and you won't need to use your 'upperEncoded' method. This bit can be done in your Form1 constructor.

public Form1()
{
    InitializeComponent();

    charMapping = new Dictionary<char, char>();
    charMapping.Add('A', 'T');
    charMapping.Add('B', 'U');

    //more mappings...
}

然后,您可以删除您拥有的big switch语句,并将其替换为以下内容:

public void Encode()
{
    upperEncodedMsg = new StringBuilder();
    string message = Convert.ToString(messageTxt.Text);
    char[] encodeArray = message.ToUpper().ToCharArray();

    for(int i = 0; i < encodeArray.Length; i++)
    {
        //Use the mappings created earlier to get the associated char.
        char outputLetter;
        charMapping.TryGetValue(encodeArray[i], out outputLetter);

        //Append letter to your upperEncodedMsg StringBuilder.          
        upperEncodedMsg.Append(outputLetter);
    }
}

在按钮单击中,您可以在底部添加:

private void encodeBtn_Click(object sender, EventArgs e)
{
    //your code...

    msgLabel.Text = upperEncodedMsg.ToString();
}

这样做的主要好处是您的映射可以在代码的其他部分中使用。它还使表单更容易维护和映射,而不是等待调用您的编码方法。查找字典也可能比大型switch语句更快。