WinForms在窗体之间传递数据 [英] WinForms passing data between Forms

查看:89
本文介绍了WinForms在窗体之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 questions 的表,其字段名称为 qcategory 。在WFA中,我有一个ToolStripMenu,其中有一个名为 Simulation 的类别和一个名为 B 的子类别。因此,我想创建一个mysql SELECT,在其中仅选择值与子类别值相等的行。 (表中的列 qcategory 的值为 B )。这是字符串:

I have a table named questions with a field name qcategory. In WFA I have a ToolStripMenu where I have a category named Simulation and a sub-category named B. So, I want to create a mysql SELECT where to select only rows which values are equal with sub-category value. (Columns qcategory from table has value B). This is the string:

static string dataA = "SELECT DISTINCT * FROM questions order by rand() limit 1";

唯一的问题是我有2种形式。

The only problem is that I have 2 forms. One with menu and one where I want to make that select.

推荐答案

您应该尝试拆分UI代码和数据库码。这称为分层(MVVM,MVC,MVP等),但您不必这样做!

You should try to split your UI-code and your database code. This is called Layering (MVVM, MVC, MVP,...) but you don't have to!

有几种方法:

1)创建一个可以同时引用两种形式并在其中执行数据库逻辑的类。 (这是目前最干净的方法)

1) Make a class that both forms can reference and execute your database logic there. (that would be the cleanest way for now)

2)使用菜单在表单中创建一个事件,然后在其他表单中对此事件做出反应

2) Create an Event in your Form with the menu and react on it in the other Form

3)您的菜单Form拥有对另一个Form的引用,并在其中传递选定的子项在其上执行Method。

3) Your menu Form holds a reference to the other Form and executes a Method on it passing the selected subitem.

在代码中

1

public static class SqlClass
{
    public static void ExecuteQuery(string menuItem)
    {
        //execute query
    }
}


Form 1
//menu changed...
SqlClass.ExecuteQuery(menuItem)

2

Form1:
public event EventHandler<string> MenuItemChanged;

//menu changed...
if(this.MenuItemChanged != null)
    this.MenuItemChanged(this, menuitem)


Form2:
public Form2(Form1 otherForm)
{
    InitializeComponent();
    _otherForm.MenuItemChange += //... handle your sql code
}

3

private readonly Form2 _otherForm;

public Form1(Form2 otherForm)
{
    InitializeComponent();
    _otherForm = otherForm;
}

//menu changed...
otherForm.ExecuteQuery(menuitem);

对于示例,表2是您要执行查询的表单,因为存在方法/ Event-Handler定义将与您的数据库进行交互。

For the examples, Form 2 is the form where you want to execute your query because there is the Method/Event-Handler defined that will interact with your database.

要了解该解决方案,您需要一个更高层次的视角-您可以在Form(A类)的代码后面获取信息,并且想要使用

通常,您需要引用一个表单,该表单包含您感兴趣的信息,并在更改时告诉您该信息(事件)或

信息源告诉每个感兴趣的目的地(称为方法)。然后信息源保存对消费者的引用。

这两个概念只是相同的,只是交流(和引用)的方向发生了变化。

To understand the solution, you need a more high level perspective - you get an information in code behind of a Form (a Class) and you want to consume that information somewhere else.
In general you need a reference to the Form that holds the information you are interested in and it tells you the information when changed (Event) OR
the information source tells every interested destination (calls a Method). Then the Information source hold the references to the consumers.
Both concepts are the same just the direction of the communication (and reference) changes.

备选方案(选项1)是将信息目标移动到其他地方(例如,在静态类中)并在那使用。传递信息的机制几乎相同(通过参数化的Method调用),但是它封装了数据库代码(SQL查询执行)中的UI-colde(窗体)

The alternative (Option 1) is that you move your information destination somewhere else (e.g. in a static class) and consume it there. The Mechanism of passing the information is pretty much the same (via a parameterized Method call) but it encapsulates the UI-colde (Form) from the Database code (SQL query execution)

这篇关于WinForms在窗体之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆