officeopenfile,openfiler安装配置
使用OpenFileDialog指定文本文件(.txt ),打开指定的文件并在TextBox中显示。
(1)在Vistual Studio上新建“Windows窗体APP应用程序”项目
)在表单1中放置标签、文本框和按钮。
设置textBox1属性:
Multiline=true
ScrollBars=Both
(3)表单1表单代码Form1.cs
用户系统; using System.Windows.Forms; using System.IO; namespacewindowsformsapplication1{ publicpartialclassform 1: form { public form1(} {初始化组件); //label1用于显示打开文件的路径名label1.Text=string .Empty; 打开button1.Text='文件. '; } privatevoidbutton1_ click (对象发送器,EventArgse ) openfiledialogopendlg=newopenfiledialog ); //打开文档(扩展名txt ) openDlg.Filter='文档|*.txt '; if (open DLG.showdialog (==dialogresult.ok ) ) /读取文本文件所以string [ ] lines=file.read all lines ) open DLG.fileles 在textBox1中显示Foreach(stringlineinlines ) textbox1.appendtext (行环境. newline ); //显示文件路径名label1.Text=openDlg.FileName; } } ()4)运行效果
其他回答将openFileDialog1和Timer1控件拖到Form1上,然后编写以下代码:
字符串文本路径;
if (openfile dialog.showdialog (==dialogresult.ok ) ) )
{
txtPath=openDialog.FileName;
}
s=file.readalllines(txtpath );
privatevoidtimer1_ tick (对象发送器,事件发送器) ) ) ) ) ) ) ) )。
{
if(nlines.Length ) )。
{
label1.Text=s[nline].ToString (;
}
else
{
nline=0;
}
nline=nline 1;
timer在这里做什么? nline是什么参数? 不是按追加答案button开始读吗? 如果textBox是单行,则在Timer中实现每隔一秒(或100ms,或这是查看Timer设置)显示一行。 nline是文本的第几行? 如果你有很多文本的话,就不能在textBox上全部显示吧。 当然,您也可以设置全局变量a,在Button的Click事件中将其递增,然后textBox.Text=s[a];则每次单击button时,textBox的内容都会发生一行变化