简体   繁体   English

想要从一种形式获取数据以将它们用作另一种形式的变量

[英]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.

相关问题 我想从一种形式中获取变量,然后以另一种形式将其提供给标签,我该怎么做 - I want to take a variable from one form and give it to a label in another how do I do this 我希望有一个页面可以从用户那里收集信息,并且我想获取这些信息并在其他页面和类上使用它 - I want to have a page that collects information from the user, and i want to take that information and use it on other pages and classes 如何使用从一种形式到另一种形式的集合 - how to use collection from one form to other 如何获取变量并在其他方法上使用 - How to take variable and use at other method 如何使用C#从xml文件中的多个节点获取数据,并在不同的文本文件中使用它们? - How to take data from multiple nodes in a xml file use them in different text file using c#? 可能会向我展示如何使用一个过程将数据添加到2个单独的表中,然后在另一个中引用其中一个 - Could I possibly be shown how to use a procedure to add data to 2 separate tables, then reference one of them in the other 从一种形式中检索数据并以另一种形式使用 - Retrieve data from one form and use it in another form 在C#中我想将变量从一个button1传递给另一个button2 - in c# i want to pass a variable from one button1 to other button2 我怎样才能让我的项目占用一列,以便我可以使用它插入另一个 window 的值和 select 来自 - How can i make my project take one column so i can use it in the insert into values from the other window with select from 如何自动将日期从Xtrascheduler转换为其他格式 - How to take the date automatically from the xtrascheduler to an other form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM