我正在尝试将一些代码放在另一个.cs文件中,因为我正在使用它 一个巨大的转换声明,我不希望人们趟过它 了解发生了什么。问题是txtresponse 描述在我的应用程序中写入的文本框的命令不是 在新文件中定义,我无法弄清楚为什么。这是我的新事物 档案:
(错误:此当前上下文中不存在txtresponse)
P.S。仅使用System.Windows.Forms;突出显示它包含所有应用程序表单, 按钮和他们做什么等...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GUI_Verify
{
class commands : Form
{
public static string call_command(string stmnt)
{
string cmd = "";
switch (stmnt)
{
case "TST_ALL":
txtresponse.Text = "\r\nTesting all memory tests.\r\n";
cmd = "1";
break;
case "TST_PKT_BFFR":
txtResponse.Text = "\r\nTest for Packet Buffer.\r\n";
cmd = "2";
break;
case "TST_XDATA":
txtResponse.Text = "\r\nTest for XDATA.\r\n";
cmd = "3";
break;
case "TST_REN_RAM":
txtResponse.Text = "\r\nTest for Retention Ram.\r\n";
cmd = "4";
break;
case "TST_CODE_RAM":
txtResponse.Text = "\r\nTest for Code Ram.\r\n";
cmd = "5";
break;
case "Copy_All":
txtResponse.Text = "\r\nCopying all memory to all places.\r\n";
cmd = "6";
break;
case "to_xdata":
txtResponse.Text = "\r\nCopying xdata -> xdata.\r\n";
cmd = "7";
break;
default:
txtResponse.Text = "\r\nunknown command\r\n";
break;
}
return cmd;
}
}
}
答案 0 :(得分:-1)
我有时会遇到这个问题。首先注释您的代码并删除表单中的txtresponse文本框。然后从工具箱中拖放新的TextBox并将其重命名为“txtresponse”。取消注释您的代码,希望它能够正常工作。
<强>更新强>
这只是对另一种方式的建议。没有继承Form类
使用输出参数来保存文本框的值。
namespace GUI_Verify
{
public class commands
{
public static string call_command(string stmnt, out string result)
{
string cmd = "";
switch (stmnt)
{
case "TST_ALL":
result = "\r\nTesting all memory tests.\r\n";
cmd = "1";
break;
case "TST_PKT_BFFR":
result = "\r\nTest for Packet Buffer.\r\n";
cmd = "2";
break;
case "TST_XDATA":
result = "\r\nTest for XDATA.\r\n";
cmd = "3";
break;
case "TST_REN_RAM":
result = "\r\nTest for Retention Ram.\r\n";
cmd = "4";
break;
case "TST_CODE_RAM":
result = "\r\nTest for Code Ram.\r\n";
cmd = "5";
break;
case "Copy_All":
result = "\r\nCopying all memory to all places.\r\n";
cmd = "6";
break;
case "to_xdata":
result = "\r\nCopying xdata -> xdata.\r\n";
cmd = "7";
break;
default:
result = "\r\nunknown command\r\n";
break;
}
return cmd;
}
}
}
在您的表单中执行以下操作。
string result="";
string cmd = commands.call_command("TST_ALL", out result);
txtresponse.Text = result;