简体   繁体   English

从一种形式获取数组并在另一种形式的标签中显示内容

[英]Take array from one form and display contents in labels on another form

I am working on some code that stores a user's answers into an array and compares them with another array with the correct answers in it. 我正在研究一些将用户答案存储到数组中的代码,并将它们与另一个具有正确答案的数组进行比较。 I have already got that part working. 我已经开始工作了。 The part I am stuck on is having the user's answers appear in labels on another form( the exam results form ) I create an array for the labels in the other form, then make it equal to the array of user answers, but for some reason they are not displaying on the next form when i click submit. 我被卡住的部分是让用户的答案出现在另一种形式的标签(检查结果表格)上。我以另一种形式为标签创建了一个数组,然后使其等于用户答案的​​数组,但是由于某种原因当我单击提交时,它们不会显示在下一个表单上。

Code: 码:

'Display user answers on the exam result form
    Dim aUserAnswersDisplayed() As String = {frmExamResults.lblUserAnswer1.Text, frmExamResults.lblUserAnswer2.Text,
                                             frmExamResults.lblUserAnswer3.Text, frmExamResults.lblUserAnswer4.Text,
                                             frmExamResults.lblUserAnswer5.Text, frmExamResults.lblUserAnswer6.Text,
                                             frmExamResults.lblUserAnswer7.Text, frmExamResults.lblUserAnswer8.Text,
                                             frmExamResults.lblUserAnswer9.Text, frmExamResults.lblUserAnswer10.Text,
                                             frmExamResults.lblUserAnswer11.Text, frmExamResults.lblUserAnswer12.Text,
                                             frmExamResults.lblUserAnswer13.Text, frmExamResults.lblUserAnswer14.Text,
                                             frmExamResults.lblUserAnswer15.Text, frmExamResults.lblUserAnswer16.Text,
                                             frmExamResults.lblUserAnswer17.Text, frmExamResults.lblUserAnswer18.Text,
                                             frmExamResults.lblUserAnswer19.Text, frmExamResults.lblUserAnswer20.Text}

    For intDisplayAnswers = 0 To 19
        aUserAnswersDisplayed(intDisplayAnswers) = aUserAnswers(intDisplayAnswers)
    Next

    frmExamResults.Show()

Add a constructor with parameters in FrmExamsResult and pass the array in the object constructor. 在FrmExamsResult中添加带有参数的构造函数,然后在对象构造函数中传递数组。 The other option is to define a property in the form. 另一种选择是在表单中定义属性。 Once instantiated the object you can set the array in the property. 实例化对象后,您可以在属性中设置数组。

Another option is to use a singleton class where you store the array. 另一种选择是使用存储数组的单例类。 Being singleton can be accessed from both forms. 可以从两种形式访问单身人士。

Greetings 问候

Suppose you have two form Named Form1 and Form2 and you want content from Form1 displayed on Form2 . 假设您有两个名为Form1Form2表单,并且希望Form1上的内容显示在Form2 Here is the method you can use 这是您可以使用的方法

Create a New() method on Form2 Form2上创建一个New()方法

Dim _value1 as <DataType>,.......
Public Sub New(ByVal value1 as <DataType>,...........)
    `INITIALIZATION FUNCTION WILL CREATE AUTOMATICALLY`
    _value1 = value1
End Sub

Now on Form1 , call Show() or ShowDialog() method after creating and using Form2 object. 现在在Form1 ,在创建和使用Form2对象后调用Show()ShowDialog()方法。 This methods will show you the constructor defined in Form1 created using New() method. 此方法将向您显示在使用New()方法创建的Form1定义的构造函数。 Pass the desired values and you will have your values on Form2 . 传递所需的值,您将在Form2上拥有您的值。

Dim a as Form1
a.ShowDialog(<Parameters here>)

Hope this helps.. 希望这可以帮助..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM