简体   繁体   中英

Get textbox.Text from FORM to CLASS

I have tried to get the textbox.text to a class and it doesent work what am I am doing wrong please help!

In my form.cs it looks like this:

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


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


    }

and in my CLASS it looks like this:

    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;


        }
        }


       }

error i get: 'WindowsFormApplication1.score1Calculator' does not contain a definition for 'player' and no extension method 'player' accepting a first argument of type 'WindowsFormApplication1.score1Calculator' could be found (are you missing a using directive or an assembly reference?)

you dont call getPlayerOneScore1Input() in order the score1 to be assigned, in Form1 try:

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

There is no need to do it overly complicated. A Textbox.Text value is just a string. You can directly call the constructor with this reference.

Just write it like this

score1Calculator theCalculator = new dart1Calculator(playerOneScore1TextBox.Text);

No need to store the value on module level.

On button click you are not calling your function getPlayerOneScore1Input() which actually assign value to score1 .

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

Instead of assigning value to score1 in form class and assign it to your class member field. You may try this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM