2类和形式之间的关系 [英] relation between 2 class and forms

查看:104
本文介绍了2类和形式之间的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
我在C#中有2个表格。我想通过形式1中的组合框选择一个值,并在形式2中使用此值,不做任何更改!我不知道它的价值是什么。只知道通过按下组合框来选择它

我该怎么做?

解决方案





看看这些提示:

在两种表格之间传递信息,第1部分:父母对儿童 [ ^ ]

在两种形式之间传递信息,第2部分:儿童到父母 [ ^ ]

在两种表格之间转移信息,第3部分:儿童给孩子 [ ^ ]


引用:

在form1中,我写了Form2 sd = new Form2(); sd.ShowDialog();



我想通过组合框选择打开一个串口。在组合框中,我有一些串口作为com1,com2,...,com8。当我在form1中选择其中一个串口时,这个comx在form2中通过serialport1.open()打开;



现在我们(最后)知道你想要什么要做...我们可以继续。



第一项任务是分析我们是否处于需要通知 access :在这种情况下,我们知道当Form2的sd以模态方式显示时,它需要知道在Form1中选择了哪个ComboBox项目。



我们也知道,由于Form2以模态显示,因此用户在显示Form2时无法切换回Form1,并选择另一个ComboBox项:所以,我们不需要担心。



因此,知道我们处于访问而不是通知场景意味着我们要向Form2添加一个Public属性或某些Type的变量。



从技术上讲,我们将向Form2注入一个价值。您还可以正确地说,我们已经将Form2中的整数变量暴露为类的消费者。



在这种情况下,不需要Property提供给我们的设施,所以我们可以简单地在Form2中写下类似的东西:

  public 类型variableName; 



假设我们只需要在打开时将一个整数传递给'sd:

  //    
public int WhichSerialPort;



实际选择 make这里将使用可空整数来表示此变量:

  public   int ? WhichSerialPort =  null ; 



我将在下面解释。



然后,我们可以将注意力转向Form1。触发Form2模式显示的操作显然是用户在Form1中选择一个ComboBox项。



所以,在Form1的ComboBox中选择了IndexIndexChanged EventHandler:

  private   void  cmboWhichSerialPort_SelectedIndexChanged( object  sender,EventArgs e)
{
sd.WhichSerialPort = cmboWhichSerialPort.SelectedIndex;
sd.ShowModal();
}

你会注意到在上面的代码中,我们可以在显示之前设置'WhichSerialPort Form2的实例'的值。



然后,在Form2的表单加载EventHandler中:

  private   void  sd_Load( object  sender,EventArgs e)
{
// 如果WhichSerialPort == null ,则抛出错误
如果(!WhichSerialPort.HasValue){ throw new ArgumentNullException( 要打开的串行端口未指定); }

// 现在你有一个有效的整数
< span class =code-keyword> switch (WhichSerialPort)
{
case 0
// 打开串口#0的代码
break ;
case 1
/ / 打开串行端口#1的代码
break ;
}
}

使用可空整数来保存从Form1写入Form2的数据,这是一种防止错误的方法。因为整数是值Type,所以在创建时它会自动设置为#0。通过使用可空整数,并将其默认设置为null,我们可以在Form_Load EventHandler中测试变量,并确保它具有整数值,而不是null。/ / blockquote>
< blockquote>问题没有正确制定,但最有可能的是,这是关于表单协作的流行问题。最强大的解决方案是在表单类中实现适当的接口,并传递接口引用而不是引用Form的整个实例。有关更多详细信息,请参阅我以前的解决方案:如何以两种形式复制列表框之间的所有项目 [ ^ ]。



另请参阅此处的其他解决方案讨论。如果应用程序足够简单,解决方案就像在一个表单中声明一些 internal 属性并将对一个表单的实例的引用传递给另一个表单的实例一样简单形成。对于更复杂的项目,这种违反严格封装的样式和松散耦合可能会增加代码的意外复杂性并引发错误,因此封装良好的解决方案将是优惠。



另请参阅:

http://en.wikipedia.org/wiki/Accidental_complexity [ ^ ],

http://en.wikipedia.org/wiki/Loose_coupling [ ^ ]。



-SA


hi i have 2 forms in C#. i want to select a value by combo box in form no.1 and use this value in form no.2, without any changes! and i don't know what's the value. just know it selected by pressed on combobox
how can i do this?

解决方案

Hi,

Have a look at these tips:
Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]


Quote:

in form1, i wrote Form2 sd = new Form2(); sd.ShowDialog();

i want to open a serial port by combo box selection. in combo box, i have some of serial ports as "com1, com2, ..., com8". when i select one of these serial ports in form1, this "comx" opens in form2 by serialport1.open();


Now that we (finally) have an idea of what you want to do ... we can proceed.

The first task is to analyze whether we are in a scenario that requires notification or access: in this case we know that when Form2 'sd is shown, modally, that it needs to "know" which ComboBox Item was selected in Form1.

We also know that since Form2 is shown modally, the user cannot switch back to Form1 while Form2 is shown, and select another ComboBox Item: so, we don't need to worry about that.

So, knowing we are in an access rather than notification scenario means we are going to add either a Public property, or variable of some Type to Form2.

Technically, we are going to "inject a value" into Form2. You could also say, correctly, that we have "exposed" an integer variable in Form2 to "consumers of the class."

In this case, there's no need for the facilities a Property offers us, so we can, simply, in Form2 'sd, write something like:

public Type variableName;


Let's assume we only need to pass an integer into 'sd when it's opened:

// in Form sd
public int WhichSerialPort;


The choice I'd actually make here would be to use a nullable integer for this variable:

public int? WhichSerialPort = null;


And, I'll explain that below.

Then, we can turn our attention to Form1. The action that triggers the modal showing of Form2 is, evidently, the user selecting a ComboBox Item in Form1.

So, in Form1's ComboBox SelectedIndexChanged EventHandler:

private void cmboWhichSerialPort_SelectedIndexChanged(object sender, EventArgs e)
{
    sd.WhichSerialPort = cmboWhichSerialPort.SelectedIndex;
    sd.ShowModal();
}

You'll note that in the above code we can set the value of 'WhichSerialPort instance of Form2 'sd before it is shown.

Then, in the Form Load EventHandler of Form2 'sd:

private void sd_Load(object sender, EventArgs e)
{
    // throw an error if WhichSerialPort == null
    if (! WhichSerialPort.HasValue) { throw new ArgumentNullException("Serial Port to open is unspecified"); }

    // now you have a valid integer
    switch (WhichSerialPort)
    {
        case 0:
            // code for opening Serial Port #0
            break;
        case 1:
            // code for opening Serial Port #1
            break;
    }
}

Using a nullable integer to hold the data written into Form2 'sd from Form1 is useful as a way to guard against errors. Because an integer is a value Type, it is automatically set to #0 when it is created. By using a nullable integer, and setting it to 'null by default, we have the means to test the variable in the Form_Load EventHandler and make sure it has an integer value, rather than 'null.


The problem is not correctly formulated, but, most likely, this is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA


这篇关于2类和形式之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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