Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从一种形式到另一种形式获取整数和不同的值?_C#_Forms_Methods_Integer - Fatal编程技术网

C# 如何从一种形式到另一种形式获取整数和不同的值?

C# 如何从一种形式到另一种形式获取整数和不同的值?,c#,forms,methods,integer,C#,Forms,Methods,Integer,我做了一个数学测验程序 我想添加一个debug菜单来更改不同的内容,例如使用debugForm上的按钮停止Form1的计时器。我设置了按钮和表单,但我不确定如何导入整数,例如: int TimeLeft into debugForm for use in Form1. 我不知道从哪里开始,所以我需要帮助,如果你需要我澄清一些事情,那么就问吧。谢谢 编辑: 对不起,我没有说清楚。 我的问题是:我如何从一种形式中提取整数,并将它们放入另一种形式中,然后让它们被识别?将此添加到您的debugFo

我做了一个数学测验程序

我想添加一个debug菜单来更改不同的内容,例如使用debugForm上的按钮停止Form1的计时器。我设置了按钮和表单,但我不确定如何导入整数,例如:

int TimeLeft

into debugForm for use in Form1.

我不知道从哪里开始,所以我需要帮助,如果你需要我澄清一些事情,那么就问吧。谢谢

编辑:

对不起,我没有说清楚。

我的问题是:我如何从一种形式中提取整数,并将它们放入另一种形式中,然后让它们被识别?

将此添加到您的
debugForm
类中:

private int m_timeLeft;
public int TimeLeft
{
    get
    {
        return m_timeLeft;
    }
    set
    {
        m_timeLeft = value;
        // some other actions if necessary
    }
}
现在,您可以从
表单1
访问此属性:

private void makeMenu(object sender, EventArgs e)
{
    debugForm form = new debugForm();
    form.TimeLeft = this.TimeLeft;
    form.Show();
}

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

编辑:

好,下面是一个快速的会员访问示例:

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

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

 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
    }
}

但是请记住第一个表单上的任何更改都不会在第二个表单中更新您必须使用引用来访问更新的值

希望这有帮助

考虑阅读一本书(Deitel Beging C#是一本非常好的书,有一些很好的例子,而且Wrox Beging Visual C#2010/2012是一本非常好的书)
在这里发布你的问题是有用的,但是在求助之前,试着自己解决问题

如果修改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();
}

我找到的将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中的变量传递到新表单中,并将其分配给为新表单生成的变量。在我的示例中,这是multiplayerGame

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();
    }

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

这看起来更像是一个项目,而不是一个问题。您想从哪里导入整数?是否希望
TimeLeft
表示剩余时间,但您不知道如何执行此操作?TimeLeft表示Form1中剩余的时间,但我不确定如何在debugForm中识别TimeLeft。我尝试将Public置于Int之前,但似乎没有多大作用。我该怎么做呢?我几天前才开始编程,所以我还不擅长任何东西。谢谢
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();
    }