如何将双数组传递给表单并在多个子表单中使用它?

时间:2017-12-06 10:46:53

标签: arrays vb.net forms

我已经搜索了很多但找不到解决方案。我有一个表格,在那个表格中我打电话给另一个表格(让我们说Form2)。在表单2中,我可以在一些文本框中输入值,然后我将这些值作为双数组传递回表单1:

Public Class Form1
  Dim a As Double
  Dim userDefined(3) As Double
  Dim Form_2 As Form2
  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    Form_2= New Form2       
    Form_2.ShowDialog()
    a = userDefined(0)

  End Sub

  Public WriteOnly Property setUserValues As Array
    Set(value As Array)

        userDefined = value

    End Set
  End Property
End Class


Public Class Form2
  Dim userDefined(0 To 3) As Double

Private Sub Btn_transmitValues_Click(sender As System.Object, e As System.EventArgs) Handles Btn_transmitValues.Click

    userDefined(0) = Txt_1.Text
    userDefined(1) = Txt_2.Text
    userDefined(2) = Txt_3.Text
    userDefined(3) = Txt_4.Text

    Form1.setUserValues = userDefined

    Me.Dispose()

End Sub

End Class

现在我想使用Button1_Click Sub中Form1中传递的Form2的双数组中的值。但是当我在Form1中将我的数组定义为

Dim userDefined(3) As Double

然后a(也在Form1中)总是0.当我在Form 1中将我的数组定义为

Dim userDefined() As Double

然后我得到错误“对象引用没有设置为对象的实例”,但是当我将鼠标悬停在正确的值上时会传递。我该怎么做才能获得合适的价值?提前谢谢。

2 个答案:

答案 0 :(得分:1)

所以我得到了它的工作,但我不知道这是一个好的风格。也许任何人都可以判断这是好还是坏编码。我基本上只是改变了传递变量的方式。首先我让Form2将新变量写入Form1数组,现在我让Form1读取Form2中的值。

我自己没有链接的是,在Form2中计算变量,然后处理Form2,之后变量被读取。也许这可能有问题,因为我调用一个封闭表格的变量?

#firstly change your model design
#an email can have 0 - n attachments

class EmailAttachment(models.Model):
    email = models.ForeignKey(Email)
    document = models.FileField(upload_to='files/%Y/%m/%d')

#if you want to save an attachment
# assume message is multipart
# 'msg' is email.message instance
for part in msg.get_payload():
    if 'attachment' in part.get('Content-Disposition',''):
        attachment = EmailAttachment()
        #saving it in a <uuid>.msg file name
        #use django ContentFile to manage files and BytesIO for stream  
        attachment.document.save(uuid.uuid4().hex + ".msg",
            ContentFile(
                BytesIO(
                    msg.get_payload(decode=True)
                ).getvalue()
            )
        )

表格2:

Public Class Form1
  Dim a As Double
  Dim userDefined(3) As Double
  Dim Form_2 As Form2

  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    Form_2= New Form2       
    Form_2.ShowDialog()
    userDefined = Form_2.getUserDefined

    a = userDefined(0)

  End Sub

End Class

答案 1 :(得分:-1)

一个很简单的方法是在Form_2中添加一个proprty,例如

Public Property userDefined() as Double

然后在表格一中,你有

Form_2= New Form2       
Form_2.ShowDialog()

在两行中添加这个 -

Form_2.userDefined= userDefined

您可能需要在Form_2.Shown事件中添加代码,以便在显示表单时,如果数组影响任何控件,则更新任何控件,但这是关于它的。