我需要在visual basic中编写这个小程序,但是我面临一个问题,即我无法在运行时更改颜色或添加列表形式的文本文件。
这是我不想做的事情的图片
http://i62.tinypic.com/30tghh0.png
这是我到目前为止编写的代码,
Public Class Form1
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
Dim listBox1 As New System.Windows.Forms.ListBox
listBox1.Text = "New List"
listBox1.Location = New Point(25, 25)
listBox1.Size = New Size(380, 280)
Me.Controls.Add(listBox1)
OpenToolStripMenuItem.Enabled = True
SaveToolStripMenuItem.Enabled = True
SaveAsToolStripMenuItem.Enabled = True
CloseToolStripMenuItem.Enabled = True
EditToolStripMenuItem.Enabled = True
End Sub
Private Sub ExirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExirToolStripMenuItem.Click
End
End Sub
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
If (OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
Dim myfile As String = OpenFileDialog1.FileName
Dim allLines As String() = File.ReadAllLines(myfile)
For Each line As String In allLines
ListBox1.Items.Add(line)
Next
End If
End Sub
End Class
您看到的问题出在OpenToolStripMenuItem子行中,行ListBox1.Items.Add(line)
是没有listbox1,因为还没有创建。与颜色相同,保存和其余部分。
所以,请帮我解决一下。
答案 0 :(得分:0)
将ListBox1的声明移到Class级别:
Public Class Form1
Private ListBox1 As System.Windows.Forms.ListBox
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
ListBox1 = New System.Windows.Forms.ListBox
...
End Sub
...
End Class
*但为什么要在运行时创建它呢?您可以在设计时添加它并将Visible()属性设置为False。单击“新建”按钮时,将Visible()更改为True。除非你每次都需要创建一个新的ListBox,所以你有多个?如果是这样,他们将如何布置?...