【问题标题】:Error message in calculator code计算器代码中的错误消息
【发布时间】:2016-05-06 03:49:01
【问题描述】:

我正在为 c#.net 中的计算器编码。我在 txtDisplay.Text 中的 txtDisplay 上有一条错误消息。错误说“当前上下文中不存在名称'txtDisplay'”。到目前为止我的代码是:

using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

    private void btnOne_Click(object sender, EventArgs e)
    {
      txtDisplay.Text = txtDisplay.Text + btnOne.Text;
    }

【问题讨论】:

  • 您还需要显示partial 类的其他部分。
  • 我认为 txtDisplay 是您表单上的一个文本框。你真的在设计器模式下添加了吗?

标签: c# winforms


【解决方案1】:

我测试了以下代码,它工作正常。就像 cmets 所说,如果您尚未创建名称为 txtDisplay 的元素,您将收到此错误。更多代码会有所帮助,因此我可以相应地编辑我的答案。

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

    private void btnOne_Click(object sender, EventArgs e)
    {
        txtDisplay.Text += btnOne.Text;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    • 2011-02-11
    • 2011-07-10
    相关资源
    最近更新 更多