从FORM到CLASS获取textbox.Text

时间:2015-02-20 10:08:48

标签: c# winforms class

我已经尝试将textbox.text设置为一个类并且它可以正常工作我正在做错了请帮忙!

在我的form.cs中,它看起来像这样:

    public string score1;
    public void getPlayerOneScore1Input()
    {
        score1 = playerOneScore1TextBox.Text;
    }


    public void playerOneAddScoreButton_Click(object sender, EventArgs e)
    {
        score1Calculator theCalculator = new score1Calculator(score1);


    }

在我的CLASS中它看起来像这样:

    class score1Calculator
    {
    public score1Calculator(string score1)
        {
            this.score1= score1;

        }

    public int playerOneDart1Value;
    public void calculateDart1()
    {
        if (score1== "t1" || score1== "T1" || score1== "3")
        {
            playerOneDart1Value = 3;

        }
  else
        {
            MessageBox.Show("Dart 1! This is not a valid input!");
            return;


        }
        }


       }

错误我得到: 'WindowsFormApplication1.score1Calculator'不包含'player'的定义,并且没有扩展方法'player'接受类型'WindowsFormApplication1.score1Calculator'的第一个参数可以找到(你是否缺少using指令或汇编引用?)

3 个答案:

答案 0 :(得分:0)

您要拨打getPlayerOneScore1Input()以便分配score1,请在Form1中尝试:

 public void playerOneAddScoreButton_Click(object sender, EventArgs e)
    {
        getPlayerOneScore1Input();
        score1Calculator theCalculator = new dart1Calculator(score1);
    }

答案 1 :(得分:0)

没有必要过于复杂。 Textbox.Text值只是一个字符串。您可以使用此引用直接调用构造函数。

就这样写吧

score1Calculator theCalculator = new dart1Calculator(playerOneScore1TextBox.Text);

无需将值存储在模块级别。

答案 2 :(得分:0)

点击按钮,您不会调用实际为getPlayerOneScore1Input()分配值的功能score1

public void playerOneAddScoreButton_Click(object sender, EventArgs e)
{
    getPlayerOneScore1Input();
    score1Calculator theCalculator = new dart1Calculator(score1);
}

而不是在表单类中为score1赋值并将其分配给您的类成员字段。你可以试试这个。

public void playerOneAddScoreButton_Click(object sender, EventArgs e)
{
    if (!String.IsNullOrEmpty(playerOneScore1TextBox.Text))
    {
         score1Calculator theCalculator = new dart1Calculator(playerOneScore1TextBox.Text);
    }
}