如何从一种形式获取整数和不同的值

本文关键字:整数 获取 一种 | 更新日期: 2023-09-27 18:37:26

我按照本教程制作了一个数学测验程序。

我想添加一个调试菜单来更改不同的东西,例如通过使用调试窗体上的按钮停止 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
    }
}

现在,您可以从Form1访问此属性:

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

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

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

编辑:

好的,这里有一个成员访问的快速示例:

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

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
    }

}

但请记住,第一个窗体上的任何更改都不会在第二个窗体中更新,您必须使用 REFERENCE 才能访问更新的值。

希望这有帮助

考虑读一本书(Deitel Beginning C#是一本非常棒的书,有一些很棒的例子,还有Wrox开始Visual C# 2010/2012是一本非常好的读物)在此处发布您的问题很有用,但在寻求帮助之前,请尝试自己解决问题。

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

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

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