打开文件和写作 [英] opening files and writing

查看:60
本文介绍了打开文件和写作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何打开我保存的文件并将信息放入不同的文本框?

how can i open a file i saved and place the info into different text boxes?

推荐答案

创建一个新的Windows应用程序,切换到代码观点&用以下代码替换现有的

代码:

公共类Form1

继承System.Windows.Forms.Form


#Region" Windows窗体设计器生成的代码


Public Sub New()

MyBase.New()


' 'Windows窗体设计器需要此调用。

InitializeComponent()

''在InitializeComponent()调用后添加任何初始化
< br $>
结束子


''表格覆盖处理以清理组件列表。

受保护的重载覆盖子处理(ByVal处理为布尔)

如果处置那么

如果不是(组件什么都没有)那么

components.Dispose()

结束如果

结束如果

MyBase.Dispose(处置)

结束子


'' Windows窗体设计器要求

私有组件As System.ComponentModel.IContainer


''注意:Windows窗体设计器需要以下过程

''可以使用Windows窗体设计器修改它。

''不要莫使用代码编辑器来区分它。

Friend WithEvents Button1 As System.Windows.Forms.Button

Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog

Friend WithEvents Label1 As System.Windows.Forms.Label

Friend WithEvents TextBox1 As System.Windows.Forms.TextBox

Friend WithEvents TextBox2 As System.Windows.Forms .TextBox

Friend WithEvents Button2 As System.Windows.Forms.Button

Friend WithEvents Button3 As System.Windows.Forms.Button

< System.Diagnostics.DebuggerStepThrough()> Private Sub

InitializeComponent()

Me.Button1 = New System.Windows.Forms.Button

Me.OpenFileDialog1 = New System.Windows。 Forms.OpenFileDialog

Me.Label1 = New System.Windows.Forms.Label

Me.TextBox1 = New System.Windows.Forms.TextBox

Me.TextBox2 = New System.Windows.Forms.TextBox

Me.Button2 = New System.Windows.Forms.Button

Me.Button3 = New System.Windows。 Forms.Button

Me.SuspendLayout()

''

''Button1

''

Me.Button1.Location = New System.Drawing.Point(512,32)

Me.Button1.Name =" Button1"

Me。 Button1.TabIndex = 0

Me.Button1.Text ="打开"

''

''Label1

''

Me.Label1.Location = New System.Drawing.Point(16,16)

Me.Label1.Name =" Label1"

Me.Label1.Size = New System.Drawing.Size(88,16)

Me.Label1.TabI ndex = 1

Me.Label1.Text ="文件名:"

''

''TextBox1

''

Me.TextBox1.Location = New System.Drawing.Point(16,32)

Me.TextBox1.Name =" TextBox1"

Me.TextBox1.Size = New System.Drawing.Size(480,20)

Me.TextBox1.TabIndex = 2

Me.TextBox1.Text =""

''

''TextBox2

''

Me.TextBox2.Location =新的System.Drawing.Point(16,64)

Me.TextBox2.Multiline = True

Me.TextBox2.Name =" TextBox2"

Me.TextBox2.Size = New System.Drawing.Size(568,216)

Me.TextBox2.TabIndex = 3

Me.TextBox2.Text ="

''

''Button2

''

Me.Button2.Location =新系统。 Drawing.Point(424,296)

Me.Button2.Name =" Button2"

Me.Button2.TabIndex = 4

Me .Button2.Text =" Clear&qu ot;

''

''Button3

''

Me.Button3.Location =新系统。 Drawing.Point(512,296)

Me.Button3.Name =" Button3"

Me.Button3.TabIndex = 5

Me .Button3.Text ="退出>

''

''Form1

''

我。 AutoScaleBaseSize = New System.Drawing.Size(5,13)

Me.ClientSize = New System.Drawing.Size(600,333)

Me.Controls.Add( Me.Button3)

Me.Controls.Add(Me.Button2)

Me.Controls.Add(Me.TextBox2)

我。 Controls.Add(Me.TextBox1)

Me.Controls.Add(Me.Label1)

Me.Controls.Add(Me.Button1)

Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle

Me.MaximizeBox = False

Me.Name =" Form1"

Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScree n

Me.Text =" Form1"

Me.ResumeLayout(False)


End Sub


#End Region


Dim strFilename As String

Dim sr作为IO.StreamReader


Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理Button1.Click

使用OpenFileDialog1

.InitialDirectory =

Environment.GetFolderPath(Environment.SpecialFolde r.Desktop)

.Filter ="文本文件(* .txt)| * .txt |所有文件(*。*)| *。*"

.FilterIndex = 1

如果.ShowDialog = DialogResult .OK然后

strFilename = .FileName

TextBox1.Text = strFilename

否则

退出子

结束如果

结束


sr =新IO.StreamReader(strFilename)

TextBox2.Text = sr.ReadToEnd

sr.Close()

End Sub


Private Sub TextBox1_GotFocus(ByVal sender As Object,ByVal e As

System.EventArgs)处理TextBox1.GotFocus

按钮1.Focus()

End Sub


Private Sub Button2_Click(ByVal sender As System.Object,ByVal e As

System .EventArgs)处理Button2.Click

TextBox1.Text = String.Empty

TextBox2.Text = String.Empty

strFilename = String.Empty

Button1.Focus()

End Sub


Private Sub Button3_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理Button3.Click

如果不是sr什么都没有那么sr.Close()

Application.Exit()

结束次级

结束班级


--------------------- ------


我特意为你写了这个&我希望这有帮助
Create a new Windows application, switch to code view & replace the existing
code with this:

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

''This call is required by the Windows Form Designer.
InitializeComponent()

''Add any initialization after the InitializeComponent() call

End Sub

''Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

''Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

''NOTE: The following procedure is required by the Windows Form Designer
''It can be modified using the Windows Form Designer.
''Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.Label1 = New System.Windows.Forms.Label
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.SuspendLayout()
''
''Button1
''
Me.Button1.Location = New System.Drawing.Point(512, 32)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Open"
''
''Label1
''
Me.Label1.Location = New System.Drawing.Point(16, 16)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(88, 16)
Me.Label1.TabIndex = 1
Me.Label1.Text = "Filename:"
''
''TextBox1
''
Me.TextBox1.Location = New System.Drawing.Point(16, 32)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(480, 20)
Me.TextBox1.TabIndex = 2
Me.TextBox1.Text = ""
''
''TextBox2
''
Me.TextBox2.Location = New System.Drawing.Point(16, 64)
Me.TextBox2.Multiline = True
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(568, 216)
Me.TextBox2.TabIndex = 3
Me.TextBox2.Text = ""
''
''Button2
''
Me.Button2.Location = New System.Drawing.Point(424, 296)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 4
Me.Button2.Text = "Clear"
''
''Button3
''
Me.Button3.Location = New System.Drawing.Point(512, 296)
Me.Button3.Name = "Button3"
Me.Button3.TabIndex = 5
Me.Button3.Text = "Exit"
''
''Form1
''
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(600, 333)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Button1)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScree n
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Dim strFilename As String
Dim sr As IO.StreamReader

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
With OpenFileDialog1
.InitialDirectory =
Environment.GetFolderPath(Environment.SpecialFolde r.Desktop)
.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
.FilterIndex = 1
If .ShowDialog = DialogResult.OK Then
strFilename = .FileName
TextBox1.Text = strFilename
Else
Exit Sub
End If
End With

sr = New IO.StreamReader(strFilename)
TextBox2.Text = sr.ReadToEnd
sr.Close()
End Sub

Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.GotFocus
Button1.Focus()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
TextBox1.Text = String.Empty
TextBox2.Text = String.Empty
strFilename = String.Empty
Button1.Focus()
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
If Not sr Is Nothing Then sr.Close()
Application.Exit()
End Sub
End Class

---------------------------

I wrote this especially for you & I hope this helps


是的,它有帮助,但我很痛苦,但我只是想学习,所以我可以

这样做也是最终的但是我如何添加到原始形式的开头

因为除了这个我几乎所有的东西都完成了,我可以将所有的

信息保存到记事本中的文本文件中我只是我希望从该文件中获取

信息(其全部内容如abcd efg dfghj)和

将每个分段放入预定的文本框中... sry再次因为是一个

痛苦
yea it helps but im sry for being a pain but im just trying to learn so i can
do this too eventuially but how wold i add opening to my original form
because i have almost everything done besides this and i can save all my
information to a text fle in notepad and i just would like to get the
inmformation from that file (its all on one like like abcd efg dfghj) and
have each segment go into a predetermined text box...sry again for being a
pain


你能澄清一下因为我对你想要的东西感到困惑吗?


从我的代码创建一个新项目。打开你的一个,添加一个OpenFileDialog

控件,粘贴我的代码打开文件...


这个ABC DEF GHI部分是什么?你想把这行分成3&

在3个不同的文本框中显示它吗?如果是这样,他们是多线的吗?


等待你的回答


" iwdu15"写道:
Can you clarify because I am confused by what you want?

Create a new project from my code. Open your one up, add a OpenFileDialog
control, paste in my code to open the file up...

What is this ABC DEF GHI part? Do you want to split that line into 3 &
display it in 3 different text boxes? If so, are they multiline?

Awaiting your answer

"iwdu15" wrote:
是的,它有帮助,但我很痛苦,但我只是想学习所以我可以这样做也是最后的事情,但我怎么加我的原始开口形式
因为除了这个我几乎已经完成了所有事情,我可以将所有的信息保存到记事本中的文本文件中,我只是想从该文件中获取
信息(它全部在一个像abcd efg dfghj)和
让每个片段进入一个预定的文本框...再次成为一个痛苦
yea it helps but im sry for being a pain but im just trying to learn so i can
do this too eventuially but how wold i add opening to my original form
because i have almost everything done besides this and i can save all my
information to a text fle in notepad and i just would like to get the
inmformation from that file (its all on one like like abcd efg dfghj) and
have each segment go into a predetermined text box...sry again for being a
pain



这篇关于打开文件和写作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆