【发布时间】: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 的事件处理程序,我似乎无法弄清楚如何使用它们,甚至不知道它们是什么。如果隐藏表单是一个更好的替代解决方案,如果有人可以为我指出正确的方向来说明如何做到这一点以及使用什么处理程序,那么我将不胜感激。
我可能在做一些非常愚蠢的事情,因为我所做的一切都是通过谷歌搜索自学的,而且我可能对我为什么需要做某些事情缺乏更深入的了解,因此对我的任何无知表示歉意。考虑到这一点,如果我以一种完全愚蠢的方式做任何事情,我愿意以一种有帮助的方式重写它,但我可能需要一些帮助才能做到这一点!
提前感谢任何人提供的任何帮助。
【问题讨论】: