0

I made a Math Quiz program by following this tutorial.

I wanted to add a debug menu to change different things such as stop the timer of Form1 by using a button on the debugForm. I got the button and the form set up but I am not sure how to import integers such as:

int TimeLeft

into debugForm for use in Form1.

Here is my code that I got so far.

I'm not sure where to start so I need help and if you need me to clarify something then just ask. Thanks!

Edit :

Sorry for not being really clear.
My question is : How do I take integers from one form and put them in another and have them being recognized?

4

4 回答 4

2

debugForm在你的课堂上添加这个:

private int m_timeLeft;
public int TimeLeft
{
    get
    {
        return m_timeLeft;
    }
    set
    {
        m_timeLeft = value;
        // some other actions if necessary
    }
}

现在,您可以通过以下方式访问此属性Form1

private void makeMenu(object sender, EventArgs e)
{
    debugForm form = new debugForm();
    form.TimeLeft = this.TimeLeft;
    form.Show();
}
于 2013-10-30T16:30:02.353 回答
0

为什么不将整数和任何其他成员/字段声明为公共,以便您可以从任何形式(类)访问它们。通过不指定访问修饰符,成员默认为私有,因此您无法从其他任何地方访问它们。

编辑:

好的,这是会员访问的一个简单示例:

首先你声明你的表单(这是一个类)

public class FormaA :Form 
{ 
      public int myFirstInteger = 100; //the public keyword in front of it does all the job 
      public int mySecondInteger = 50; //second integer declaration
}

现在让我们声明另一种形式

 public class FormB : Form {    

    //this is the FormB constructor
    //if you don't declare one C# will declare one for you
    public FormB()
    {
        //you have access to the members in the CONSTRUCTOR
        int myFormAInteger = formA.myFirstInteger; //Whoaallllaaaa ! magic ! you have acces to the member
        int myFormASecondInteger = formA.mySecondInteger ; //the second integer
    }

}

但是请记住,第一个表格的任何更改都不会在第二个表格中更新,您必须使用参考才能访问更新的值。

希望这可以帮助

考虑阅读一本书(DeitelBeginning C# 是一本非常棒的书,其中包含一些很好的示例,WroxBeginning Visual C# 2010 /2012 也是一本非常好的读物)在这里发布你的问题很有用,但在转向之前尝试自己解决问题帮助。

于 2013-10-30T16:31:53.653 回答
0

如果修改 debugform 的构造函数,则可以在声明新表单时传递所需的任何值:

Form 2 Code
-------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Math_Quiz
{
    public partial class debugForm : Form
    {
        public debugForm(int value1, string value2)
        {
            InitializeComponent();
            //set the values to local variables.
        }

        private void timeButton_Click(object sender, EventArgs e)
        {

        }
    }
}


private void makeMenu(object sender, EventArgs e)
{
    debugForm form = new debugForm(intvalue, stringvalue);
    form.Show();
}
于 2013-10-30T18:06:33.593 回答
0

我发现将 int 和字符串值从一个传递到下一个的最简单方法是:

 private void gameStart_Click(object sender, EventArgs e)
    {
        string machineName = System.Environment.MachineName;
        int numberOfPlayers = 10;

        multiplayerGame y = new multiplayerGame(numberOfPlayers, machineName);// multiplayerGame is the next form, and y is just a very simple variable the new form plus variables that are being passed is assigned to
                this.Hide();();// hides this (current) form
                y.ShowDialog();//here is where the variable y is used with .ShowDialog to make your program open the new form
        }
    }

上面的代码是我的一个程序中的一个示例,也是我在表单中使用的代码,传递了我想在下一个表单中使用的变量。在下一个表单中,您需要添加以下代码才能看到变量并将其分配给整个表单的可用变量。

 public partial class multiplayerGame : Form
{
    private int numPlayers;// This is the variable that will be used on this form for the int variable being passed 
    private string myPcName; ;// This is the variable that will be used on this form for the string variable being passed   
}

在接下来的部分中,您的程序将初始化您需要的所有内容,以允许 form1 中的变量传递到新表单中,并将其分配给为新表单创建的变量。在我的示例中,这是多人游戏。

public multiplayerGame(int numberOfPlayers, string machineName)// int so the new form knows your passing an int, then the name of your variable same goes for the string variable
    {
        this.myPcName = machineName;// here you assign the private string created above for this form to equal the string variable being passed into this from form the previous form
        this.numPlayers = numberOfPlayers; // here you assign the private int created above for this form to equal the int variable being passed into this from form the previous form
        InitializeComponent();
    }

然后,您将拥有一个可以使用的变量,该变量将被识别,并且在您加载第二个表单时已经具有从前一个表单设置的值。在这一点上唯一剩下的就是在需要的地方使用你的变量。

于 2015-06-26T03:51:11.370 回答