VB.NET编程语言的应用,能够帮助开发人员轻松的实现许多功能。其代码编写方式简单,应用方式灵活急受广大开发人员的喜爱。在这里我们将会学到VB.NET删除空白行的应用技巧,增加大家的编程实践经验。
有时从网页中复制文字,复制过来后有很多空行,想编程序将其删去,没有找到,只有自己写了。刚学VB.NET,代码很简单。有什么好的优化,还是那个地方要修改请通知我。
VB.NET删除空白行代码示例:
- Imports System.IO
- Imports System.Drawing
- Public Class FrmTextOPClass FrmTextOP
- Dim FPath As String
- Dim Rows As Integer
- Private Sub BtChooseTxtFile_Click()Sub
BtChooseTxtFile_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles BtChooseTxtFile.Click- FPath = ChooseAFile(, "文本文件", "*.txt")
- If FPath > "" Then
- TxtFilePath.Text = FPath
- 'MainTxtBox.Text = LoadFile(FPath)
- MainTxtBox.Text = My.Computer.FileSystem.
ReadAllText(FPath, System.Text.Encoding.Default)- SetTextLine()
- ToolStripStatusLabel1.Text = "文件路径:" & FPath
- End If
- End Sub
- Public Function ChooseAFile()Function
ChooseAFile(Optional ByVal TitleStr As
String = "选择你要的文件", Optional ByVal
TypesDec As String = "所有文件", Optional
ByVal ExtenStr As String = "*.*", Optional
ByVal IniDirStr As String = "") As String- Dim dlgOpen As New System.Windows.Forms.
OpenFileDialog- With dlgOpen
- .Title = TitleStr
- .Filter = TypesDec & "(" & ExtenStr & ")
|" & ExtenStr- If IniDirStr.Length > 0 Then
- .InitialDirectory = IniDirStr
- End If
- If .ShowDialog = Windows.Forms.
DialogResult.OK Then- ChooseAFile = .FileName '第一个文件
- Else
- ChooseAFile = ""
- End If
- End With
- dlgOpen = Nothing
- End Function
- Private Sub SetTextLine()Sub SetTextLine()
- Rows = 0
- Rows = MainTxtBox.Lines.Length
- ToolStripStatusLabel2.Text = "共 " & Rows & " 行"
- End Sub
- Private Sub BtClearText_Click()Sub
BtClearText_Click(ByVal sender As System.
Object, ByVal e As System.EventArgs) Handles
BtCreatNewText.Click- TxtFilePath.Text = ""
- MainTxtBox.Text = ""
- ToolStripStatusLabel2.Text = ""
- ToolStripStatusLabel1.Text =
"请选择一个文件文件或输入新内容!"- End Sub
- Private Sub FrmTextOP_Load()Sub
FrmTextOP_Load(ByVal
sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load- ToolStripStatusLabel1.Text =
"请选择一个文件文件或输入新内容!"- End Sub
- Private Sub BtDelBlankRow_Click()Sub
BtDelBlankRow_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles
BtDelBlankRow.Click- SetTextLine()
- If Rows = 0 Then
- ToolStripStatusLabel1.Text =
"没有文本内容,请选择一个文件文件或输入新内容!"- ElseIf Rows = 1 Then
- MainTxtBox.Text = Trim(MainTxtBox.Text)
- ElseIf Rows > 1 Then
- Dim SWriter As StreamWriter
- Dim FStream As FileStream
- Dim Temptextpath As String =
Application.StartupPath.ToString
& "TempText.txt"- FStream = New FileStream(Temptextpath,
FileMode.Create, FileAccess.Write)- SWriter = New StreamWriter(FStream,
System.Text.Encoding.Default)- SWriter.BaseStream.Seek(0, SeekOrigin.End)
- For i As Integer = 0 To Rows - 1
- If Trim(MainTxtBox.Lines(i)) <> "" Then
- SWriter.WriteLine(MainTxtBox.Lines(i))
- SWriter.Flush()
- End If
- Next
- SWriter.Close()
- FStream.Close()
- FStream = Nothing
- SWriter = Nothing
- MainTxtBox.Text = My.Computer.FileSystem.
ReadAllText(Temptextpath,
System.Text.Encoding.Default)- ToolStripStatusLabel1.Text =
"已经删除空白行!请注意保存文件!"- End If
- SetTextLine()
- End Sub
- Private Sub TxtFilePath_DoubleClick()
Sub TxtFilePath_DoubleClick(ByVal sender
As Object, ByVal e As System.EventArgs)
Handles TxtFilePath.DoubleClick- If FPath > "" Then
- System.Diagnostics.Process.Start(FPath)
- End If
- End Sub
- Private Sub TxtFilePath_TextChanged()Sub
TxtFilePath_TextChanged(ByVal sender As
Object, ByVal e As System.EventArgs)
Handles TxtFilePath.TextChanged- FPath = TxtFilePath.Text
- End Sub
- Private Sub BtSaveTextFile_Click()Sub
BtSaveTextFile_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles BtSaveTextFile.Click- Dim dr As DialogResult
- Dim SWriter As StreamWriter
- Dim FStream As FileStream
- SetTextLine()
- If Rows = 0 Then
- ToolStripStatusLabel1.Text =
"没有输入文字内容!无法保存文本文件!"- Exit Sub
- End If
- If FPath = Nothing Or FPath = "" Then
- SaveFileDialog1.Filter = "文本文件(*.txt)|*.txt"
- dr = SaveFileDialog1.ShowDialog()
- If dr = Windows.Forms.DialogResult.OK Then
- FPath = SaveFileDialog1.FileName
- End If
- End If
- If FPath > "" And Rows > 0 Then
- FStream = New FileStream
(FPath, FileMode.Create, FileAccess.Write)- SWriter = New StreamWriter
(FStream, System.Text.Encoding.Default)- SWriter.BaseStream.Seek(0, SeekOrigin.End)
- For i As Integer = 0 To Rows - 1
- SWriter.WriteLine(MainTxtBox.Lines(i))
- SWriter.Flush()
- Next
- SWriter.Close()
- FStream.Close()
- FStream = Nothing
- SWriter = Nothing
- ToolStripStatusLabel1.Text = "保存文件成功!"
- Else
- ToolStripStatusLabel1.Text =
"没有设置保存文件路径!或没有输入文字内容!"- End If
- End Sub
- Private Sub BtOpenCurFile_Click()Sub
BtOpenCurFile_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles BtOpenCurFile.Click- If FPath > "" Then
- System.Diagnostics.Process.Start(FPath)
- End If
- End Sub
- Private Sub BtBrowserDir_Click()Sub
BtBrowserDir_Click(ByVal sender As System.
Object, ByVal e As System.EventArgs)
Handles BtBrowserDir.Click- If FPath > "" Then
- Dim p As System.IO.FileInfo = My.Computer.
FileSystem.GetFileInfo(FPath)- System.Diagnostics.Process.Start("explorer.
exe", p.DirectoryName)- End If
- End Sub
- Private Sub BtExitApp_Click()Sub BtExitApp_
Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles BtExitApp.Click- Application.Exit()
- End Sub
- Private Sub ToolStripMenuItemBigFont_Click()Sub
ToolStripMenuItemBigFont_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles ToolStripMenuItemBigFont.Click- 'Dim bigFont As Font
- MainTxtBox.Font = New Font(MainTxtBox.Font.FontFamily, 13)
- End Sub
- Private Sub ToolStripMenuItemFontSet_Click()Sub
ToolStripMenuItemFontSet_Click(ByVal sender
As System.Object, ByVal e As System.EventArgs)
Handles ToolStripMenuItemFontSet.Click- FontDialog1.ShowDialog()
- MainTxtBox.Font = FontDialog1.Font
- End Sub
- Private Sub ToolStripMenuItemColorSet_Click()
Sub ToolStripMenuItemColorSet_Click(ByVal
sender As System.Object, ByVal e As System.
EventArgs) Handles ToolStripMenuItemColorSet.Click- ColorDialog1.ShowDialog()
- MainTxtBox.ForeColor = ColorDialog1.Color
- End Sub
- Private Sub ToolStripMenuItemMidFont_Click()
Sub ToolStripMenuItemMidFont_Click(ByVal
sender As System.Object, ByVal e As System.
EventArgs) Handles ToolStripMenuItemMidFont.Click- MainTxtBox.Font = New Font(MainTxtBox.Font.
FontFamily, 11)- End Sub
- Private Sub ToolStripMenuItemSmallFont_Click()
Sub ToolStripMenuItemSmallFont_Click(ByVal
sender As Object, ByVal e As System.
EventArgs) Handles ToolStripMenuItemSmallFont.Click- MainTxtBox.Font = New Font(MainTxtBox.Font.FontFamily, 9)
- End Sub
- End Class
以上就是我们为大家介绍的有关VB.NET删除空白行的实现方法。
【编辑推荐】