【问题标题】:Controls not populated when form loaded for second time第二次加载表单时未填充控件
【发布时间】:2018-05-21 16:27:54
【问题描述】:

我有一个调用第二种形式的程序。第二种形式有一个由外部文件内容填充的组合框,用户需要在组合框中从显示的选项中选择一个选项。然后将此选择传递回完成大量工作的主窗体。

这一切在第一次完成时运行良好。但是,第二次调用此第二个表单时,下拉列表为空白。我已经通过一些调试确认正在运行正确的代码,并且正在通过“SecondForm.ComboBox1.Items.Add”添加条目(我可以清除组合框,检查它是否为零,读取数据,然后检查项目再次列表,它会正确增加)但它们只是没有显示在表单上。我不知道为什么或如何解决它。

所以代码的相关部分....

在表单级别,我有这条线来设置第二个表单,我相信我需要 WithEvents 将所选数据传回,据我所知:

Public Class Form1 Friend WithEvents SecondForm As New Form2

Public Sub OpenStripformatstxtToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenStripformatstxtToolStripMenuItem.Click

    Dim fd As OpenFileDialog = New OpenFileDialog()
    Dim pos1 As Integer
    Dim pos2 As Integer


    ' Select the file to open
    fd.Title = "Open File Dialog"
    fd.InitialDirectory = "C:\BEST\Data"
    fd.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
    fd.FilterIndex = 1
    fd.RestoreDirectory = True

    ' Put the filename selected in strfilename2
    If fd.ShowDialog() = DialogResult.OK Then
        strFileName2 = fd.FileName
    Else : Return
    End If

    If SecondForm.IsDisposed Then
        Dim secondform As New Form2

我怀疑上面的这一行是问题所在,我正在第二次创建表单,但没有 WithEvents 参数。但是我不能在这部分代码中使用它,我得到一个错误“'WithEvents'不是有效的局部变量声明”。我读过关闭和重新打开表单不是很好的编码,我应该隐藏/显示它们

        secondform.Show()
        InitializeComponent()

    Else
        SecondForm.Show()
    End If

    ' Copy the file contents to a string called sfcontents (Strip Format Contents)
    sfcontents = My.Computer.FileSystem.ReadAllText(fd.FileName)


    ' Define some points in the string, starting at the beginning
    pos1 = 1
    pos2 = 1


    ' Loop from the start to the end of the string
    For pos1 = 1 To Len(sfcontents)

        ' Look for FO, the strip name header, do the following if you find it
        If Mid(sfcontents, pos1, 3) = "FO " Then
            pos1 = pos1 + 3
            pos2 = pos1 + 1

            'Find the space after "FO " so we've captured the whole next word, that's the strip name
            Do Until Mid(sfcontents, pos2, 1) = " "
                pos2 = pos2 + 1
            Loop
            ' Add that strip name to the combobox for selecting by user
            SecondForm.ComboBox1.Items.Add(Mid(sfcontents, pos1, pos2 - pos1))

正是上面的这一行填充了 ComboBox,但是在显示表单的第一个实例后向用户显示的表单上没有显示该数据

        End If

        ' Next step in the string
    Next pos1
End Sub

Private Sub secondform_formclosing(sender As Object, e As FormClosingEventArgs) Handles SecondForm.FormClosing

这里有几百行代码,然后处理从表单关闭传递的数据,即 ComboBox 的选定值。这一切都适用于代码的第一次运行,但由于 ComboBox 在后续运行中为空,因此之后它就不起作用了。如果有人认为它会有所帮助,很高兴发布该代码,但我认为在这个阶段它只会使问题变得混乱,因为该代码看起来不错。但是,请参阅下面有关事件处理程序的部分...

End Sub

Form2.vb上的代码如下:

Public Class Form2
Public selectedstrip As String '= ComboBox1.SelectedItem
Public stripfunction As Integer

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If RadioButton1.Checked Then stripfunction = 1
    If RadioButton2.Checked Then stripfunction = 2
    If RadioButton3.Checked Then stripfunction = 3
    selectedstrip = ComboBox1.SelectedItem
    Me.Close()

End Sub
End Class

我在网上读到一点,说关闭和重新打开表单不是,请原谅双关语,好的表单。但是,然后我需要 form.hide 的事件处理程序,我似乎无法弄清楚如何使用它们,甚至不知道它们是什么。如果隐藏表单是一个更好的替代解决方案,如果有人可以为我指出正确的方向来说明如何做到这一点以及使用什么处理程序,那么我将不胜感激。

我可能在做一些非常愚蠢的事情,因为我所做的一切都是通过谷歌搜索自学的,而且我可能对我为什么需要做某些事情缺乏更深入的了解,因此对我的任何无知表示歉意。考虑到这一点,如果我以一种完全愚蠢的方式做任何事情,我愿意以一种有帮助的方式重写它,但我可能需要一些帮助才能做到这一点!

提前感谢任何人提供的任何帮助。

【问题讨论】:

    标签: vb.net forms


    【解决方案1】:

    主要问题出现在这里:

    If SecondForm.IsDisposed Then
        Dim secondform As New Form2
    

    您在那里声明了一个新的局部变量并将新的Form2 对象而不是成员变量分配给它,因此当您稍后引用成员变量来填充ComboBox 时,您并不是指Form2 您刚刚创建的实例。

    无论如何,您的代码相当奇怪。这是我的建议。

    首先,去掉在Form2 中填充ComboBox 的代码形式Form1。表单应该填充自己的控件。将用于填充ComboBox 的代码放入Form2Load 事件处理程序中。然后,您可以保证,每当您在 Form2 的新实例上调用 Show 时,将执行填充 ComboBox 的代码。这就是表单的工作方式。

    作为替代方案,假设您正在从文件中读取数据并且数据可能不会在会话过程中发生变化,请读取数据并将其放入 Load 事件处理程序的数组中 Form1然后将该数组传递给Form2 的构造函数。您必须自己编写该构造​​函数,并在其中使用数组数据填充ComboBox。这样一来,您就不会一遍又一遍地读取和处理同一个数据文件,但您仍然会在 Form2 中填充 Form2 的控件。

    其次,修改这段代码:

    If SecondForm.IsDisposed Then
        Dim secondform As New Form2
        secondform.Show()
        InitializeComponent()
    Else
        SecondForm.Show()
    End If
    

    到这里:

    If SecondForm.IsDisposed Then
        'Create and display a new instance.
        Secondform = New Form2
        Secondform.Show()
    Else
        'Focus the existing instance.
        SecondForm.Activate()
    End If
    

    注意这里没有局部变量,所以将新实例赋值给成员变量。

    InitializeComponent 也没有电话。该方法是基于设计器中的操作在表单上创建和配置控件的方法。唯一被调用的地方是在构造函数中。

    最后,如果Form2 的实例已经显示,则调用其Activate 方法以确保它具有焦点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-05
      • 1970-01-01
      相关资源
      最近更新 更多