VB问题请教高手:VB2010旗舰版如何把文本(TEXT1)显示到列表框(LISTBOX1)并且可以保存为TXT文件!谢谢
我建一个文本(TEXT1)和一个LISTBOX1及一个BUTTON1,我想在TEXT1里面输入内容回车后,这内容会在LISTBOX1里面显示,并且TEXT1会清空及有光标...
我建一个文本(TEXT1)和一个LISTBOX1及一个BUTTON1,我想在TEXT1里面输入内容回车后,这内容会在LISTBOX1里面显示,并且TEXT1会清空及有光标。另外要把所有在TEXT1里面输入过的都在LISTBOX1里面显示出来,并且不能重复!及点BUTTON1能保存为TXT文档!高分征代码!我用 ListBox1.Items.Add(TextBox1.Text)这个的话是可以显示,但文本框不能清空及重新有光票在文本框处!
展开
2个回答
展开全部
首先text1清空,你可以用TextBox1.Text = ""
然后让Text1有光标,你可以用TextBox1.Focus()
这两句都可以加载ListBox1.Items.Add(TextBox1.Text)后面(这段代码之前最好判断一下text1的内容)
在窗口上加入一个SaveFileDialog
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SaveFileDialog1.Filter() = "文本文件|*.txt"
SaveFileDialog1.ShowDialog()
End Sub
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyValue = Keys.Enter Then
For i = 0 To ListBox1.Items.Count - 1 ''将textbox1的内容和listbox1里所有内容对比
If TextBox1.Text = ListBox1.Items.Item(i) Then ''如果有相同的就退出sub
TextBox1.Text = ""
TextBox1.Focus()
Exit Sub
End If
Next
If TextBox1.Text <> "" Then ListBox1.Items.Add(TextBox1.Text)
TextBox1.Text = ""
TextBox1.Focus()
End If
End Sub
Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
Microsoft.VisualBasic.FileIO.FileSystem.WriteAllText(SaveFileDialog1.FileName, "", False)
For i = 0 To ListBox1.Items.Count - 1
Microsoft.VisualBasic.FileIO.FileSystem.WriteAllText(SaveFileDialog1.FileName, ListBox1.Items(i) & vbCrLf, True)
Next
End Sub
然后让Text1有光标,你可以用TextBox1.Focus()
这两句都可以加载ListBox1.Items.Add(TextBox1.Text)后面(这段代码之前最好判断一下text1的内容)
在窗口上加入一个SaveFileDialog
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SaveFileDialog1.Filter() = "文本文件|*.txt"
SaveFileDialog1.ShowDialog()
End Sub
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyValue = Keys.Enter Then
For i = 0 To ListBox1.Items.Count - 1 ''将textbox1的内容和listbox1里所有内容对比
If TextBox1.Text = ListBox1.Items.Item(i) Then ''如果有相同的就退出sub
TextBox1.Text = ""
TextBox1.Focus()
Exit Sub
End If
Next
If TextBox1.Text <> "" Then ListBox1.Items.Add(TextBox1.Text)
TextBox1.Text = ""
TextBox1.Focus()
End If
End Sub
Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
Microsoft.VisualBasic.FileIO.FileSystem.WriteAllText(SaveFileDialog1.FileName, "", False)
For i = 0 To ListBox1.Items.Count - 1
Microsoft.VisualBasic.FileIO.FileSystem.WriteAllText(SaveFileDialog1.FileName, ListBox1.Items(i) & vbCrLf, True)
Next
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询