Board logo

標題: [分享] [VB2010] txt 簡易 讀 寫 視窗 [打印本頁]

作者: mark15jill    時間: 2013-3-7 13:45     標題: [VB2010] txt 簡易 讀 寫 視窗

預設目錄:D:\123.txt
[attach]14290[/attach]
有問題可以問...
此帖僅提供基本讀寫功能,並無提供關鍵字搜尋功能
  1. Imports System.IO
  2. '方法1
  3. Public Class Form1
  4.     Dim lab As New Label
  5.     Dim bum1, bum2 As New Button
  6.     Dim te1, te2 As New TextBox
  7.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  8.         With Me
  9.             .Controls.Add(lab)
  10.             .Height = 600
  11.             .Width = 800
  12.             .FormBorderStyle = Windows.Forms.FormBorderStyle.Fixed3D
  13.             With .Controls
  14.                 .Add(te1) : .Add(te2) : .Add(bum1) : .Add(bum2)
  15.             End With
  16.         End With
  17.         With te1
  18.             .ScrollBars = ScrollBars.Both
  19.             .BorderStyle = BorderStyle.Fixed3D
  20.             .Width = (Me.Width - 40) / 2
  21.             .Height = Me.Height - 150
  22.             .Location = New Point(10, 100)
  23.             .Multiline = True
  24.         End With

  25.         With te2
  26.             .ScrollBars = ScrollBars.Both
  27.             .BorderStyle = BorderStyle.Fixed3D
  28.             .Width = (Me.Width - 40) / 2
  29.             .Height = Me.Height - 150
  30.             .Location = New Point(10 + te1.Width + te1.Location.X, 100)
  31.             .Multiline = True
  32.         End With
  33.         With bum1
  34.             .TextAlign = ContentAlignment.MiddleCenter
  35.             .Text = "寫"
  36.             .Width = 100
  37.             .Height = 40
  38.             .Location = New Point(((te1.Location.X + te1.Width) / 2) - 50, 50)
  39.         End With

  40.         With bum2
  41.             .TextAlign = ContentAlignment.MiddleCenter
  42.             .Text = "讀"
  43.             .Width = 100
  44.             .Height = 40
  45.             .Location = New Point((Me.Width - te2.Location.X - 10) + ((te2.Width + te2.Location.X) / 4), 50)
  46.         End With

  47.         With lab
  48.             .TextAlign = ContentAlignment.MiddleCenter
  49.             .Location = New Point((Me.Width / 2) - 100, 10)
  50.             .Width = 200
  51.             .Height = 25
  52.             .AutoSize = False
  53.             .BackColor = Color.Peru
  54.             .BorderStyle = BorderStyle.Fixed3D
  55.             .Text = "先寫後讀"
  56.         End With

  57.         AddHandler bum1.Click, AddressOf button1_Click
  58.         AddHandler bum2.Click, AddressOf button2_Click


  59.     End Sub

  60.     Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
  61.         Dim kk1 As String
  62.         kk1 = te1.Text

  63.         Dim FileNum As Integer
  64.         Dim strTemp As String

  65.         FileNum = FreeFile()
  66.         FileOpen(FileNum, "D:\123.txt", OpenMode.Output)

  67.         strTemp = kk1
  68.         PrintLine(FileNum, strTemp)

  69.         FileClose(FileNum)


  70.     End Sub
  71.     Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
  72.         Dim FileNum As Integer
  73.         Dim strTemp As String

  74.         FileNum = FreeFile()
  75.         FileOpen(FileNum, "D:\123.txt", OpenMode.Input)

  76.         Do Until EOF(FileNum)
  77.             te2.Text = LineInput(FileNum) & vbNewLine
  78.         Loop

  79.         FileClose(FileNum)
  80.     End Sub

  81. End Class
複製代碼





歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)