将变量从方法传递给按钮单击事件
本文关键字:按钮 单击 事件 变量 方法 | 更新日期: 2023-09-27 18:03:00
我希望能够将upperEncodedMsg传递到我的代码底部的msgLabel的文本属性。
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;
}
}
}
我试着让你的结构保持完整,有一些事情可以用更少的字符和更简单的方法来完成,但是随着你学习语言,练习是唯一可以提高你的技能的方法:)做得很好。
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;
}
}
}
我将使用Dictionary<char, char>
创建映射(请参阅此处了解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...
}
你可以删除你的大开关语句,并替换为以下内容:
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();
}
这样做的主要好处是映射可以在代码的其他部分可用。它还使维护更容易,并且映射在表单加载时就加载,而不是等待调用encode方法。查找Dictionary也可能比查找大型switch语句要快。