[英]Want to take data from one form to use them as variable in an other
I'm almost done doing a Connect4 game with VS2012 using WinForms.我几乎完成了使用 WinForms 在 VS2012 上完成的 Connect4 游戏。 Everything is working well but I wanted to bring the options for the user on a dedicated Start Menu window.
一切正常,但我想在专用的“开始”菜单窗口中为用户提供选项。 On that menu I have two comboBoxes I need to take the text from to use them as a value for two variables in my other form (the game window).
在那个菜单上,我有两个组合框,我需要从中获取文本以将它们用作其他形式(游戏窗口)中两个变量的值。 I also have one New Game button that should call a method from my other form if that's possible (basically, I made an "Initialization()" method in my game form and I'd like it to be launched when I click the "New Game" button on the other form).
我还有一个新游戏按钮,如果可能的话,它应该从我的另一个表单中调用一个方法(基本上,我在我的游戏表单中创建了一个“Initialization()”方法,我希望它在我点击“New游戏”按钮在另一种形式)。
I only found tutorials that show how to do very basic things from one form to an other (such as labeling texts) but II didn't find an answer to my specific problem.我只找到了展示如何从一种形式到另一种形式(例如标记文本)做非常基本的事情的教程,但我没有找到我的具体问题的答案。
I used this in my main form to instantiate the menu form我在主窗体中使用它来实例化菜单窗体
public FormMenu myMenu;
myMenu = new FormMenu();
What I want to do is that I could do something like this in the other form :我想要做的是我可以用另一种形式做这样的事情:
amountOfRows = Int32.Parse(myMenu.comboBoxRows.Text);
amountOfColumns = Int32.Parse(myMenu.comboBoxColumns.Text);
Any idea how I could do that?知道我怎么做吗?
I would love to see some example code so I can help see where your confusion lies.我很想看到一些示例代码,以便我可以帮助您了解您的困惑所在。 WinForms requires the other form to be instantiated.
WinForms 要求实例化另一个表单。
OtherForm form = new OtherForm();
Once the form is instantiated you should be able to run code from it.一旦表单被实例化,您应该能够从中运行代码。
EDIT:编辑:
Based on your implementation I would suggest making public methods within FormMenu that return these int values.根据您的实现,我建议在 FormMenu 中创建返回这些 int 值的公共方法。
public int ReturnRows()
{
return Int32.Parse(myMenu.comboBoxRows.Text);
}
public int ReturnColumns()
{
return Int32.Parse(myMenu.comboBoxColumns.Text);
}
Then from the other form in which myMenu is instantiated you can call myMenu.ReturnRows() and myMenu.ReturnColumns()然后从另一个实例化 myMenu 的表单中,您可以调用 myMenu.ReturnRows() 和 myMenu.ReturnColumns()
The easiest way would be to store a reference to your form in the menue as a variable.最简单的方法是将您的表单引用存储在菜单中作为变量。 (you already named it
myMenu
) (您已经将其命名为
myMenu
)
Then you should create the property/properties you need in the form an add a setter for the values.然后您应该在表单中创建您需要的属性/属性,并为值添加一个设置器。 (see example here )
(见这里的例子)
Last you update the form fields with最后你更新表单域
myMenu.property = newvalue;
That`s all about it就是这样
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.