【问题标题】:How to get integers and different values from one form to another?如何从一种形式到另一种形式获取整数和不同的值?
【发布时间】:2013-10-30 16:25:38
【问题描述】:

我通过关注this tutorial 做了一个数学测验程序。

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

int TimeLeft

into debugForm for use in Form1.

Here is my code that I got so far.

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

编辑:

很抱歉没有很清楚。
我的问题是:我如何从一种形式中获取整数并将它们放入另一种形式中并让它们被识别?

【问题讨论】:

  • 这个设置看起来更像是一个项目而不是一个问题。
  • 你想从哪里导入整数?你想让TimeLeft代表剩余时间,但你不知道怎么做?
  • TimeLeft 代表 Form1 中剩余的时间成功,但我不确定如何让 TimeLeft 在 debugForm 中被识别。

标签: c# forms methods integer


【解决方案1】:

将此添加到您的 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();
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      编辑:

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

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

      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
          }
      

      }

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

      希望对你有帮助

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

      【讨论】:

      • 我尝试将 Public 放在 Int 的前面,但它似乎没有多大作用。我该怎么做呢?我几天前才开始编程,所以我还不是很擅长任何事情。谢谢!
      【解决方案4】:

      我发现传递 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();
          }
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-06
        • 2015-06-23
        • 1970-01-01
        相关资源
        最近更新 更多